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 { SubchannelAddress } from './subchannel-address';
|
20 | import { ConnectivityState } from './connectivity-state';
|
21 | import { Picker } from './picker';
|
22 | import { ChannelRef, SubchannelRef } from './channelz';
|
23 | import { SubchannelInterface } from './subchannel-interface';
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | export interface ChannelControlHelper {
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 | createSubchannel(
|
36 | subchannelAddress: SubchannelAddress,
|
37 | subchannelArgs: ChannelOptions
|
38 | ): SubchannelInterface;
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | updateState(connectivityState: ConnectivityState, picker: Picker): void;
|
47 | |
48 |
|
49 |
|
50 | requestReresolution(): void;
|
51 | addChannelzChild(child: ChannelRef | SubchannelRef): void;
|
52 | removeChannelzChild(child: ChannelRef | SubchannelRef): void;
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | export function createChildChannelControlHelper(
|
64 | parent: ChannelControlHelper,
|
65 | overrides: Partial<ChannelControlHelper>
|
66 | ): ChannelControlHelper {
|
67 | return {
|
68 | createSubchannel:
|
69 | overrides.createSubchannel?.bind(overrides) ??
|
70 | parent.createSubchannel.bind(parent),
|
71 | updateState:
|
72 | overrides.updateState?.bind(overrides) ?? parent.updateState.bind(parent),
|
73 | requestReresolution:
|
74 | overrides.requestReresolution?.bind(overrides) ??
|
75 | parent.requestReresolution.bind(parent),
|
76 | addChannelzChild:
|
77 | overrides.addChannelzChild?.bind(overrides) ??
|
78 | parent.addChannelzChild.bind(parent),
|
79 | removeChannelzChild:
|
80 | overrides.removeChannelzChild?.bind(overrides) ??
|
81 | parent.removeChannelzChild.bind(parent),
|
82 | };
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | export interface LoadBalancer {
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | updateAddressList(
|
100 | addressList: SubchannelAddress[],
|
101 | lbConfig: LoadBalancingConfig,
|
102 | attributes: { [key: string]: unknown }
|
103 | ): void;
|
104 | |
105 |
|
106 |
|
107 | exitIdle(): void;
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 | resetBackoff(): void;
|
114 | |
115 |
|
116 |
|
117 |
|
118 | destroy(): void;
|
119 | |
120 |
|
121 |
|
122 |
|
123 |
|
124 | getTypeName(): string;
|
125 | }
|
126 |
|
127 | export interface LoadBalancerConstructor {
|
128 | new (channelControlHelper: ChannelControlHelper): LoadBalancer;
|
129 | }
|
130 |
|
131 | export interface LoadBalancingConfig {
|
132 | getLoadBalancerName(): string;
|
133 | toJsonObject(): object;
|
134 | }
|
135 |
|
136 | export interface LoadBalancingConfigConstructor {
|
137 |
|
138 | new (...args: any): LoadBalancingConfig;
|
139 |
|
140 | createFromJson(obj: any): LoadBalancingConfig;
|
141 | }
|
142 |
|
143 | const registeredLoadBalancerTypes: {
|
144 | [name: string]: {
|
145 | LoadBalancer: LoadBalancerConstructor;
|
146 | LoadBalancingConfig: LoadBalancingConfigConstructor;
|
147 | };
|
148 | } = {};
|
149 |
|
150 | let defaultLoadBalancerType: string | null = null;
|
151 |
|
152 | export function registerLoadBalancerType(
|
153 | typeName: string,
|
154 | loadBalancerType: LoadBalancerConstructor,
|
155 | loadBalancingConfigType: LoadBalancingConfigConstructor
|
156 | ) {
|
157 | registeredLoadBalancerTypes[typeName] = {
|
158 | LoadBalancer: loadBalancerType,
|
159 | LoadBalancingConfig: loadBalancingConfigType,
|
160 | };
|
161 | }
|
162 |
|
163 | export function registerDefaultLoadBalancerType(typeName: string) {
|
164 | defaultLoadBalancerType = typeName;
|
165 | }
|
166 |
|
167 | export function createLoadBalancer(
|
168 | config: LoadBalancingConfig,
|
169 | channelControlHelper: ChannelControlHelper
|
170 | ): LoadBalancer | null {
|
171 | const typeName = config.getLoadBalancerName();
|
172 | if (typeName in registeredLoadBalancerTypes) {
|
173 | return new registeredLoadBalancerTypes[typeName].LoadBalancer(
|
174 | channelControlHelper
|
175 | );
|
176 | } else {
|
177 | return null;
|
178 | }
|
179 | }
|
180 |
|
181 | export function isLoadBalancerNameRegistered(typeName: string): boolean {
|
182 | return typeName in registeredLoadBalancerTypes;
|
183 | }
|
184 |
|
185 | export function getFirstUsableConfig(
|
186 | configs: LoadBalancingConfig[],
|
187 | fallbackTodefault?: true
|
188 | ): LoadBalancingConfig;
|
189 | export function getFirstUsableConfig(
|
190 | configs: LoadBalancingConfig[],
|
191 | fallbackTodefault = false
|
192 | ): LoadBalancingConfig | null {
|
193 | for (const config of configs) {
|
194 | if (config.getLoadBalancerName() in registeredLoadBalancerTypes) {
|
195 | return config;
|
196 | }
|
197 | }
|
198 | if (fallbackTodefault) {
|
199 | if (defaultLoadBalancerType) {
|
200 | return new registeredLoadBalancerTypes[
|
201 | defaultLoadBalancerType
|
202 | ]!.LoadBalancingConfig();
|
203 | } else {
|
204 | return null;
|
205 | }
|
206 | } else {
|
207 | return null;
|
208 | }
|
209 | }
|
210 |
|
211 |
|
212 | export function validateLoadBalancingConfig(obj: any): LoadBalancingConfig {
|
213 | if (!(obj !== null && typeof obj === 'object')) {
|
214 | throw new Error('Load balancing config must be an object');
|
215 | }
|
216 | const keys = Object.keys(obj);
|
217 | if (keys.length !== 1) {
|
218 | throw new Error(
|
219 | 'Provided load balancing config has multiple conflicting entries'
|
220 | );
|
221 | }
|
222 | const typeName = keys[0];
|
223 | if (typeName in registeredLoadBalancerTypes) {
|
224 | return registeredLoadBalancerTypes[
|
225 | typeName
|
226 | ].LoadBalancingConfig.createFromJson(obj[typeName]);
|
227 | } else {
|
228 | throw new Error(`Unrecognized load balancing config name ${typeName}`);
|
229 | }
|
230 | }
|