UNPKG

4.94 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 channel_1 = require("./channel");
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.ChildPolicyHelper = class {
29 constructor(parent) {
30 this.parent = parent;
31 this.child = null;
32 }
33 createSubchannel(subchannelAddress, subchannelArgs) {
34 return this.parent.channelControlHelper.createSubchannel(subchannelAddress, subchannelArgs);
35 }
36 updateState(connectivityState, picker) {
37 var _a;
38 if (this.calledByPendingChild()) {
39 if (connectivityState !== channel_1.ConnectivityState.READY) {
40 return;
41 }
42 (_a = this.parent.currentChild) === null || _a === void 0 ? void 0 : _a.destroy();
43 this.parent.currentChild = this.parent.pendingChild;
44 this.parent.pendingChild = null;
45 }
46 else if (!this.calledByCurrentChild()) {
47 return;
48 }
49 this.parent.channelControlHelper.updateState(connectivityState, picker);
50 }
51 requestReresolution() {
52 var _a;
53 const latestChild = (_a = this.parent.pendingChild) !== null && _a !== void 0 ? _a : this.parent.currentChild;
54 if (this.child === latestChild) {
55 this.parent.channelControlHelper.requestReresolution();
56 }
57 }
58 setChild(newChild) {
59 this.child = newChild;
60 }
61 calledByPendingChild() {
62 return this.child === this.parent.pendingChild;
63 }
64 calledByCurrentChild() {
65 return this.child === this.parent.currentChild;
66 }
67 };
68 }
69 /**
70 * Prerequisites: lbConfig !== null and lbConfig.name is registered
71 * @param addressList
72 * @param lbConfig
73 * @param attributes
74 */
75 updateAddressList(addressList, lbConfig, attributes) {
76 let childToUpdate;
77 if (this.currentChild === null ||
78 this.currentChild.getTypeName() !== lbConfig.getLoadBalancerName()) {
79 const newHelper = new this.ChildPolicyHelper(this);
80 const newChild = load_balancer_1.createLoadBalancer(lbConfig, newHelper);
81 newHelper.setChild(newChild);
82 if (this.currentChild === null) {
83 this.currentChild = newChild;
84 childToUpdate = this.currentChild;
85 }
86 else {
87 if (this.pendingChild) {
88 this.pendingChild.destroy();
89 }
90 this.pendingChild = newChild;
91 childToUpdate = this.pendingChild;
92 }
93 }
94 else {
95 if (this.pendingChild === null) {
96 childToUpdate = this.currentChild;
97 }
98 else {
99 childToUpdate = this.pendingChild;
100 }
101 }
102 childToUpdate.updateAddressList(addressList, lbConfig, attributes);
103 }
104 exitIdle() {
105 if (this.currentChild) {
106 this.currentChild.resetBackoff();
107 if (this.pendingChild) {
108 this.pendingChild.resetBackoff();
109 }
110 }
111 }
112 resetBackoff() {
113 if (this.currentChild) {
114 this.currentChild.resetBackoff();
115 if (this.pendingChild) {
116 this.pendingChild.resetBackoff();
117 }
118 }
119 }
120 destroy() {
121 if (this.currentChild) {
122 this.currentChild.destroy();
123 this.currentChild = null;
124 }
125 if (this.pendingChild) {
126 this.pendingChild.destroy();
127 this.pendingChild = null;
128 }
129 }
130 getTypeName() {
131 return TYPE_NAME;
132 }
133}
134exports.ChildLoadBalancerHandler = ChildLoadBalancerHandler;
135//# sourceMappingURL=load-balancer-child-handler.js.map
\No newline at end of file