From 1e28a043308bcfa446cc43825338f032d5f434db Mon Sep 17 00:00:00 2001 From: Michael Lumish Date: Mon, 24 Feb 2025 15:12:31 -0800 Subject: [PATCH] Register xds listener with channelz --- packages/grpc-js-xds/src/server.ts | 3 ++- packages/grpc-js/src/server.ts | 32 +++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/packages/grpc-js-xds/src/server.ts b/packages/grpc-js-xds/src/server.ts index ca78df3a..9bbf897c 100644 --- a/packages/grpc-js-xds/src/server.ts +++ b/packages/grpc-js-xds/src/server.ts @@ -628,8 +628,9 @@ export class XdsServer extends Server { if (!hostPort || !isValidIpPort(hostPort)) { throw new Error(`Listening port string must have the format IP:port with non-zero port, got ${port}`); } + const channelzRef = this.experimentalRegisterListenerToChannelz({host: hostPort.host, port: hostPort.port!}); const configParameters: ConfigParameters = { - createConnectionInjector: (credentials) => this.createConnectionInjector(credentials), + createConnectionInjector: (credentials) => this.experimentalCreateConnectionInjectorWithChannelzRef(credentials, channelzRef), drainGraceTimeMs: this.drainGraceTimeMs, listenerResourceNameTemplate: this.listenerResourceNameTemplate, xdsClient: this.xdsClient diff --git a/packages/grpc-js/src/server.ts b/packages/grpc-js/src/server.ts index adefec01..a73c9669 100644 --- a/packages/grpc-js/src/server.ts +++ b/packages/grpc-js/src/server.ts @@ -539,7 +539,12 @@ export class Server { throw new Error('Not implemented. Use bindAsync() instead'); } - private registerListenerToChannelz(boundAddress: SubchannelAddress) { + /** + * This API is experimental, so API stability is not guaranteed across minor versions. + * @param boundAddress + * @returns + */ + protected experimentalRegisterListenerToChannelz(boundAddress: SubchannelAddress) { return registerChannelzSocket( subchannelAddressToString(boundAddress), () => { @@ -649,7 +654,7 @@ export class Server { }; } - const channelzRef = this.registerListenerToChannelz( + const channelzRef = this.experimentalRegisterListenerToChannelz( boundSubchannelAddress ); this.listenerChildrenTracker.refChild(channelzRef); @@ -945,15 +950,17 @@ export class Server { ); } - createConnectionInjector(credentials: ServerCredentials): ConnectionInjector { + /** + * This API is experimental, so API stability is not guaranteed across minor versions. + * @param credentials + * @param channelzRef + * @returns + */ + protected experimentalCreateConnectionInjectorWithChannelzRef(credentials: ServerCredentials, channelzRef: SocketRef) { if (credentials === null || !(credentials instanceof ServerCredentials)) { throw new TypeError('creds must be a ServerCredentials object'); } const server = this.createHttp2Server(credentials); - const channelzRef = this.registerInjectorToChannelz(); - if (this.channelzEnabled) { - this.listenerChildrenTracker.refChild(channelzRef); - } const sessionsSet: Set = new Set(); this.http2Servers.set(server, { channelzRef: channelzRef, @@ -982,6 +989,17 @@ export class Server { }; } + createConnectionInjector(credentials: ServerCredentials): ConnectionInjector { + if (credentials === null || !(credentials instanceof ServerCredentials)) { + throw new TypeError('creds must be a ServerCredentials object'); + } + const channelzRef = this.registerInjectorToChannelz(); + if (this.channelzEnabled) { + this.listenerChildrenTracker.refChild(channelzRef); + } + return this.experimentalCreateConnectionInjectorWithChannelzRef(credentials, channelzRef); + } + private closeServer(server: AnyHttp2Server, callback?: () => void) { this.trace( 'Closing server with address ' + JSON.stringify(server.address())