Skip to content

BackoffPolicy

Defined in: src/pattern/BackoffPolicy.ts:22

Pure backoff-policy primitives — stateless functions of (restartCount) to a delay in milliseconds. Decoupled from BackoffSupervisor so callers can plug in their own policy without subclassing the supervisor, and so the policy itself is trivially unit-testable.

Two built-ins:

  • exponentialBackoffmin × 2^n clamped to max, with optional ± jitter expressed as a fraction of the un-jittered delay. This is the policy you usually want — fast-then-slow, randomised so a herd of clients doesn’t synchronise their reconnect attempts.

  • linearBackoffmin + step × n clamped to max. Niche; use when you specifically want bounded growth (e.g. polling cadence that should plateau quickly).

The randomness source defaults to Math.random but can be overridden — pass a seeded RNG to make the policy deterministic in tests.

delayFor(restartCount): number

Defined in: src/pattern/BackoffPolicy.ts:28

Delay in milliseconds before the next restart attempt. restartCount is 0-based: the first restart (after the very first failure) passes 0, the second restart passes 1, etc.

number

number