Skip to content

Commit

Permalink
Merge pull request #1688 from murgatroid99/grpc-js_backoff_ref
Browse files Browse the repository at this point in the history
grpc-js: Ref and unref backoff timer
  • Loading branch information
murgatroid99 committed Feb 12, 2021
2 parents 7436983 + cd14345 commit 24d1a04
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions 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": {
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion packages/grpc-js-xds/src/xds-client.ts
Expand Up @@ -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]) => {
Expand Down
2 changes: 1 addition & 1 deletion 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",
Expand Down
14 changes: 14 additions & 0 deletions packages/grpc-js/src/backoff-timeout.ts
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
1 change: 1 addition & 0 deletions packages/grpc-js/src/resolving-load-balancer.ts
Expand Up @@ -196,6 +196,7 @@ export class ResolvingLoadBalancer implements LoadBalancer {
this.updateState(this.latestChildState, this.latestChildPicker);
}
});
this.backoffTimeout.unref();
}

private updateResolution() {
Expand Down
2 changes: 2 additions & 0 deletions packages/grpc-js/src/subchannel.ts
Expand Up @@ -615,6 +615,7 @@ export class Subchannel {
if (this.session) {
this.session.ref();
}
this.backoffTimeout.ref();
if (!this.keepaliveWithoutCalls) {
this.startKeepalivePings();
}
Expand All @@ -635,6 +636,7 @@ export class Subchannel {
if (this.session) {
this.session.unref();
}
this.backoffTimeout.unref();
if (!this.keepaliveWithoutCalls) {
this.stopKeepalivePings();
}
Expand Down

0 comments on commit 24d1a04

Please sign in to comment.