1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | import { ChannelOptions } from './channel-options';
|
19 | import { Endpoint, SubchannelAddress } from './subchannel-address';
|
20 | import { ConnectivityState } from './connectivity-state';
|
21 | import { Picker } from './picker';
|
22 | import type { ChannelRef, SubchannelRef } from './channelz';
|
23 | import { SubchannelInterface } from './subchannel-interface';
|
24 | import { LoadBalancingConfig } from './service-config';
|
25 | import { log } from './logging';
|
26 | import { LogVerbosity } from './constants';
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | export interface ChannelControlHelper {
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 | createSubchannel(
|
39 | subchannelAddress: SubchannelAddress,
|
40 | subchannelArgs: ChannelOptions
|
41 | ): SubchannelInterface;
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | updateState(connectivityState: ConnectivityState, picker: Picker): void;
|
50 | |
51 |
|
52 |
|
53 | requestReresolution(): void;
|
54 | addChannelzChild(child: ChannelRef | SubchannelRef): void;
|
55 | removeChannelzChild(child: ChannelRef | SubchannelRef): void;
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | export function createChildChannelControlHelper(
|
67 | parent: ChannelControlHelper,
|
68 | overrides: Partial<ChannelControlHelper>
|
69 | ): ChannelControlHelper {
|
70 | return {
|
71 | createSubchannel:
|
72 | overrides.createSubchannel?.bind(overrides) ??
|
73 | parent.createSubchannel.bind(parent),
|
74 | updateState:
|
75 | overrides.updateState?.bind(overrides) ?? parent.updateState.bind(parent),
|
76 | requestReresolution:
|
77 | overrides.requestReresolution?.bind(overrides) ??
|
78 | parent.requestReresolution.bind(parent),
|
79 | addChannelzChild:
|
80 | overrides.addChannelzChild?.bind(overrides) ??
|
81 | parent.addChannelzChild.bind(parent),
|
82 | removeChannelzChild:
|
83 | overrides.removeChannelzChild?.bind(overrides) ??
|
84 | parent.removeChannelzChild.bind(parent),
|
85 | };
|
86 | }
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | export interface LoadBalancer {
|
93 | |
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | updateAddressList(
|
103 | endpointList: Endpoint[],
|
104 | lbConfig: TypedLoadBalancingConfig,
|
105 | attributes: { [key: string]: unknown }
|
106 | ): void;
|
107 | |
108 |
|
109 |
|
110 | exitIdle(): void;
|
111 | |
112 |
|
113 |
|
114 |
|
115 |
|
116 | resetBackoff(): void;
|
117 | |
118 |
|
119 |
|
120 |
|
121 | destroy(): void;
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 | getTypeName(): string;
|
128 | }
|
129 |
|
130 | export interface LoadBalancerConstructor {
|
131 | new (
|
132 | channelControlHelper: ChannelControlHelper,
|
133 | options: ChannelOptions
|
134 | ): LoadBalancer;
|
135 | }
|
136 |
|
137 | export interface TypedLoadBalancingConfig {
|
138 | getLoadBalancerName(): string;
|
139 | toJsonObject(): object;
|
140 | }
|
141 |
|
142 | export interface TypedLoadBalancingConfigConstructor {
|
143 |
|
144 | new (...args: any): TypedLoadBalancingConfig;
|
145 |
|
146 | createFromJson(obj: any): TypedLoadBalancingConfig;
|
147 | }
|
148 |
|
149 | const registeredLoadBalancerTypes: {
|
150 | [name: string]: {
|
151 | LoadBalancer: LoadBalancerConstructor;
|
152 | LoadBalancingConfig: TypedLoadBalancingConfigConstructor;
|
153 | };
|
154 | } = {};
|
155 |
|
156 | let defaultLoadBalancerType: string | null = null;
|
157 |
|
158 | export function registerLoadBalancerType(
|
159 | typeName: string,
|
160 | loadBalancerType: LoadBalancerConstructor,
|
161 | loadBalancingConfigType: TypedLoadBalancingConfigConstructor
|
162 | ) {
|
163 | registeredLoadBalancerTypes[typeName] = {
|
164 | LoadBalancer: loadBalancerType,
|
165 | LoadBalancingConfig: loadBalancingConfigType,
|
166 | };
|
167 | }
|
168 |
|
169 | export function registerDefaultLoadBalancerType(typeName: string) {
|
170 | defaultLoadBalancerType = typeName;
|
171 | }
|
172 |
|
173 | export function createLoadBalancer(
|
174 | config: TypedLoadBalancingConfig,
|
175 | channelControlHelper: ChannelControlHelper,
|
176 | options: ChannelOptions
|
177 | ): LoadBalancer | null {
|
178 | const typeName = config.getLoadBalancerName();
|
179 | if (typeName in registeredLoadBalancerTypes) {
|
180 | return new registeredLoadBalancerTypes[typeName].LoadBalancer(
|
181 | channelControlHelper,
|
182 | options
|
183 | );
|
184 | } else {
|
185 | return null;
|
186 | }
|
187 | }
|
188 |
|
189 | export function isLoadBalancerNameRegistered(typeName: string): boolean {
|
190 | return typeName in registeredLoadBalancerTypes;
|
191 | }
|
192 |
|
193 | export function parseLoadBalancingConfig(
|
194 | rawConfig: LoadBalancingConfig
|
195 | ): TypedLoadBalancingConfig {
|
196 | const keys = Object.keys(rawConfig);
|
197 | if (keys.length !== 1) {
|
198 | throw new Error(
|
199 | 'Provided load balancing config has multiple conflicting entries'
|
200 | );
|
201 | }
|
202 | const typeName = keys[0];
|
203 | if (typeName in registeredLoadBalancerTypes) {
|
204 | try {
|
205 | return registeredLoadBalancerTypes[
|
206 | typeName
|
207 | ].LoadBalancingConfig.createFromJson(rawConfig[typeName]);
|
208 | } catch (e) {
|
209 | throw new Error(`${typeName}: ${(e as Error).message}`);
|
210 | }
|
211 | } else {
|
212 | throw new Error(`Unrecognized load balancing config name ${typeName}`);
|
213 | }
|
214 | }
|
215 |
|
216 | export function getDefaultConfig() {
|
217 | if (!defaultLoadBalancerType) {
|
218 | throw new Error('No default load balancer type registered');
|
219 | }
|
220 | return new registeredLoadBalancerTypes[
|
221 | defaultLoadBalancerType
|
222 | ]!.LoadBalancingConfig();
|
223 | }
|
224 |
|
225 | export function selectLbConfigFromList(
|
226 | configs: LoadBalancingConfig[],
|
227 | fallbackTodefault = false
|
228 | ): TypedLoadBalancingConfig | null {
|
229 | for (const config of configs) {
|
230 | try {
|
231 | return parseLoadBalancingConfig(config);
|
232 | } catch (e) {
|
233 | log(
|
234 | LogVerbosity.DEBUG,
|
235 | 'Config parsing failed with error',
|
236 | (e as Error).message
|
237 | );
|
238 | continue;
|
239 | }
|
240 | }
|
241 | if (fallbackTodefault) {
|
242 | if (defaultLoadBalancerType) {
|
243 | return new registeredLoadBalancerTypes[
|
244 | defaultLoadBalancerType
|
245 | ]!.LoadBalancingConfig();
|
246 | } else {
|
247 | return null;
|
248 | }
|
249 | } else {
|
250 | return null;
|
251 | }
|
252 | }
|