From bb26dcfd1ef1019af887e4a76c72262bc26a5378 Mon Sep 17 00:00:00 2001 From: Michael Lumish Date: Fri, 5 Nov 2021 10:11:22 -0700 Subject: [PATCH] grpc-js: Fix handling of grpc.enable_channelz option --- packages/grpc-js/package.json | 2 +- packages/grpc-js/src/subchannel.ts | 11 +++++++-- packages/grpc-js/test/test-channelz.ts | 32 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/packages/grpc-js/package.json b/packages/grpc-js/package.json index 4acf948df..69d0662b7 100644 --- a/packages/grpc-js/package.json +++ b/packages/grpc-js/package.json @@ -1,6 +1,6 @@ { "name": "@grpc/grpc-js", - "version": "1.4.2", + "version": "1.4.3", "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/subchannel.ts b/packages/grpc-js/src/subchannel.ts index 0b6465b2e..87defa740 100644 --- a/packages/grpc-js/src/subchannel.ts +++ b/packages/grpc-js/src/subchannel.ts @@ -832,6 +832,7 @@ export class Subchannel { headersString ); const streamSession = this.session; + let statsTracker: SubchannelCallStatsTracker; if (this.channelzEnabled) { this.callTracker.addCallStarted(); callStream.addStatusWatcher(status => { @@ -851,7 +852,7 @@ export class Subchannel { } } }); - callStream.attachHttp2Stream(http2Stream, this, extraFilters, { + statsTracker = { addMessageSent: () => { this.messagesSent += 1; this.lastMessageSentTimestamp = new Date(); @@ -859,8 +860,14 @@ export class Subchannel { addMessageReceived: () => { this.messagesReceived += 1; } - }); + } + } else { + statsTracker = { + addMessageSent: () => {}, + addMessageReceived: () => {} + } } + callStream.attachHttp2Stream(http2Stream, this, extraFilters, statsTracker); } /** diff --git a/packages/grpc-js/test/test-channelz.ts b/packages/grpc-js/test/test-channelz.ts index 2cca780e8..f14145c37 100644 --- a/packages/grpc-js/test/test-channelz.ts +++ b/packages/grpc-js/test/test-channelz.ts @@ -286,4 +286,36 @@ describe('Channelz', () => { }); }); }); +}); + +describe('Disabling channelz', () => { + let testServer: grpc.Server; + let testClient: ServiceClient; + beforeEach((done) => { + testServer = new grpc.Server({'grpc.enable_channelz': 0}); + testServer.addService(TestServiceClient.service, testServiceImpl); + testServer.bindAsync('localhost:0', grpc.ServerCredentials.createInsecure(), (error, port) => { + if (error) { + done(error); + return; + } + testServer.start(); + testClient = new TestServiceClient(`localhost:${port}`, grpc.credentials.createInsecure(), {'grpc.enable_channelz': 0}); + done(); + }); + }); + + afterEach(() => { + testClient.close(); + testServer.forceShutdown(); + }); + + it('Should still work', (done) => { + const deadline = new Date(); + deadline.setSeconds(deadline.getSeconds() + 1); + testClient.unary({}, {deadline}, (error: grpc.ServiceError, value: unknown) => { + assert.ifError(error); + done(); + }); + }); }); \ No newline at end of file