Close websocket if request times out

This commit is contained in:
trevor-signal 2024-04-18 13:16:05 -04:00 committed by GitHub
parent 187fe08816
commit d31a4d5d91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -656,13 +656,10 @@ export default class WebSocketResource
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);
this.close(3001, 'Request timed out');
reject(new Error(`Request timed out; id: [${idString}]`)); reject(new Error(`Request timed out; id: [${idString}]`));
}, options.timeout) }, options.timeout)
: undefined; : undefined;
@ -672,16 +669,8 @@ export default class WebSocketResource
Timers.clearTimeout(timer); Timers.clearTimeout(timer);
timer = undefined; timer = undefined;
} }
if (timedOut) {
log.warn( this.keepalive?.reset();
`${this.logId}: Response received after timeout; ` +
`id: [${idString}], path: [${options.path}], ` +
`response time: ${Date.now() - sentAt}ms`
);
} else {
// Reset keepalive when an on-time response arrives
this.keepalive?.reset();
}
this.removeActive(idString); this.removeActive(idString);
resolve(result); resolve(result);
}); });