UNPKG

4.84 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2019 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.validateLoadBalancingConfig = exports.getFirstUsableConfig = exports.isLoadBalancerNameRegistered = exports.createLoadBalancer = exports.registerDefaultLoadBalancerType = exports.registerLoadBalancerType = exports.createChildChannelControlHelper = void 0;
20/**
21 * Create a child ChannelControlHelper that overrides some methods of the
22 * parent while letting others pass through to the parent unmodified. This
23 * allows other code to create these children without needing to know about
24 * all of the methods to be passed through.
25 * @param parent
26 * @param overrides
27 */
28function 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}
38exports.createChildChannelControlHelper = createChildChannelControlHelper;
39const registeredLoadBalancerTypes = {};
40let defaultLoadBalancerType = null;
41function registerLoadBalancerType(typeName, loadBalancerType, loadBalancingConfigType) {
42 registeredLoadBalancerTypes[typeName] = {
43 LoadBalancer: loadBalancerType,
44 LoadBalancingConfig: loadBalancingConfigType,
45 };
46}
47exports.registerLoadBalancerType = registerLoadBalancerType;
48function registerDefaultLoadBalancerType(typeName) {
49 defaultLoadBalancerType = typeName;
50}
51exports.registerDefaultLoadBalancerType = registerDefaultLoadBalancerType;
52function 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}
61exports.createLoadBalancer = createLoadBalancer;
62function isLoadBalancerNameRegistered(typeName) {
63 return typeName in registeredLoadBalancerTypes;
64}
65exports.isLoadBalancerNameRegistered = isLoadBalancerNameRegistered;
66function 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}
84exports.getFirstUsableConfig = getFirstUsableConfig;
85// eslint-disable-next-line @typescript-eslint/no-explicit-any
86function 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}
102exports.validateLoadBalancingConfig = validateLoadBalancingConfig;
103//# sourceMappingURL=load-balancer.js.map
\No newline at end of file