From 0e2f81e9715a877f6bcb2cac2bf31b1af5e95f92 Mon Sep 17 00:00:00 2001 From: nexy7574 Date: Sun, 19 Jan 2025 18:32:55 +0000 Subject: [PATCH] Fix the individual request rate-limit (#694) * Fix the individual request rate-limit This fixes the first problem raised in #693 --------- Signed-off-by: nexy7574 Co-authored-by: Gnuxie <50846879+Gnuxie@users.noreply.github.com> --- src/utils.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index ef4fff4d..4c1cba78 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -492,14 +492,18 @@ function patchMatrixClientForRetry() { return cb(...result); } catch (err) { // Need to retry. - let retryAfterMs = attempt * attempt * REQUEST_RETRY_BASE_DURATION_MS; - if ("retry_after_ms" in err) { - try { - retryAfterMs = Number.parseInt(err.retry_after_ms, 10); - } catch (ex) { - // Use default value. + const retryAfterMs = (() => { + if (err instanceof MatrixError && err.retryAfterMs !== undefined) { + return err.retryAfterMs; + } else { + LogService.error( + "Draupnir.client", + "Unable to extract retry_after_ms from error, using fallback to create retry duration", + err + ); + return attempt * attempt * REQUEST_RETRY_BASE_DURATION_MS; } - } + })(); LogService.debug( "Draupnir.client", `Waiting ${retryAfterMs}ms before retrying ${params.method} ${params.uri}`