Skip to content

Commit

Permalink
fix: implement exponential back off on the retries of `_refreshAccess…
Browse files Browse the repository at this point in the history
…Token` method (#869)

## What kind of change does this PR introduce?

Currently the retry interval of `_refreshAccessToken` goes 200, 400,
600, 800, where as I believe the intended intervals are 200, 400, 800,
1600...

## What is the current behavior?

The retry interval is multiple of 200 ms.

## What is the new behavior?

The retry interval is properly set exponentially.

---------

Co-authored-by: Kang Ming <kang.ming1996@gmail.com>
  • Loading branch information
dshukertjr and kangmingtay committed Apr 15, 2024
1 parent 6adf8ca commit f66711d
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,9 @@ export default class GoTrueClient {
// will attempt to refresh the token with exponential backoff
return await retryable(
async (attempt) => {
await sleep(attempt * 200) // 0, 200, 400, 800, ...
if (attempt > 0) {
await sleep(200 * Math.pow(2, attempt - 1)) // 200, 400, 800, ...
}

this._debug(debugName, 'refreshing attempt', attempt)

Expand All @@ -1827,12 +1829,15 @@ export default class GoTrueClient {
xform: _sessionResponse,
})
},
(attempt, _, result) =>
result &&
result.error &&
isAuthRetryableFetchError(result.error) &&
// retryable only if the request can be sent before the backoff overflows the tick duration
Date.now() + (attempt + 1) * 200 - startedAt < AUTO_REFRESH_TICK_DURATION
(attempt, error) => {
const nextBackOffInterval = 200 * Math.pow(2, attempt)
return (
error &&
isAuthRetryableFetchError(error) &&
// retryable only if the request can be sent before the backoff overflows the tick duration
Date.now() + nextBackOffInterval - startedAt < AUTO_REFRESH_TICK_DURATION
)
}
)
} catch (error) {
this._debug(debugName, 'error', error)
Expand Down

0 comments on commit f66711d

Please sign in to comment.