Skip to content

feat(js-sdk): support retry-after header #504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ryanpq
Copy link
Contributor

@ryanpq ryanpq commented Mar 4, 2025

Description

Honor the retry-after header so that a client does not overwhelm the FGA server with requests.

References

Closes: openfga/js-sdk#208

Review Checklist

  • I have clicked on "allow edits by maintainers".
  • I have added documentation for new/changed functionality in this PR or in a PR to openfga.dev [Provide a link to any relevant PRs in the references section above]
  • The correct base branch is being used, if not main
  • I have added tests to validate that the change in functionality is working as expected

@ryanpq ryanpq marked this pull request as ready for review March 5, 2025 17:42
@ryanpq ryanpq requested a review from a team as a code owner March 5, 2025 17:42
@@ -486,6 +488,24 @@ describe("{{appTitleCaseName}} SDK", function () {
fgaApi.check(baseConfig.storeId!, { tuple_key: tupleKey }, { retryParams: { maxRetry: 0 }})
).rejects.toThrow(FgaApiInternalError);
});
it("should throw FgaApiInternalError for 500 error without Retry-After header", async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the retry header, it should fall back to exponential backoff

if (status === 429) {
retryDelayMs = randomTime(iterationCount, config.minWaitInMs);
} else if (status >= 500 && status !== 501) {
// For 5xx (except 501), we only retry if Retry-After was present
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change this so that we fall back to exponential backoff here too

const secondsValue = parseInt(retryAfterValue, 10);
if (!isNaN(secondsValue)) {
const msValue = secondsValue * 1000;
if (msValue >= 1000 && msValue <= 1800000) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than these being hard-coded, now that the go-sdk changes have been merged, you can use the following variables:

  • {{maxBackoffTimeInSec}}
Suggested change
if (msValue >= 1000 && msValue <= 1800000) {
if (msValue > 0 && msValue <= {{retryHeaderMaxAllowableDurationInSec}}) {
}

const now = new Date();
const delayMs = dateValue.getTime() - now.getTime();

if (delayMs >= 1000 && delayMs <= 1800000) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic looks repeated, can you extract it to it's own function and reference it above and here?

@@ -128,8 +128,47 @@ function isAxiosError(err: any): boolean {
function randomTime(loopCount: number, minWaitInMs: number): number {
const min = Math.ceil(2 ** loopCount * minWaitInMs);
const max = Math.ceil(2 ** (loopCount + 1) * minWaitInMs);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
const calculatedTime = Math.floor(Math.random() * (max - min) + min);
return Math.min(calculatedTime, 120000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Math.min(calculatedTime, 120000);
return Math.min(calculatedTime, {{maxBackoffTimeInSec}});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defined here:

"maxBackoffTimeInSec": 120,
"retryHeaderMaxAllowableDurationInSec": 1800,

@@ -164,18 +203,40 @@ export async function attemptHttpRequest<B, R>(
throw new FgaApiAuthenticationError(err);
} else if (status === 404) {
throw new FgaApiNotFoundError(err);
} else if (status === 429 || status >= 500) {
if (iterationCount >= config.maxRetry) {
} else if (status === 429 || (status >= 500 && status !== 501)) {
Copy link
Member

@rhamzeh rhamzeh Mar 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also make sure we are retrying on network errors?

Currently we do not retry on them: https://github.com/openfga/js-sdk/blob/8fb95c80ff57d10616c0fd60ca1dfc991ce41675/common.ts#L177

@rhamzeh rhamzeh marked this pull request as draft April 28, 2025 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve the retry strategy
2 participants