Skip to content

Commit 99fb887

Browse files
authored
Merge pull request #2069 from murgatroid99/grpc-js_server_channelz_enabled
grpc-js: Improve coverage of channelzEnabled checks in server code
2 parents ec334f2 + 680ff7c commit 99fb887

File tree

2 files changed

+95
-54
lines changed

2 files changed

+95
-54
lines changed

packages/grpc-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@grpc/grpc-js",
3-
"version": "1.5.9",
3+
"version": "1.5.10",
44
"description": "gRPC Library for Node - pure JS implementation",
55
"homepage": "https://grpc.io/",
66
"repository": "https://github.com/grpc/grpc-node/tree/master/packages/grpc-js",

packages/grpc-js/src/server.ts

Lines changed: 94 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -430,27 +430,36 @@ export class Server {
430430
port: boundAddress.port
431431
}
432432
}
433-
const channelzRef = registerChannelzSocket(subchannelAddressToString(boundSubchannelAddress), () => {
434-
return {
435-
localAddress: boundSubchannelAddress,
436-
remoteAddress: null,
437-
security: null,
438-
remoteName: null,
439-
streamsStarted: 0,
440-
streamsSucceeded: 0,
441-
streamsFailed: 0,
442-
messagesSent: 0,
443-
messagesReceived: 0,
444-
keepAlivesSent: 0,
445-
lastLocalStreamCreatedTimestamp: null,
446-
lastRemoteStreamCreatedTimestamp: null,
447-
lastMessageSentTimestamp: null,
448-
lastMessageReceivedTimestamp: null,
449-
localFlowControlWindow: null,
450-
remoteFlowControlWindow: null
433+
let channelzRef: SocketRef;
434+
if (this.channelzEnabled) {
435+
channelzRef = registerChannelzSocket(subchannelAddressToString(boundSubchannelAddress), () => {
436+
return {
437+
localAddress: boundSubchannelAddress,
438+
remoteAddress: null,
439+
security: null,
440+
remoteName: null,
441+
streamsStarted: 0,
442+
streamsSucceeded: 0,
443+
streamsFailed: 0,
444+
messagesSent: 0,
445+
messagesReceived: 0,
446+
keepAlivesSent: 0,
447+
lastLocalStreamCreatedTimestamp: null,
448+
lastRemoteStreamCreatedTimestamp: null,
449+
lastMessageSentTimestamp: null,
450+
lastMessageReceivedTimestamp: null,
451+
localFlowControlWindow: null,
452+
remoteFlowControlWindow: null
453+
};
454+
});
455+
this.listenerChildrenTracker.refChild(channelzRef);
456+
} else {
457+
channelzRef = {
458+
kind: 'socket',
459+
id: -1,
460+
name: ''
451461
};
452-
});
453-
this.listenerChildrenTracker.refChild(channelzRef);
462+
}
454463
this.http2ServerList.push({server: http2Server, channelzRef: channelzRef});
455464
this.trace('Successfully bound ' + subchannelAddressToString(boundSubchannelAddress));
456465
resolve('port' in boundSubchannelAddress ? boundSubchannelAddress.port : portNum);
@@ -499,27 +508,36 @@ export class Server {
499508
host: boundAddress.address,
500509
port: boundAddress.port
501510
};
502-
const channelzRef = registerChannelzSocket(subchannelAddressToString(boundSubchannelAddress), () => {
503-
return {
504-
localAddress: boundSubchannelAddress,
505-
remoteAddress: null,
506-
security: null,
507-
remoteName: null,
508-
streamsStarted: 0,
509-
streamsSucceeded: 0,
510-
streamsFailed: 0,
511-
messagesSent: 0,
512-
messagesReceived: 0,
513-
keepAlivesSent: 0,
514-
lastLocalStreamCreatedTimestamp: null,
515-
lastRemoteStreamCreatedTimestamp: null,
516-
lastMessageSentTimestamp: null,
517-
lastMessageReceivedTimestamp: null,
518-
localFlowControlWindow: null,
519-
remoteFlowControlWindow: null
511+
let channelzRef: SocketRef;
512+
if (this.channelzEnabled) {
513+
channelzRef = registerChannelzSocket(subchannelAddressToString(boundSubchannelAddress), () => {
514+
return {
515+
localAddress: boundSubchannelAddress,
516+
remoteAddress: null,
517+
security: null,
518+
remoteName: null,
519+
streamsStarted: 0,
520+
streamsSucceeded: 0,
521+
streamsFailed: 0,
522+
messagesSent: 0,
523+
messagesReceived: 0,
524+
keepAlivesSent: 0,
525+
lastLocalStreamCreatedTimestamp: null,
526+
lastRemoteStreamCreatedTimestamp: null,
527+
lastMessageSentTimestamp: null,
528+
lastMessageReceivedTimestamp: null,
529+
localFlowControlWindow: null,
530+
remoteFlowControlWindow: null
531+
};
532+
});
533+
this.listenerChildrenTracker.refChild(channelzRef);
534+
} else {
535+
channelzRef = {
536+
kind: 'socket',
537+
id: -1,
538+
name: ''
520539
};
521-
});
522-
this.listenerChildrenTracker.refChild(channelzRef);
540+
}
523541
this.http2ServerList.push({server: http2Server, channelzRef: channelzRef});
524542
this.trace('Successfully bound ' + subchannelAddressToString(boundSubchannelAddress));
525543
resolve(
@@ -599,8 +617,10 @@ export class Server {
599617
for (const {server: http2Server, channelzRef: ref} of this.http2ServerList) {
600618
if (http2Server.listening) {
601619
http2Server.close(() => {
602-
this.listenerChildrenTracker.unrefChild(ref);
603-
unregisterChannelzRef(ref);
620+
if (this.channelzEnabled) {
621+
this.listenerChildrenTracker.unrefChild(ref);
622+
unregisterChannelzRef(ref);
623+
}
604624
});
605625
}
606626
}
@@ -616,7 +636,9 @@ export class Server {
616636
session.destroy(http2.constants.NGHTTP2_CANCEL as any);
617637
});
618638
this.sessions.clear();
619-
unregisterChannelzRef(this.channelzRef);
639+
if (this.channelzEnabled) {
640+
unregisterChannelzRef(this.channelzRef);
641+
}
620642
}
621643

622644
register<RequestType, ResponseType>(
@@ -665,7 +687,9 @@ export class Server {
665687

666688
tryShutdown(callback: (error?: Error) => void): void {
667689
const wrappedCallback = (error?: Error) => {
668-
unregisterChannelzRef(this.channelzRef);
690+
if (this.channelzEnabled) {
691+
unregisterChannelzRef(this.channelzRef);
692+
}
669693
callback(error);
670694
};
671695
let pendingChecks = 0;
@@ -685,8 +709,10 @@ export class Server {
685709
if (http2Server.listening) {
686710
pendingChecks++;
687711
http2Server.close(() => {
688-
this.listenerChildrenTracker.unrefChild(ref);
689-
unregisterChannelzRef(ref);
712+
if (this.channelzEnabled) {
713+
this.listenerChildrenTracker.unrefChild(ref);
714+
unregisterChannelzRef(ref);
715+
}
690716
maybeCallback();
691717
});
692718
}
@@ -727,8 +753,10 @@ export class Server {
727753
'stream',
728754
(stream: http2.ServerHttp2Stream, headers: http2.IncomingHttpHeaders) => {
729755
const channelzSessionInfo = this.sessions.get(stream.session as http2.ServerHttp2Session);
730-
this.callTracker.addCallStarted();
731-
channelzSessionInfo?.streamTracker.addCallStarted();
756+
if (this.channelzEnabled) {
757+
this.callTracker.addCallStarted();
758+
channelzSessionInfo?.streamTracker.addCallStarted();
759+
}
732760
const contentType = headers[http2.constants.HTTP2_HEADER_CONTENT_TYPE];
733761

734762
if (
@@ -743,7 +771,9 @@ export class Server {
743771
{ endStream: true }
744772
);
745773
this.callTracker.addCallFailed();
746-
channelzSessionInfo?.streamTracker.addCallFailed();
774+
if (this.channelzEnabled) {
775+
channelzSessionInfo?.streamTracker.addCallFailed();
776+
}
747777
return;
748778
}
749779

@@ -786,7 +816,7 @@ export class Server {
786816
this.callTracker.addCallFailed();
787817
}
788818
});
789-
if (channelzSessionInfo) {
819+
if (this.channelzEnabled && channelzSessionInfo) {
790820
call.once('streamEnd', (success: boolean) => {
791821
if (success) {
792822
channelzSessionInfo.streamTracker.addCallSucceeded();
@@ -841,8 +871,10 @@ export class Server {
841871
} catch (err) {
842872
if (!call) {
843873
call = new Http2ServerCallStream(stream, null!, this.options);
844-
this.callTracker.addCallFailed();
845-
channelzSessionInfo?.streamTracker.addCallFailed()
874+
if (this.channelzEnabled) {
875+
this.callTracker.addCallFailed();
876+
channelzSessionInfo?.streamTracker.addCallFailed()
877+
}
846878
}
847879

848880
if (err.code === undefined) {
@@ -860,7 +892,16 @@ export class Server {
860892
return;
861893
}
862894

863-
const channelzRef = registerChannelzSocket(session.socket.remoteAddress ?? 'unknown', this.getChannelzSessionInfoGetter(session));
895+
let channelzRef: SocketRef;
896+
if (this.channelzEnabled) {
897+
channelzRef = registerChannelzSocket(session.socket.remoteAddress ?? 'unknown', this.getChannelzSessionInfoGetter(session));
898+
} else {
899+
channelzRef = {
900+
kind: 'socket',
901+
id: -1,
902+
name: ''
903+
}
904+
}
864905

865906
const channelzSessionInfo: ChannelzSessionInfo = {
866907
ref: channelzRef,

0 commit comments

Comments
 (0)