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