remove keepaliveDisabled from server.ts. rename keepaliveTimer.

This commit is contained in:
David Fiala 2024-05-29 15:09:55 -07:00
parent a77d94f7c6
commit c2da436a8e
2 changed files with 20 additions and 24 deletions

View File

@ -1384,8 +1384,7 @@ export class Server {
let connectionAgeTimer: NodeJS.Timeout | null = null; let connectionAgeTimer: NodeJS.Timeout | null = null;
let connectionAgeGraceTimer: NodeJS.Timeout | null = null; let connectionAgeGraceTimer: NodeJS.Timeout | null = null;
let keepaliveTimeout: NodeJS.Timeout | null = null; let keepaliveTimer: NodeJS.Timeout | null = null;
let keepaliveDisabled = false;
let sessionClosedByServer = false; let sessionClosedByServer = false;
const idleTimeoutObj = this.enableIdleTimeout(session); const idleTimeoutObj = this.enableIdleTimeout(session);
@ -1429,15 +1428,15 @@ export class Server {
} }
const clearKeepaliveTimeout = () => { const clearKeepaliveTimeout = () => {
if (keepaliveTimeout) { if (keepaliveTimer) {
clearTimeout(keepaliveTimeout); clearTimeout(keepaliveTimer);
keepaliveTimeout = null; keepaliveTimer = null;
} }
}; };
const canSendPing = () => { const canSendPing = () => {
return ( return (
!keepaliveDisabled && !session.destroyed &&
this.keepaliveTimeMs < KEEPALIVE_MAX_TIME_MS && this.keepaliveTimeMs < KEEPALIVE_MAX_TIME_MS &&
this.keepaliveTimeMs > 0 this.keepaliveTimeMs > 0
); );
@ -1453,11 +1452,11 @@ export class Server {
this.keepaliveTrace( this.keepaliveTrace(
'Starting keepalive timer for ' + this.keepaliveTimeMs + 'ms' 'Starting keepalive timer for ' + this.keepaliveTimeMs + 'ms'
); );
keepaliveTimeout = setTimeout(() => { keepaliveTimer = setTimeout(() => {
clearKeepaliveTimeout(); clearKeepaliveTimeout();
sendPing(); sendPing();
}, this.keepaliveTimeMs); }, this.keepaliveTimeMs);
keepaliveTimeout.unref?.(); keepaliveTimer.unref?.();
}; };
sendPing = () => { sendPing = () => {
@ -1501,14 +1500,14 @@ export class Server {
return; return;
} }
keepaliveTimeout = setTimeout(() => { keepaliveTimer = setTimeout(() => {
clearKeepaliveTimeout(); clearKeepaliveTimeout();
this.keepaliveTrace('Ping timeout passed without response'); this.keepaliveTrace('Ping timeout passed without response');
this.trace('Connection dropped by keepalive timeout'); this.trace('Connection dropped by keepalive timeout');
sessionClosedByServer = true; sessionClosedByServer = true;
session.close(); session.close();
}, this.keepaliveTimeoutMs); }, this.keepaliveTimeoutMs);
keepaliveTimeout.unref?.(); keepaliveTimer.unref?.();
}; };
maybeStartKeepalivePingTimer(); maybeStartKeepalivePingTimer();
@ -1528,7 +1527,6 @@ export class Server {
clearTimeout(connectionAgeGraceTimer); clearTimeout(connectionAgeGraceTimer);
} }
keepaliveDisabled = true;
clearKeepaliveTimeout(); clearKeepaliveTimeout();
if (idleTimeoutObj !== null) { if (idleTimeoutObj !== null) {
@ -1575,7 +1573,6 @@ export class Server {
let connectionAgeTimer: NodeJS.Timeout | null = null; let connectionAgeTimer: NodeJS.Timeout | null = null;
let connectionAgeGraceTimer: NodeJS.Timeout | null = null; let connectionAgeGraceTimer: NodeJS.Timeout | null = null;
let keepaliveTimeout: NodeJS.Timeout | null = null; let keepaliveTimeout: NodeJS.Timeout | null = null;
let keepaliveDisabled = false;
let sessionClosedByServer = false; let sessionClosedByServer = false;
const idleTimeoutObj = this.enableIdleTimeout(session); const idleTimeoutObj = this.enableIdleTimeout(session);
@ -1626,7 +1623,7 @@ export class Server {
const canSendPing = () => { const canSendPing = () => {
return ( return (
!keepaliveDisabled && !session.destroyed &&
this.keepaliveTimeMs < KEEPALIVE_MAX_TIME_MS && this.keepaliveTimeMs < KEEPALIVE_MAX_TIME_MS &&
this.keepaliveTimeMs > 0 this.keepaliveTimeMs > 0
); );
@ -1734,7 +1731,6 @@ export class Server {
clearTimeout(connectionAgeGraceTimer); clearTimeout(connectionAgeGraceTimer);
} }
keepaliveDisabled = true;
clearKeepaliveTimeout(); clearKeepaliveTimeout();
if (idleTimeoutObj !== null) { if (idleTimeoutObj !== null) {

View File

@ -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. * 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 * 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. * calls, and a ping should be sent the next time a call starts.
@ -416,7 +416,7 @@ class Http2Transport implements Transport {
this.pendingSendKeepalivePing = true; this.pendingSendKeepalivePing = true;
return; return;
} }
if (this.keepaliveTimeout) { if (this.keepaliveTimer) {
console.error('keepaliveTimeout is not null'); console.error('keepaliveTimeout is not null');
return; return;
} }
@ -426,11 +426,11 @@ class Http2Transport implements Transport {
this.keepaliveTrace( this.keepaliveTrace(
'Sending ping with timeout ' + this.keepaliveTimeoutMs + 'ms' 'Sending ping with timeout ' + this.keepaliveTimeoutMs + 'ms'
); );
this.keepaliveTimeout = setTimeout(() => { this.keepaliveTimer = setTimeout(() => {
this.keepaliveTrace('Ping timeout passed without response'); this.keepaliveTrace('Ping timeout passed without response');
this.handleDisconnect(); this.handleDisconnect();
}, this.keepaliveTimeoutMs); }, this.keepaliveTimeoutMs);
this.keepaliveTimeout.unref?.(); this.keepaliveTimer.unref?.();
let pingSendError = ''; let pingSendError = '';
try { try {
const pingSentSuccessfully = this.session.ping( const pingSentSuccessfully = this.session.ping(
@ -471,14 +471,14 @@ class Http2Transport implements Transport {
if (this.pendingSendKeepalivePing) { if (this.pendingSendKeepalivePing) {
this.pendingSendKeepalivePing = false; this.pendingSendKeepalivePing = false;
this.maybeSendPing(); this.maybeSendPing();
} else if (!this.keepaliveTimeout) { } else if (!this.keepaliveTimer) {
this.keepaliveTrace( this.keepaliveTrace(
'Starting keepalive timer for ' + this.keepaliveTimeMs + 'ms' 'Starting keepalive timer for ' + this.keepaliveTimeMs + 'ms'
); );
this.keepaliveTimeout = setTimeout(() => { this.keepaliveTimer = setTimeout(() => {
this.maybeSendPing(); this.maybeSendPing();
}, this.keepaliveTimeMs); }, this.keepaliveTimeMs);
this.keepaliveTimeout.unref?.(); this.keepaliveTimer.unref?.();
} }
/* Otherwise, there is already either a keepalive timer or a ping pending, /* Otherwise, there is already either a keepalive timer or a ping pending,
* wait for those to resolve. */ * wait for those to resolve. */
@ -488,9 +488,9 @@ class Http2Transport implements Transport {
* Clears whichever keepalive timeout is currently active, if any. * Clears whichever keepalive timeout is currently active, if any.
*/ */
private clearKeepaliveTimeout() { private clearKeepaliveTimeout() {
if (this.keepaliveTimeout) { if (this.keepaliveTimer) {
clearTimeout(this.keepaliveTimeout); clearTimeout(this.keepaliveTimer);
this.keepaliveTimeout = null; this.keepaliveTimer = null;
} }
} }