From 0c1dd73341bdbdb05dfea0b6215fa4e0c3cd7a8b Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 13 Dec 2024 15:56:07 +0000 Subject: [PATCH 1/2] on 429, retry `fetchAccount` instead of failing when switching between accounts, with many tabs open (10 seem to be enough), they all hit the endpoint at the same time, and some get rate limited. treating a 429 as a fatal error confuses the frontend, which ends up logging the user out of all their accounts. this code makes the frontend retry, after waiting the appropriate amount of time. seems to work fine in my testing. --- packages/backend/src/server/api/ApiCallService.ts | 1 + packages/frontend/src/account.ts | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/packages/backend/src/server/api/ApiCallService.ts b/packages/backend/src/server/api/ApiCallService.ts index c6c33f7303..974be7e4e1 100644 --- a/packages/backend/src/server/api/ApiCallService.ts +++ b/packages/backend/src/server/api/ApiCallService.ts @@ -340,6 +340,7 @@ export class ApiCallService implements OnApplicationShutdown { code: 'RATE_LIMIT_EXCEEDED', id: 'd5826d14-3982-4d2e-8011-b9e9f02499ef', httpStatusCode: 429, + info, }); } } diff --git a/packages/frontend/src/account.ts b/packages/frontend/src/account.ts index e3416f2c29..f0a464084f 100644 --- a/packages/frontend/src/account.ts +++ b/packages/frontend/src/account.ts @@ -147,6 +147,13 @@ function fetchAccount(token: string, id?: string, forceShowDialog?: boolean): Pr text: i18n.ts.tokenRevokedDescription, }); } + } else if (res.error.id === 'd5826d14-3982-4d2e-8011-b9e9f02499ef') { + // rate limited + const timeToWait = res.error.info?.resetMs ?? 1000; + window.setTimeout(timeToWait, () => { + fetchAccount(token, id, forceShowDialog).then(done, fail); + }); + return; } else { await alert({ type: 'error', From 9b1fc969086fb0c3ea88c979f254e964f9218dc5 Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 13 Dec 2024 16:17:43 +0000 Subject: [PATCH 2/2] fix passing rate limiting info via ApiError --- packages/backend/src/server/api/ApiCallService.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/backend/src/server/api/ApiCallService.ts b/packages/backend/src/server/api/ApiCallService.ts index 974be7e4e1..03f25a51fe 100644 --- a/packages/backend/src/server/api/ApiCallService.ts +++ b/packages/backend/src/server/api/ApiCallService.ts @@ -340,8 +340,7 @@ export class ApiCallService implements OnApplicationShutdown { code: 'RATE_LIMIT_EXCEEDED', id: 'd5826d14-3982-4d2e-8011-b9e9f02499ef', httpStatusCode: 429, - info, - }); + }, info); } } }