grpc-js: pick_first: Properly dispose of current pick when it disconnects

This commit is contained in:
Michael Lumish 2023-07-24 16:00:13 -07:00
parent 698d1427c6
commit 66cd8519bd
1 changed files with 17 additions and 12 deletions

View File

@ -223,6 +223,21 @@ export class PickFirstLoadBalancer implements LoadBalancer {
this.calculateAndReportNewState(); this.calculateAndReportNewState();
} }
private removeCurrentPick() {
if (this.currentPick !== null) {
/* Unref can cause a state change, which can cause a change in the value
* 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(
currentPick.getChannelzRef()
);
}
}
private onSubchannelStateUpdate( private onSubchannelStateUpdate(
subchannel: SubchannelInterface, subchannel: SubchannelInterface,
previousState: ConnectivityState, previousState: ConnectivityState,
@ -230,7 +245,7 @@ export class PickFirstLoadBalancer implements LoadBalancer {
) { ) {
if (this.currentPick?.realSubchannelEquals(subchannel)) { if (this.currentPick?.realSubchannelEquals(subchannel)) {
if (newState !== ConnectivityState.READY) { if (newState !== ConnectivityState.READY) {
this.currentPick = null; this.removeCurrentPick();
this.calculateAndReportNewState(); this.calculateAndReportNewState();
this.channelControlHelper.requestReresolution(); this.channelControlHelper.requestReresolution();
} }
@ -415,17 +430,7 @@ export class PickFirstLoadBalancer implements LoadBalancer {
destroy() { destroy() {
this.resetSubchannelList(); this.resetSubchannelList();
if (this.currentPick !== null) { this.removeCurrentPick();
/* Unref can cause a state change, which can cause a change in the value
* of this.currentPick, so we hold a local reference to make sure that
* does not impact this function. */
const currentPick = this.currentPick;
currentPick.unref();
currentPick.removeConnectivityStateListener(this.subchannelStateListener);
this.channelControlHelper.removeChannelzChild(
currentPick.getChannelzRef()
);
}
} }
getTypeName(): string { getTypeName(): string {