From 73f4374e1e44b58b95525c9bff1aba8f76bd961f Mon Sep 17 00:00:00 2001 From: trevor-signal <131492920+trevor-signal@users.noreply.github.com> Date: Wed, 17 May 2023 12:43:33 -0400 Subject: [PATCH] Improved WS request timeout handling --- ts/textsecure/WebsocketResources.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/ts/textsecure/WebsocketResources.ts b/ts/textsecure/WebsocketResources.ts index aedc90431..c6cc77814 100644 --- a/ts/textsecure/WebsocketResources.ts +++ b/ts/textsecure/WebsocketResources.ts @@ -200,10 +200,14 @@ export default class WebSocketResource extends EventTarget { strictAssert(!this.shuttingDown, 'Cannot send request, shutting down'); this.addActive(idString); const promise = new Promise((resolve, reject) => { + const sentAt = Date.now(); + let timedOut = false; + let timer = options.timeout ? Timers.setTimeout(() => { + timedOut = true; this.removeActive(idString); - reject(new Error('Request timed out')); + reject(new Error(`Request timed out; id: [${idString}]`)); }, options.timeout) : undefined; @@ -212,7 +216,13 @@ export default class WebSocketResource extends EventTarget { Timers.clearTimeout(timer); timer = undefined; } - + if (timedOut) { + log.warn( + `Response received after timeout; id: [${idString}], path: [${ + options.path + }], response time: [${Date.now() - sentAt}]` + ); + } this.removeActive(idString); resolve(result); }); @@ -390,6 +400,7 @@ export type KeepAliveOptionsType = { // 30 seconds + 5 seconds for closing the socket above. const KEEPALIVE_INTERVAL_MS = 30 * durations.SECOND; const MAX_KEEPALIVE_INTERVAL_MS = 5 * durations.MINUTE; +const LOG_KEEPALIVE_AFTER_MS = 500; class KeepAlive { private keepAliveTimer: Timers.Timeout | undefined; @@ -446,11 +457,20 @@ class KeepAlive { } log.info('WebSocketResources: Sending a keepalive message'); + const sentAt = Date.now(); + const { status } = await this.wsr.sendRequest({ verb: 'GET', 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) { this.reset(); }