mirror of https://github.com/grpc/grpc-node.git
grpc-js: Simplify pick_first behavior
This commit is contained in:
parent
f5ea6ce271
commit
a6575c3e73
|
@ -320,21 +320,16 @@ export class PickFirstLoadBalancer implements LoadBalancer {
|
||||||
|
|
||||||
private removeCurrentPick() {
|
private removeCurrentPick() {
|
||||||
if (this.currentPick !== null) {
|
if (this.currentPick !== null) {
|
||||||
/* Unref can cause a state change, which can cause a change in the value
|
this.currentPick.removeConnectivityStateListener(this.subchannelStateListener);
|
||||||
* of this.currentPick, so we hold a local reference to make sure that
|
|
||||||
* does not impact this function. */
|
|
||||||
const currentPick = this.currentPick;
|
|
||||||
this.currentPick = null;
|
|
||||||
currentPick.unref();
|
|
||||||
currentPick.removeConnectivityStateListener(this.subchannelStateListener);
|
|
||||||
this.channelControlHelper.removeChannelzChild(
|
this.channelControlHelper.removeChannelzChild(
|
||||||
currentPick.getChannelzRef()
|
this.currentPick.getChannelzRef()
|
||||||
);
|
);
|
||||||
if (this.reportHealthStatus) {
|
this.currentPick.removeHealthStateWatcher(
|
||||||
currentPick.removeHealthStateWatcher(
|
this.pickedSubchannelHealthListener
|
||||||
this.pickedSubchannelHealthListener
|
);
|
||||||
);
|
// Unref last, to avoid triggering listeners
|
||||||
}
|
this.currentPick.unref();
|
||||||
|
this.currentPick = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -418,20 +413,25 @@ export class PickFirstLoadBalancer implements LoadBalancer {
|
||||||
this.connectionDelayTimeout.unref?.();
|
this.connectionDelayTimeout.unref?.();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Declare that the specified subchannel should be used to make requests.
|
||||||
|
* This functions the same independent of whether subchannel is a member of
|
||||||
|
* this.children and whether it is equal to this.currentPick.
|
||||||
|
* Prerequisite: subchannel.getConnectivityState() === READY.
|
||||||
|
* @param subchannel
|
||||||
|
*/
|
||||||
private pickSubchannel(subchannel: SubchannelInterface) {
|
private pickSubchannel(subchannel: SubchannelInterface) {
|
||||||
if (this.currentPick && subchannel.realSubchannelEquals(this.currentPick)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
trace('Pick subchannel with address ' + subchannel.getAddress());
|
trace('Pick subchannel with address ' + subchannel.getAddress());
|
||||||
this.stickyTransientFailureMode = false;
|
this.stickyTransientFailureMode = false;
|
||||||
this.removeCurrentPick();
|
/* Ref before removeCurrentPick and resetSubchannelList to avoid the
|
||||||
this.currentPick = subchannel;
|
* refcount dropping to 0 during this process. */
|
||||||
subchannel.ref();
|
subchannel.ref();
|
||||||
if (this.reportHealthStatus) {
|
|
||||||
subchannel.addHealthStateWatcher(this.pickedSubchannelHealthListener);
|
|
||||||
}
|
|
||||||
this.channelControlHelper.addChannelzChild(subchannel.getChannelzRef());
|
this.channelControlHelper.addChannelzChild(subchannel.getChannelzRef());
|
||||||
|
this.removeCurrentPick();
|
||||||
this.resetSubchannelList();
|
this.resetSubchannelList();
|
||||||
|
subchannel.addConnectivityStateListener(this.subchannelStateListener);
|
||||||
|
subchannel.addHealthStateWatcher(this.pickedSubchannelHealthListener);
|
||||||
|
this.currentPick = subchannel;
|
||||||
clearTimeout(this.connectionDelayTimeout);
|
clearTimeout(this.connectionDelayTimeout);
|
||||||
this.calculateAndReportNewState();
|
this.calculateAndReportNewState();
|
||||||
}
|
}
|
||||||
|
@ -448,20 +448,11 @@ export class PickFirstLoadBalancer implements LoadBalancer {
|
||||||
|
|
||||||
private resetSubchannelList() {
|
private resetSubchannelList() {
|
||||||
for (const child of this.children) {
|
for (const child of this.children) {
|
||||||
if (
|
/* Always remoev the connectivity state listener. If the subchannel is
|
||||||
!(
|
getting picked, it will be re-added then. */
|
||||||
this.currentPick &&
|
child.subchannel.removeConnectivityStateListener(
|
||||||
child.subchannel.realSubchannelEquals(this.currentPick)
|
this.subchannelStateListener
|
||||||
)
|
);
|
||||||
) {
|
|
||||||
/* The connectivity state listener is the same whether the subchannel
|
|
||||||
* is in the list of children or it is the currentPick, so if it is in
|
|
||||||
* both, removing it here would cause problems. In particular, that
|
|
||||||
* always happens immediately after the subchannel is picked. */
|
|
||||||
child.subchannel.removeConnectivityStateListener(
|
|
||||||
this.subchannelStateListener
|
|
||||||
);
|
|
||||||
}
|
|
||||||
/* Refs are counted independently for the children list and the
|
/* Refs are counted independently for the children list and the
|
||||||
* currentPick, so we call unref whether or not the child is the
|
* currentPick, so we call unref whether or not the child is the
|
||||||
* currentPick. Channelz child references are also refcounted, so
|
* currentPick. Channelz child references are also refcounted, so
|
||||||
|
@ -478,15 +469,13 @@ export class PickFirstLoadBalancer implements LoadBalancer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private connectToAddressList(addressList: SubchannelAddress[]) {
|
private connectToAddressList(addressList: SubchannelAddress[]) {
|
||||||
|
trace('connectToAddressList([' + addressList.map(address => subchannelAddressToString(address)) + '])');
|
||||||
const newChildrenList = addressList.map(address => ({
|
const newChildrenList = addressList.map(address => ({
|
||||||
subchannel: this.channelControlHelper.createSubchannel(address, {}),
|
subchannel: this.channelControlHelper.createSubchannel(address, {}),
|
||||||
hasReportedTransientFailure: false,
|
hasReportedTransientFailure: false,
|
||||||
}));
|
}));
|
||||||
trace('connectToAddressList([' + addressList.map(address => subchannelAddressToString(address)) + '])');
|
|
||||||
for (const { subchannel } of newChildrenList) {
|
for (const { subchannel } of newChildrenList) {
|
||||||
if (subchannel.getConnectivityState() === ConnectivityState.READY) {
|
if (subchannel.getConnectivityState() === ConnectivityState.READY) {
|
||||||
this.channelControlHelper.addChannelzChild(subchannel.getChannelzRef());
|
|
||||||
subchannel.addConnectivityStateListener(this.subchannelStateListener);
|
|
||||||
this.pickSubchannel(subchannel);
|
this.pickSubchannel(subchannel);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -522,6 +511,7 @@ export class PickFirstLoadBalancer implements LoadBalancer {
|
||||||
if (!(lbConfig instanceof PickFirstLoadBalancingConfig)) {
|
if (!(lbConfig instanceof PickFirstLoadBalancingConfig)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
this.requestedResolutionSinceLastUpdate = false;
|
||||||
/* Previously, an update would be discarded if it was identical to the
|
/* Previously, an update would be discarded if it was identical to the
|
||||||
* previous update, to minimize churn. Now the DNS resolver is
|
* previous update, to minimize churn. Now the DNS resolver is
|
||||||
* rate-limited, so that is less of a concern. */
|
* rate-limited, so that is less of a concern. */
|
||||||
|
|
Loading…
Reference in New Issue