Skip to content

Commit

Permalink
Added comments to retry and backoff logic
Browse files Browse the repository at this point in the history
  • Loading branch information
taras committed Jan 8, 2024
1 parent 39f71b9 commit bf5d4fe
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/retry-backoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@ export function* retryBackoffExample<T>(
{ attempts = 5, startDelay = 5, maxDelay = 200 } = {},
): Operation<T | undefined> {
try {
// This is the initial operation,
// if it succeeds then the operation will be complete
return yield* op();
} catch (e) {
// if we encounter error that we're not expecting then throw
// and do not proceed any further
if (!isKnownError(e)) {
throw e;
}
}

// we're here because we encountered a known issue,
// we want to run the retry logic attempting up to 5 attempts
// pause between each attempt and double the delay with each attempt
// trigger a timeout if we reach maxDelay
// we do this by setting up a race between retry logic and
// the timeout. If retry logic succeeds, timeout will be halted
// automatically. If timeout reaches the throw, it'll interrupt the
// retry logic automatically.

let lastError: Error;
function* retry() {
let delay = startDelay;
Expand Down

0 comments on commit bf5d4fe

Please sign in to comment.