Skip to content

Commit 42124c0

Browse files
authored
Add retry logic to querier (#678)
1 parent 0faebfa commit 42124c0

File tree

12 files changed

+309
-15
lines changed

12 files changed

+309
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [unreleased]
99

10+
## [14.1.4] - 2023-08-28
11+
12+
- Adds logic to retry network calls if the core returns status 429
13+
1014
## [14.1.3] - 2023-07-03
1115

1216
### Changes

lib/build/constants.d.ts

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/constants.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/querier.js

Lines changed: 27 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/version.d.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/version.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ts/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515

1616
export const HEADER_RID = "rid";
1717
export const HEADER_FDI = "fdi-version";
18+
export const RATE_LIMIT_STATUS_CODE = 429;

lib/ts/querier.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { cdiSupported } from "./version";
1919
import NormalisedURLDomain from "./normalisedURLDomain";
2020
import NormalisedURLPath from "./normalisedURLPath";
2121
import { PROCESS_STATE, ProcessState } from "./processState";
22+
import { RATE_LIMIT_STATUS_CODE } from "./constants";
2223

2324
export class Querier {
2425
private static initCalled = false;
@@ -263,7 +264,8 @@ export class Querier {
263264
path: NormalisedURLPath,
264265
method: string,
265266
requestFunc: (url: string) => Promise<Response>,
266-
numberOfTries: number
267+
numberOfTries: number,
268+
retryInfoMap?: Record<string, number>
267269
): Promise<any> => {
268270
if (this.__hosts === undefined) {
269271
throw Error(
@@ -276,6 +278,17 @@ export class Querier {
276278
let currentDomain: string = this.__hosts[Querier.lastTriedIndex].domain.getAsStringDangerous();
277279
let currentBasePath: string = this.__hosts[Querier.lastTriedIndex].basePath.getAsStringDangerous();
278280
const url = currentDomain + currentBasePath + path.getAsStringDangerous();
281+
282+
const maxRetries = 5;
283+
284+
if (retryInfoMap === undefined) {
285+
retryInfoMap = {};
286+
}
287+
288+
if (retryInfoMap[url] === undefined) {
289+
retryInfoMap[url] = maxRetries;
290+
}
291+
279292
Querier.lastTriedIndex++;
280293
Querier.lastTriedIndex = Querier.lastTriedIndex % this.__hosts.length;
281294
try {
@@ -296,9 +309,24 @@ export class Querier {
296309
err.message !== undefined &&
297310
(err.message.includes("Failed to fetch") || err.message.includes("ECONNREFUSED"))
298311
) {
299-
return await this.sendRequestHelper(path, method, requestFunc, numberOfTries - 1);
312+
return await this.sendRequestHelper(path, method, requestFunc, numberOfTries - 1, retryInfoMap);
300313
}
301314
if (err instanceof Response) {
315+
if (err.status === RATE_LIMIT_STATUS_CODE) {
316+
const retriesLeft = retryInfoMap[url];
317+
318+
if (retriesLeft > 0) {
319+
retryInfoMap[url] = retriesLeft - 1;
320+
321+
const attemptsMade = maxRetries - retriesLeft;
322+
const delay = 10 + 250 * attemptsMade;
323+
324+
await new Promise((resolve) => setTimeout(resolve, delay));
325+
326+
return await this.sendRequestHelper(path, method, requestFunc, numberOfTries, retryInfoMap);
327+
}
328+
}
329+
302330
throw new Error(
303331
"SuperTokens core threw an error for a " +
304332
method +
@@ -309,9 +337,9 @@ export class Querier {
309337
" and message: " +
310338
(await err.text())
311339
);
312-
} else {
313-
throw err;
314340
}
341+
342+
throw err;
315343
}
316344
};
317345
}

lib/ts/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* License for the specific language governing permissions and limitations
1313
* under the License.
1414
*/
15-
export const version = "14.1.3";
15+
export const version = "14.1.4";
1616

1717
export const cdiSupported = ["2.21"];
1818

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)