Skip to content

Commit

Permalink
remove keepaliveDisabled from server.ts. rename keepaliveTimer.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfiala committed May 29, 2024
1 parent a77d94f commit c2da436
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
24 changes: 10 additions & 14 deletions packages/grpc-js/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1384,8 +1384,7 @@ export class Server {

let connectionAgeTimer: NodeJS.Timeout | null = null;
let connectionAgeGraceTimer: NodeJS.Timeout | null = null;
let keepaliveTimeout: NodeJS.Timeout | null = null;
let keepaliveDisabled = false;
let keepaliveTimer: NodeJS.Timeout | null = null;
let sessionClosedByServer = false;

const idleTimeoutObj = this.enableIdleTimeout(session);
Expand Down Expand Up @@ -1429,15 +1428,15 @@ export class Server {
}

const clearKeepaliveTimeout = () => {
if (keepaliveTimeout) {
clearTimeout(keepaliveTimeout);
keepaliveTimeout = null;
if (keepaliveTimer) {
clearTimeout(keepaliveTimer);
keepaliveTimer = null;
}
};

const canSendPing = () => {
return (
!keepaliveDisabled &&
!session.destroyed &&
this.keepaliveTimeMs < KEEPALIVE_MAX_TIME_MS &&
this.keepaliveTimeMs > 0
);
Expand All @@ -1453,11 +1452,11 @@ export class Server {
this.keepaliveTrace(
'Starting keepalive timer for ' + this.keepaliveTimeMs + 'ms'
);
keepaliveTimeout = setTimeout(() => {
keepaliveTimer = setTimeout(() => {
clearKeepaliveTimeout();
sendPing();
}, this.keepaliveTimeMs);
keepaliveTimeout.unref?.();
keepaliveTimer.unref?.();
};

sendPing = () => {
Expand Down Expand Up @@ -1501,14 +1500,14 @@ export class Server {
return;
}

keepaliveTimeout = setTimeout(() => {
keepaliveTimer = setTimeout(() => {
clearKeepaliveTimeout();
this.keepaliveTrace('Ping timeout passed without response');
this.trace('Connection dropped by keepalive timeout');
sessionClosedByServer = true;
session.close();
}, this.keepaliveTimeoutMs);
keepaliveTimeout.unref?.();
keepaliveTimer.unref?.();
};

maybeStartKeepalivePingTimer();
Expand All @@ -1528,7 +1527,6 @@ export class Server {
clearTimeout(connectionAgeGraceTimer);
}

keepaliveDisabled = true;
clearKeepaliveTimeout();

if (idleTimeoutObj !== null) {
Expand Down Expand Up @@ -1575,7 +1573,6 @@ export class Server {
let connectionAgeTimer: NodeJS.Timeout | null = null;
let connectionAgeGraceTimer: NodeJS.Timeout | null = null;
let keepaliveTimeout: NodeJS.Timeout | null = null;
let keepaliveDisabled = false;
let sessionClosedByServer = false;

const idleTimeoutObj = this.enableIdleTimeout(session);
Expand Down Expand Up @@ -1626,7 +1623,7 @@ export class Server {

const canSendPing = () => {
return (
!keepaliveDisabled &&
!session.destroyed &&
this.keepaliveTimeMs < KEEPALIVE_MAX_TIME_MS &&
this.keepaliveTimeMs > 0
);
Expand Down Expand Up @@ -1734,7 +1731,6 @@ export class Server {
clearTimeout(connectionAgeGraceTimer);
}

keepaliveDisabled = true;
clearKeepaliveTimeout();

if (idleTimeoutObj !== null) {
Expand Down
20 changes: 10 additions & 10 deletions packages/grpc-js/src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Http2Transport implements Transport {
/**
* Timer reference indicating when to send the next ping or when the most recent ping will be considered lost.
*/
private keepaliveTimeout: NodeJS.Timeout | null = null;
private keepaliveTimer: NodeJS.Timeout | null = null;
/**
* Indicates that the keepalive timer ran out while there were no active
* calls, and a ping should be sent the next time a call starts.
Expand Down Expand Up @@ -416,7 +416,7 @@ class Http2Transport implements Transport {
this.pendingSendKeepalivePing = true;
return;
}
if (this.keepaliveTimeout) {
if (this.keepaliveTimer) {
console.error('keepaliveTimeout is not null');
return;
}
Expand All @@ -426,11 +426,11 @@ class Http2Transport implements Transport {
this.keepaliveTrace(
'Sending ping with timeout ' + this.keepaliveTimeoutMs + 'ms'
);
this.keepaliveTimeout = setTimeout(() => {
this.keepaliveTimer = setTimeout(() => {
this.keepaliveTrace('Ping timeout passed without response');
this.handleDisconnect();
}, this.keepaliveTimeoutMs);
this.keepaliveTimeout.unref?.();
this.keepaliveTimer.unref?.();
let pingSendError = '';
try {
const pingSentSuccessfully = this.session.ping(
Expand Down Expand Up @@ -471,14 +471,14 @@ class Http2Transport implements Transport {
if (this.pendingSendKeepalivePing) {
this.pendingSendKeepalivePing = false;
this.maybeSendPing();
} else if (!this.keepaliveTimeout) {
} else if (!this.keepaliveTimer) {
this.keepaliveTrace(
'Starting keepalive timer for ' + this.keepaliveTimeMs + 'ms'
);
this.keepaliveTimeout = setTimeout(() => {
this.keepaliveTimer = setTimeout(() => {
this.maybeSendPing();
}, this.keepaliveTimeMs);
this.keepaliveTimeout.unref?.();
this.keepaliveTimer.unref?.();
}
/* Otherwise, there is already either a keepalive timer or a ping pending,
* wait for those to resolve. */
Expand All @@ -488,9 +488,9 @@ class Http2Transport implements Transport {
* Clears whichever keepalive timeout is currently active, if any.
*/
private clearKeepaliveTimeout() {
if (this.keepaliveTimeout) {
clearTimeout(this.keepaliveTimeout);
this.keepaliveTimeout = null;
if (this.keepaliveTimer) {
clearTimeout(this.keepaliveTimer);
this.keepaliveTimer = null;
}
}

Expand Down

0 comments on commit c2da436

Please sign in to comment.