-
Notifications
You must be signed in to change notification settings - Fork 164
feature: Exponential Backoff And Jitter #582
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
Open
ysknsid25
wants to merge
1
commit into
unjs:main
Choose a base branch
from
ysknsid25:feature/backoff-and-jitter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Exponential backoff + jitter strategies. | ||
| // Reference: https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/ | ||
|
|
||
| export type RetryBackoffStrategy = | ||
| | "full-jitter" | ||
| | "equal-jitter" | ||
| | "decorrelated-jitter"; | ||
|
|
||
| export interface RetryBackoffOptions { | ||
| strategy: RetryBackoffStrategy; | ||
| /** Minimum delay in milliseconds. */ | ||
| base: number; | ||
| /** Maximum delay in milliseconds. */ | ||
| cap: number; | ||
| } | ||
|
|
||
| export interface ComputeBackoffParams { | ||
| options: RetryBackoffOptions; | ||
| /** 0-indexed: how many retries have already happened. */ | ||
| attempt: number; | ||
| /** Previous sleep value, used by decorrelated-jitter. */ | ||
| prevDelay?: number; | ||
| /** Random source (DI for tests). Defaults to Math.random. */ | ||
| random?: () => number; | ||
| } | ||
|
|
||
| export function computeBackoffDelay(params: ComputeBackoffParams): number { | ||
| const { options, attempt, prevDelay, random = Math.random } = params; | ||
| const { strategy, base, cap } = options; | ||
|
|
||
| // Saturate the exponent first to avoid 2 ** large -> Infinity. | ||
| const expCap = Math.min(cap, base * 2 ** Math.min(attempt, 31)); | ||
|
|
||
| if (strategy === "full-jitter") { | ||
| // sleep = random(0, min(cap, base * 2^attempt)) | ||
| return Math.floor(random() * expCap); | ||
| } | ||
|
|
||
| if (strategy === "equal-jitter") { | ||
| // sleep = temp/2 + random(0, temp/2) | ||
| return Math.floor(expCap / 2 + random() * (expCap / 2)); | ||
| } | ||
|
|
||
| // decorrelated-jitter: sleep = min(cap, random(base, prev * 3)) | ||
| const prev = prevDelay ?? base; | ||
| const upper = Math.max(base, prev * 3); | ||
| const sleep = base + random() * (upper - base); | ||
| return Math.floor(Math.min(cap, sleep)); | ||
| } | ||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { computeBackoffDelay } from "../src/retry.ts"; | ||
|
|
||
| describe("computeBackoffDelay", () => { | ||
| describe("full-jitter", () => { | ||
| const options = { | ||
| strategy: "full-jitter" as const, | ||
| base: 100, | ||
| cap: 3000, | ||
| }; | ||
|
|
||
| it("random=0 returns 0", () => { | ||
| expect( | ||
| computeBackoffDelay({ options, attempt: 0, random: () => 0 }) | ||
| ).toBe(0); | ||
| }); | ||
|
|
||
| it("random~=1 approaches expCap", () => { | ||
| // expCap = min(3000, 100 * 2^0) = 100; sleep = 0.999 * 100 = 99 | ||
| expect( | ||
| computeBackoffDelay({ options, attempt: 0, random: () => 0.999 }) | ||
| ).toBe(99); | ||
| }); | ||
|
|
||
| it("upper bound grows exponentially within cap", () => { | ||
| // attempt=3 -> expCap = min(3000, 800) = 800 | ||
| const high = computeBackoffDelay({ | ||
| options, | ||
| attempt: 3, | ||
| random: () => 0.999, | ||
| }); | ||
| expect(high).toBeGreaterThanOrEqual(799 - 1); | ||
| expect(high).toBeLessThan(800); | ||
| }); | ||
|
|
||
| it("saturates at cap for large attempt", () => { | ||
| const high = computeBackoffDelay({ | ||
| options, | ||
| attempt: 50, | ||
| random: () => 0.999, | ||
| }); | ||
| expect(high).toBeLessThanOrEqual(options.cap); | ||
| expect(high).toBeGreaterThan(options.cap - 5); | ||
| }); | ||
| }); | ||
|
|
||
| describe("equal-jitter", () => { | ||
| const options = { | ||
| strategy: "equal-jitter" as const, | ||
| base: 100, | ||
| cap: 3000, | ||
| }; | ||
|
|
||
| it("attempt=0 with random=0 returns base/2", () => { | ||
| // temp = min(3000, 100 * 2^0) = 100; sleep = 50 + 0 = 50 | ||
| expect( | ||
| computeBackoffDelay({ options, attempt: 0, random: () => 0 }) | ||
| ).toBe(50); | ||
| }); | ||
|
|
||
| it("attempt=0 with random~=1 returns just under base", () => { | ||
| // temp = 100; sleep = 50 + 0.999 * 50 ~= 99 | ||
| expect( | ||
| computeBackoffDelay({ options, attempt: 0, random: () => 0.999 }) | ||
| ).toBe(99); | ||
| }); | ||
|
|
||
| it("grows exponentially within cap", () => { | ||
| // attempt=3 -> temp = min(3000, 100*8) = 800; sleep range [400, 800) | ||
| const mid = computeBackoffDelay({ | ||
| options, | ||
| attempt: 3, | ||
| random: () => 0.5, | ||
| }); | ||
| expect(mid).toBeGreaterThanOrEqual(400); | ||
| expect(mid).toBeLessThan(800); | ||
| }); | ||
|
|
||
| it("saturates at cap", () => { | ||
| // very large attempt should still respect cap; max sleep == cap | ||
| const high = computeBackoffDelay({ | ||
| options, | ||
| attempt: 50, | ||
| random: () => 0.999, | ||
| }); | ||
| expect(high).toBeLessThanOrEqual(options.cap); | ||
| expect(high).toBeGreaterThanOrEqual(options.cap / 2 - 1); | ||
| }); | ||
| }); | ||
|
|
||
| describe("decorrelated-jitter", () => { | ||
| const options = { | ||
| strategy: "decorrelated-jitter" as const, | ||
| base: 100, | ||
| cap: 3000, | ||
| }; | ||
|
|
||
| it("first call (no prevDelay) uses base as prev", () => { | ||
| // upper = max(100, 100*3) = 300; sleep = 100 + 0 = 100 | ||
| expect( | ||
| computeBackoffDelay({ options, attempt: 0, random: () => 0 }) | ||
| ).toBe(100); | ||
| }); | ||
|
|
||
| it("first call upper bound is base*3", () => { | ||
| // sleep = 100 + 0.999 * (300 - 100) ~= 299 | ||
| expect( | ||
| computeBackoffDelay({ options, attempt: 0, random: () => 0.999 }) | ||
| ).toBe(299); | ||
| }); | ||
|
|
||
| it("grows from prevDelay", () => { | ||
| // prev=500 -> upper = 1500; sleep at random=0.5 = 100 + 700 = 800 | ||
| expect( | ||
| computeBackoffDelay({ | ||
| options, | ||
| attempt: 1, | ||
| prevDelay: 500, | ||
| random: () => 0.5, | ||
| }) | ||
| ).toBe(800); | ||
| }); | ||
|
|
||
| it("saturates at cap", () => { | ||
| expect( | ||
| computeBackoffDelay({ | ||
| options, | ||
| attempt: 10, | ||
| prevDelay: 5000, | ||
| random: () => 0.999, | ||
| }) | ||
| ).toBe(options.cap); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clarify
basesemantics in API docs.baseis not a strict minimum delay forfull-jitterandequal-jitter(those can go belowbase, including0). Please reword this to avoid misleading users.Suggested wording
📝 Committable suggestion
🤖 Prompt for AI Agents