@edgetime/resilience
Retry with backoff, timeouts, bounded concurrency, fallback reads and replicated writes. No dependencies.
npm install @edgetime/resilienceHelpers for calls that can fail: retries, bounded concurrency, fallback reads and replicated writes. No dependencies, and nothing here knows about HTTP or any platform.
retry(operation, options?)
Section titled “retry(operation, options?)”Calls operation(attempt) until it resolves and throws the last failure. Only retry operations
that are safe to repeat.
import { retry } from '@edgetime/resilience';
const invoice = await retry(() => billing.fetchInvoice(id), { attempts: 4, retryable: (error) => error instanceof UpstreamError && error.status >= 500, signal: request.signal,});| Option | Default | Purpose |
|---|---|---|
attempts |
3 |
Total tries, including the first. |
delay |
100 ms doubling to 2 s, with full jitter | Milliseconds to wait before each retry, counted from 1. A result that is not a positive finite number means no wait. |
retryable |
every failure | Return true to try again. Anything else, a throw or a promise stops and throws the failure. |
signal |
none | Aborting stops the wait and throws the abort reason. It is also passed to operation as its second argument. |
timeout |
none | Milliseconds each attempt may take. A slower attempt fails with TimeoutError, which is retried like any other failure, and the signal passed to it is aborted. |
sleep |
timer | Replaces the timer, for tests. |
firstAvailable(sources, options?)
Section titled “firstAvailable(sources, options?)”Reads from each source in order and returns the first value that is not null or undefined. A
source that throws hands over to the next. It returns undefined only when every source answered
and none had the value. If any source failed and no other had the value, it throws the first
error: the source that is down may hold what the others lack, so an outage is not reported as a
missing value. onError(error, index) is called for each failing source; if it throws,
the next source is still tried. With timeout, a source that takes longer than that many
milliseconds counts as failed with a TimeoutError, and the signal it receives is aborted.
writeAll(writes, options?)
Section titled “writeAll(writes, options?)”Runs the first write and throws if it fails. The remaining writes are replicas: they run together
afterwards, and a failure goes to onError(error, index) without failing the call, so a replica
can fall behind until the write is repeated. It resolves to the number of failed replica writes.
Pass waitUntil to let replica writes finish in the background; the result is then 0. Pass it
as (promise) => ctx.waitUntil(promise): a bare ctx.waitUntil loses its receiver and throws,
in which case the replicas are waited for instead. With timeout, each write may take that many
milliseconds: a slow primary fails the call with a TimeoutError, and a slow replica is reported
to onError.
Both functions skip an entry that is false, null or undefined, so a second store is one
conditional entry:
import { firstAvailable, writeAll } from '@edgetime/resilience';
await writeAll([() => primary.put(key, bytes), replica && (() => replica.put(key, bytes))], { onError: (error) => log.error('replica write failed', { object: key, error }),});
const object = await firstAvailable([ () => primary.get(key), replica && (() => replica.get(key)),]);A body that can only be read once, such as a stream, needs a separate copy for each write.
withTimeout(call, ms, { signal? })
Section titled “withTimeout(call, ms, { signal? })”Calls call(signal) and settles with its result, or rejects with a TimeoutError (carrying
timeout) once ms milliseconds pass, aborting signal at that point. Aborting the optional
outer signal settles it at once with the abort reason. A call that ignores its signal keeps
running, but nothing waits for it. ms must be above 0 and at most a day.
import { withTimeout } from '@edgetime/resilience';
const profile = await withTimeout((signal) => fetch(profileUrl, { signal }), 2000);mapLimit(items, limit, call)
Section titled “mapLimit(items, limit, call)”Runs call(item, index) over items with at most limit in flight and returns the results in
order. It rejects on the first failure and starts no further items; calls already in flight
finish. A limit below 1 throws.
backoff(attempts, { base?, max? })
Section titled “backoff(attempts, { base?, max? })”An exponential delay in seconds for the nth attempt: base (30) doubling up to max (900). It
suits redelivery delays measured in seconds; retry() has its own millisecond default. A missing
attempt count is treated as the first attempt, and a base or max that is not positive throws.
Source: packages/resilience on GitHub.