Improved WS request timeout handling

This commit is contained in:
trevor-signal 2023-05-17 12:43:33 -04:00 committed by GitHub
parent 0e79a99a5d
commit 73f4374e1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -200,10 +200,14 @@ export default class WebSocketResource extends EventTarget {
strictAssert(!this.shuttingDown, 'Cannot send request, shutting down'); strictAssert(!this.shuttingDown, 'Cannot send request, shutting down');
this.addActive(idString); this.addActive(idString);
const promise = new Promise<SendRequestResult>((resolve, reject) => { const promise = new Promise<SendRequestResult>((resolve, reject) => {
const sentAt = Date.now();
let timedOut = false;
let timer = options.timeout let timer = options.timeout
? Timers.setTimeout(() => { ? Timers.setTimeout(() => {
timedOut = true;
this.removeActive(idString); this.removeActive(idString);
reject(new Error('Request timed out')); reject(new Error(`Request timed out; id: [${idString}]`));
}, options.timeout) }, options.timeout)
: undefined; : undefined;
@ -212,7 +216,13 @@ export default class WebSocketResource extends EventTarget {
Timers.clearTimeout(timer); Timers.clearTimeout(timer);
timer = undefined; timer = undefined;
} }
if (timedOut) {
log.warn(
`Response received after timeout; id: [${idString}], path: [${
options.path
}], response time: [${Date.now() - sentAt}]`
);
}
this.removeActive(idString); this.removeActive(idString);
resolve(result); resolve(result);
}); });
@ -390,6 +400,7 @@ export type KeepAliveOptionsType = {
// 30 seconds + 5 seconds for closing the socket above. // 30 seconds + 5 seconds for closing the socket above.
const KEEPALIVE_INTERVAL_MS = 30 * durations.SECOND; const KEEPALIVE_INTERVAL_MS = 30 * durations.SECOND;
const MAX_KEEPALIVE_INTERVAL_MS = 5 * durations.MINUTE; const MAX_KEEPALIVE_INTERVAL_MS = 5 * durations.MINUTE;
const LOG_KEEPALIVE_AFTER_MS = 500;
class KeepAlive { class KeepAlive {
private keepAliveTimer: Timers.Timeout | undefined; private keepAliveTimer: Timers.Timeout | undefined;
@ -446,11 +457,20 @@ class KeepAlive {
} }
log.info('WebSocketResources: Sending a keepalive message'); log.info('WebSocketResources: Sending a keepalive message');
const sentAt = Date.now();
const { status } = await this.wsr.sendRequest({ const { status } = await this.wsr.sendRequest({
verb: 'GET', verb: 'GET',
path: this.path, path: this.path,
}); });
const responseTime = Date.now() - sentAt;
if (responseTime > LOG_KEEPALIVE_AFTER_MS) {
log.warn(
`Delayed response to keepalive request, response time: [${responseTime}]`
);
}
if (status >= 200 || status < 300) { if (status >= 200 || status < 300) {
this.reset(); this.reset();
} }