feat: add retry util#157
Open
FarajiOranj wants to merge 1 commit into
Open
Conversation
Add a `retry` utility that calls a function and retries it if it throws, until it succeeds or a maximum number of attempts is reached. Configurable via `maxAttempts`, `delay` (a fixed ms value or a function of the attempt number, defaulting to an exponential backoff), and a `shouldRetry` predicate. The action always runs at least once, and a throwing delay strategy can't mask the action's error.
🦋 Changeset detectedLatest commit: d3a1d98 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #136.
Adds a built-in
retryutility that calls a function and retries it if itthrows, until it succeeds or a maximum number of attempts is reached. The docs
showed a couple of hand-rolled retry loops; this makes it a first-class helper.
Usage
API
retry(action, options?)callsaction(attempt)(1-based) and returns itsresult, retrying on throw. Options:
maxAttempts(default3) - total attempts, including the first. Valuesless than 1 are treated as 1 (the action always runs at least once).
delay- the wait before each retry in ms, or a function of the failedattempt number. Defaults to an exponential backoff (
100ms,200ms, ...).shouldRetry(error, attempt) => boolean | Promise<boolean>- returnfalseto rethrow immediately. Defaults to always retrying untilmaxAttempts.On the final attempt it throws the last error without waiting or consulting
shouldRetry, and a throwingdelaystrategy never masks the action's error.Also exports
RetryOptions,DEFAULT_MAX_ATTEMPTS, anddefaultRetryDelay, andupdates the "Retry Logic" docs to point at the built-in util.
Notes / possible follow-up
The issue also mentions "possibly options on client operations for automatic
retries." I kept this PR to the standalone utility (which composes with any
operation); wiring per-operation retry options into the client would make a good
follow-up.
Tests
retry.test.tscovers success without retry, retry-until-success, throwing thelast error,
maxAttempts(incl.<= 1), attempt numbering, the delay strategy,shouldRetry(false / awaited / not-called-on-final), and that a throwing delaystrategy rethrows the original error.