UNPKG

5.8 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2020 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.ChildLoadBalancerHandler = void 0;
20const load_balancer_1 = require("./load-balancer");
21const connectivity_state_1 = require("./connectivity-state");
22const TYPE_NAME = 'child_load_balancer_helper';
23class ChildLoadBalancerHandler {
24 constructor(channelControlHelper) {
25 this.channelControlHelper = channelControlHelper;
26 this.currentChild = null;
27 this.pendingChild = null;
28 this.latestConfig = null;
29 this.ChildPolicyHelper = class {
30 constructor(parent) {
31 this.parent = parent;
32 this.child = null;
33 }
34 createSubchannel(subchannelAddress, subchannelArgs) {
35 return this.parent.channelControlHelper.createSubchannel(subchannelAddress, subchannelArgs);
36 }
37 updateState(connectivityState, picker) {
38 var _a;
39 if (this.calledByPendingChild()) {
40 if (connectivityState === connectivity_state_1.ConnectivityState.CONNECTING) {
41 return;
42 }
43 (_a = this.parent.currentChild) === null || _a === void 0 ? void 0 : _a.destroy();
44 this.parent.currentChild = this.parent.pendingChild;
45 this.parent.pendingChild = null;
46 }
47 else if (!this.calledByCurrentChild()) {
48 return;
49 }
50 this.parent.channelControlHelper.updateState(connectivityState, picker);
51 }
52 requestReresolution() {
53 var _a;
54 const latestChild = (_a = this.parent.pendingChild) !== null && _a !== void 0 ? _a : this.parent.currentChild;
55 if (this.child === latestChild) {
56 this.parent.channelControlHelper.requestReresolution();
57 }
58 }
59 setChild(newChild) {
60 this.child = newChild;
61 }
62 addChannelzChild(child) {
63 this.parent.channelControlHelper.addChannelzChild(child);
64 }
65 removeChannelzChild(child) {
66 this.parent.channelControlHelper.removeChannelzChild(child);
67 }
68 calledByPendingChild() {
69 return this.child === this.parent.pendingChild;
70 }
71 calledByCurrentChild() {
72 return this.child === this.parent.currentChild;
73 }
74 };
75 }
76 configUpdateRequiresNewPolicyInstance(oldConfig, newConfig) {
77 return oldConfig.getLoadBalancerName() !== newConfig.getLoadBalancerName();
78 }
79 /**
80 * Prerequisites: lbConfig !== null and lbConfig.name is registered
81 * @param addressList
82 * @param lbConfig
83 * @param attributes
84 */
85 updateAddressList(addressList, lbConfig, attributes) {
86 let childToUpdate;
87 if (this.currentChild === null ||
88 this.latestConfig === null ||
89 this.configUpdateRequiresNewPolicyInstance(this.latestConfig, lbConfig)) {
90 const newHelper = new this.ChildPolicyHelper(this);
91 const newChild = (0, load_balancer_1.createLoadBalancer)(lbConfig, newHelper);
92 newHelper.setChild(newChild);
93 if (this.currentChild === null) {
94 this.currentChild = newChild;
95 childToUpdate = this.currentChild;
96 }
97 else {
98 if (this.pendingChild) {
99 this.pendingChild.destroy();
100 }
101 this.pendingChild = newChild;
102 childToUpdate = this.pendingChild;
103 }
104 }
105 else {
106 if (this.pendingChild === null) {
107 childToUpdate = this.currentChild;
108 }
109 else {
110 childToUpdate = this.pendingChild;
111 }
112 }
113 this.latestConfig = lbConfig;
114 childToUpdate.updateAddressList(addressList, lbConfig, attributes);
115 }
116 exitIdle() {
117 if (this.currentChild) {
118 this.currentChild.exitIdle();
119 if (this.pendingChild) {
120 this.pendingChild.exitIdle();
121 }
122 }
123 }
124 resetBackoff() {
125 if (this.currentChild) {
126 this.currentChild.resetBackoff();
127 if (this.pendingChild) {
128 this.pendingChild.resetBackoff();
129 }
130 }
131 }
132 destroy() {
133 /* Note: state updates are only propagated from the child balancer if that
134 * object is equal to this.currentChild or this.pendingChild. Since this
135 * function sets both of those to null, no further state updates will
136 * occur after this function returns. */
137 if (this.currentChild) {
138 this.currentChild.destroy();
139 this.currentChild = null;
140 }
141 if (this.pendingChild) {
142 this.pendingChild.destroy();
143 this.pendingChild = null;
144 }
145 }
146 getTypeName() {
147 return TYPE_NAME;
148 }
149}
150exports.ChildLoadBalancerHandler = ChildLoadBalancerHandler;
151//# sourceMappingURL=load-balancer-child-handler.js.map
\No newline at end of file