From cd14345cb429432e341a068c8bc3f940ccc6672c Mon Sep 17 00:00:00 2001 From: Michael Lumish Date: Thu, 11 Feb 2021 09:55:24 -0800 Subject: [PATCH] grpc-js: Ref and unref backoff timer --- packages/grpc-js-xds/package.json | 4 ++-- packages/grpc-js-xds/src/xds-client.ts | 4 +++- packages/grpc-js/package.json | 2 +- packages/grpc-js/src/backoff-timeout.ts | 14 ++++++++++++++ packages/grpc-js/src/resolving-load-balancer.ts | 1 + packages/grpc-js/src/subchannel.ts | 2 ++ 6 files changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/grpc-js-xds/package.json b/packages/grpc-js-xds/package.json index f2b32a773..5ff82e49d 100644 --- a/packages/grpc-js-xds/package.json +++ b/packages/grpc-js-xds/package.json @@ -1,6 +1,6 @@ { "name": "@grpc/grpc-js-xds", - "version": "1.2.1", + "version": "1.2.2", "description": "Plugin for @grpc/grpc-js. Adds the xds:// URL scheme and associated features.", "main": "build/src/index.js", "scripts": { @@ -45,7 +45,7 @@ "@grpc/proto-loader": "^0.6.0-pre14" }, "peerDependencies": { - "@grpc/grpc-js": "~1.2.2" + "@grpc/grpc-js": "~1.2.7" }, "engines": { "node": ">=10.10.0" diff --git a/packages/grpc-js-xds/src/xds-client.ts b/packages/grpc-js-xds/src/xds-client.ts index ae67960d7..7f6586573 100644 --- a/packages/grpc-js-xds/src/xds-client.ts +++ b/packages/grpc-js-xds/src/xds-client.ts @@ -774,9 +774,11 @@ export class XdsClient { this.adsBackoff = new BackoffTimeout(() => { this.maybeStartAdsStream(); }); + this.adsBackoff.unref(); this.lrsBackoff = new BackoffTimeout(() => { this.maybeStartLrsStream(); - }) + }); + this.lrsBackoff.unref(); Promise.all([loadBootstrapInfo(), loadAdsProtos()]).then( ([bootstrapInfo, protoDefinitions]) => { diff --git a/packages/grpc-js/package.json b/packages/grpc-js/package.json index 7e7f45877..6b84fd9e2 100644 --- a/packages/grpc-js/package.json +++ b/packages/grpc-js/package.json @@ -1,6 +1,6 @@ { "name": "@grpc/grpc-js", - "version": "1.2.6", + "version": "1.2.7", "description": "gRPC Library for Node - pure JS implementation", "homepage": "https://grpc.io/", "repository": "https://github.com/grpc/grpc-node/tree/master/packages/grpc-js", diff --git a/packages/grpc-js/src/backoff-timeout.ts b/packages/grpc-js/src/backoff-timeout.ts index 8cad14f74..49a5b1e29 100644 --- a/packages/grpc-js/src/backoff-timeout.ts +++ b/packages/grpc-js/src/backoff-timeout.ts @@ -44,6 +44,7 @@ export class BackoffTimeout { private nextDelay: number; private timerId: NodeJS.Timer; private running = false; + private hasRef = true; constructor(private callback: () => void, options?: BackoffOptions) { if (options) { @@ -74,6 +75,9 @@ export class BackoffTimeout { this.callback(); this.running = false; }, this.nextDelay); + if (!this.hasRef) { + this.timerId.unref(); + } const nextBackoff = Math.min( this.nextDelay * this.multiplier, this.maxDelay @@ -102,4 +106,14 @@ export class BackoffTimeout { isRunning() { return this.running; } + + ref() { + this.hasRef = true; + this.timerId.ref(); + } + + unref() { + this.hasRef = false; + this.timerId.unref(); + } } diff --git a/packages/grpc-js/src/resolving-load-balancer.ts b/packages/grpc-js/src/resolving-load-balancer.ts index 5a4c62f57..ca6d07213 100644 --- a/packages/grpc-js/src/resolving-load-balancer.ts +++ b/packages/grpc-js/src/resolving-load-balancer.ts @@ -196,6 +196,7 @@ export class ResolvingLoadBalancer implements LoadBalancer { this.updateState(this.latestChildState, this.latestChildPicker); } }); + this.backoffTimeout.unref(); } private updateResolution() { diff --git a/packages/grpc-js/src/subchannel.ts b/packages/grpc-js/src/subchannel.ts index c32ee43ea..30959122d 100644 --- a/packages/grpc-js/src/subchannel.ts +++ b/packages/grpc-js/src/subchannel.ts @@ -615,6 +615,7 @@ export class Subchannel { if (this.session) { this.session.ref(); } + this.backoffTimeout.ref(); if (!this.keepaliveWithoutCalls) { this.startKeepalivePings(); } @@ -635,6 +636,7 @@ export class Subchannel { if (this.session) { this.session.unref(); } + this.backoffTimeout.unref(); if (!this.keepaliveWithoutCalls) { this.stopKeepalivePings(); }