1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.validateLoadBalancingConfig = exports.getFirstUsableConfig = exports.isLoadBalancerNameRegistered = exports.createLoadBalancer = exports.registerDefaultLoadBalancerType = exports.registerLoadBalancerType = exports.createChildChannelControlHelper = void 0;
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function createChildChannelControlHelper(parent, overrides) {
|
29 | var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
30 | return {
|
31 | createSubchannel: (_b = (_a = overrides.createSubchannel) === null || _a === void 0 ? void 0 : _a.bind(overrides)) !== null && _b !== void 0 ? _b : parent.createSubchannel.bind(parent),
|
32 | updateState: (_d = (_c = overrides.updateState) === null || _c === void 0 ? void 0 : _c.bind(overrides)) !== null && _d !== void 0 ? _d : parent.updateState.bind(parent),
|
33 | requestReresolution: (_f = (_e = overrides.requestReresolution) === null || _e === void 0 ? void 0 : _e.bind(overrides)) !== null && _f !== void 0 ? _f : parent.requestReresolution.bind(parent),
|
34 | addChannelzChild: (_h = (_g = overrides.addChannelzChild) === null || _g === void 0 ? void 0 : _g.bind(overrides)) !== null && _h !== void 0 ? _h : parent.addChannelzChild.bind(parent),
|
35 | removeChannelzChild: (_k = (_j = overrides.removeChannelzChild) === null || _j === void 0 ? void 0 : _j.bind(overrides)) !== null && _k !== void 0 ? _k : parent.removeChannelzChild.bind(parent),
|
36 | };
|
37 | }
|
38 | exports.createChildChannelControlHelper = createChildChannelControlHelper;
|
39 | const registeredLoadBalancerTypes = {};
|
40 | let defaultLoadBalancerType = null;
|
41 | function registerLoadBalancerType(typeName, loadBalancerType, loadBalancingConfigType) {
|
42 | registeredLoadBalancerTypes[typeName] = {
|
43 | LoadBalancer: loadBalancerType,
|
44 | LoadBalancingConfig: loadBalancingConfigType,
|
45 | };
|
46 | }
|
47 | exports.registerLoadBalancerType = registerLoadBalancerType;
|
48 | function registerDefaultLoadBalancerType(typeName) {
|
49 | defaultLoadBalancerType = typeName;
|
50 | }
|
51 | exports.registerDefaultLoadBalancerType = registerDefaultLoadBalancerType;
|
52 | function createLoadBalancer(config, channelControlHelper) {
|
53 | const typeName = config.getLoadBalancerName();
|
54 | if (typeName in registeredLoadBalancerTypes) {
|
55 | return new registeredLoadBalancerTypes[typeName].LoadBalancer(channelControlHelper);
|
56 | }
|
57 | else {
|
58 | return null;
|
59 | }
|
60 | }
|
61 | exports.createLoadBalancer = createLoadBalancer;
|
62 | function isLoadBalancerNameRegistered(typeName) {
|
63 | return typeName in registeredLoadBalancerTypes;
|
64 | }
|
65 | exports.isLoadBalancerNameRegistered = isLoadBalancerNameRegistered;
|
66 | function getFirstUsableConfig(configs, fallbackTodefault = false) {
|
67 | for (const config of configs) {
|
68 | if (config.getLoadBalancerName() in registeredLoadBalancerTypes) {
|
69 | return config;
|
70 | }
|
71 | }
|
72 | if (fallbackTodefault) {
|
73 | if (defaultLoadBalancerType) {
|
74 | return new registeredLoadBalancerTypes[defaultLoadBalancerType].LoadBalancingConfig();
|
75 | }
|
76 | else {
|
77 | return null;
|
78 | }
|
79 | }
|
80 | else {
|
81 | return null;
|
82 | }
|
83 | }
|
84 | exports.getFirstUsableConfig = getFirstUsableConfig;
|
85 |
|
86 | function validateLoadBalancingConfig(obj) {
|
87 | if (!(obj !== null && typeof obj === 'object')) {
|
88 | throw new Error('Load balancing config must be an object');
|
89 | }
|
90 | const keys = Object.keys(obj);
|
91 | if (keys.length !== 1) {
|
92 | throw new Error('Provided load balancing config has multiple conflicting entries');
|
93 | }
|
94 | const typeName = keys[0];
|
95 | if (typeName in registeredLoadBalancerTypes) {
|
96 | return registeredLoadBalancerTypes[typeName].LoadBalancingConfig.createFromJson(obj[typeName]);
|
97 | }
|
98 | else {
|
99 | throw new Error(`Unrecognized load balancing config name ${typeName}`);
|
100 | }
|
101 | }
|
102 | exports.validateLoadBalancingConfig = validateLoadBalancingConfig;
|
103 |
|
\ | No newline at end of file |