Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serverside keepalive error detection and cleanups #2756

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 60 additions & 9 deletions packages/grpc-js/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1407,8 +1407,9 @@ export class Server {
}
}, this.maxConnectionAgeMs + jitter).unref?.();
}
const keeapliveTimeTimer: NodeJS.Timeout | null = setInterval(() => {
const timeoutTImer = setTimeout(() => {
let keepaliveInterval: NodeJS.Timeout | null = null;
keepaliveInterval = setInterval(() => {
const keepaliveTimeout = setTimeout(() => {
sessionClosedByServer = true;
if (this.channelzEnabled) {
this.channelzTrace.addTrace(
Expand All @@ -1419,16 +1420,65 @@ export class Server {
session.close();
}, this.keepaliveTimeoutMs).unref?.();
try {
session.ping(
(err: Error | null, duration: number, payload: Buffer) => {
clearTimeout(timeoutTImer);
}
);
if (
!session.ping(
(err: Error | null, duration: number, payload: Buffer) => {
clearTimeout(keepaliveTimeout);
if (err) {
if (keepaliveInterval) {
if (this.channelzEnabled) {
this.channelzTrace.addTrace(
'CT_INFO',
'Connection dropped by keepalive failure from ' +
clientAddress
);
}
clearInterval(keepaliveInterval);
keepaliveInterval = null;
}
session.destroy();
}
}
)
) {
throw new Error('Server keepalive ping send failed');
}
} catch (e) {
// The ping can't be sent because the session is already closed
clearTimeout(keepaliveTimeout);
if (keepaliveInterval) {
if (this.channelzEnabled) {
this.channelzTrace.addTrace(
'CT_INFO',
'Connection dropped by keepalive error from ' + clientAddress
);
}
clearInterval(keepaliveInterval);
keepaliveInterval = null;
}
session.destroy();
}
}, this.keepaliveTimeMs).unref?.();
session.once('goaway', (errorCode, opaqueData) => {
if (keepaliveInterval) {
if (this.channelzEnabled) {
if (errorCode === http2.constants.NGHTTP2_ENHANCE_YOUR_CALM) {
this.channelzTrace.addTrace(
'CT_INFO',
`Client sent GOAWAY for too many pings ${clientAddress}`
);
} else {
this.channelzTrace.addTrace(
'CT_INFO',
`Client sent GOAWAY ${clientAddress} with error code ${errorCode} and opaque data ${opaqueData.toString()}`
);
}
}
clearInterval(keepaliveInterval);
keepaliveInterval = null;
}
session.destroy();
});
session.on('close', () => {
if (this.channelzEnabled) {
if (!sessionClosedByServer) {
Expand All @@ -1446,8 +1496,9 @@ export class Server {
if (connectionAgeGraceTimer) {
clearTimeout(connectionAgeGraceTimer);
}
if (keeapliveTimeTimer) {
clearTimeout(keeapliveTimeTimer);
if (keepaliveInterval) {
clearInterval(keepaliveInterval);
keepaliveInterval = null;
}
this.http2Servers.get(http2Server)?.sessions.delete(session);
this.sessions.delete(session);
Expand Down
Loading