UNPKG

5.23 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.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 !== connectivity_state_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 addChannelzChild(child) {
62 this.parent.channelControlHelper.addChannelzChild(child);
63 }
64 removeChannelzChild(child) {
65 this.parent.channelControlHelper.removeChannelzChild(child);
66 }
67 calledByPendingChild() {
68 return this.child === this.parent.pendingChild;
69 }
70 calledByCurrentChild() {
71 return this.child === this.parent.currentChild;
72 }
73 };
74 }
75 /**
76 * Prerequisites: lbConfig !== null and lbConfig.name is registered
77 * @param addressList
78 * @param lbConfig
79 * @param attributes
80 */
81 updateAddressList(addressList, lbConfig, attributes) {
82 let childToUpdate;
83 if (this.currentChild === null ||
84 this.currentChild.getTypeName() !== lbConfig.getLoadBalancerName()) {
85 const newHelper = new this.ChildPolicyHelper(this);
86 const newChild = load_balancer_1.createLoadBalancer(lbConfig, newHelper);
87 newHelper.setChild(newChild);
88 if (this.currentChild === null) {
89 this.currentChild = newChild;
90 childToUpdate = this.currentChild;
91 }
92 else {
93 if (this.pendingChild) {
94 this.pendingChild.destroy();
95 }
96 this.pendingChild = newChild;
97 childToUpdate = this.pendingChild;
98 }
99 }
100 else {
101 if (this.pendingChild === null) {
102 childToUpdate = this.currentChild;
103 }
104 else {
105 childToUpdate = this.pendingChild;
106 }
107 }
108 childToUpdate.updateAddressList(addressList, lbConfig, attributes);
109 }
110 exitIdle() {
111 if (this.currentChild) {
112 this.currentChild.exitIdle();
113 if (this.pendingChild) {
114 this.pendingChild.exitIdle();
115 }
116 }
117 }
118 resetBackoff() {
119 if (this.currentChild) {
120 this.currentChild.resetBackoff();
121 if (this.pendingChild) {
122 this.pendingChild.resetBackoff();
123 }
124 }
125 }
126 destroy() {
127 if (this.currentChild) {
128 this.currentChild.destroy();
129 this.currentChild = null;
130 }
131 if (this.pendingChild) {
132 this.pendingChild.destroy();
133 this.pendingChild = null;
134 }
135 }
136 getTypeName() {
137 return TYPE_NAME;
138 }
139}
140exports.ChildLoadBalancerHandler = ChildLoadBalancerHandler;
141//# sourceMappingURL=load-balancer-child-handler.js.map
\No newline at end of file