firefly-limiter
v1.0.3
Published
Resilience harness for TypeScript: retries, timeouts, circuit breakers, bulkheads and rate limits, composed as one policy.
Downloads
124,017
Maintainers
Readme
firefly-limiter

Retries, timeouts, circuit breakers, bulkheads, rate limits and hedging for the outgoing calls of one process, for setups without a service mesh.
npm install firefly-limiterNode 22 or later, ESM only. CI runs Node 22 and 24 on Linux, Windows and macOS. No runtime dependencies, and nothing runs at import time.
Example
Describe the upstream once, next to the client that calls it:
import { Dependency, exponential, retryAnything } from "firefly-limiter";
export const rates = new Dependency({
name: "rates",
attempts: 3,
backoff: exponential({ base: 200, max: 5_000 }),
shouldRetry: retryAnything,
deadline: 2_000,
breaker: { threshold: 5, resetAfter: 30_000 },
});Every field is required. There are no default numbers. Then call through it. Each attempt gets an AbortSignal that the deadline aborts:
const today = await rates.run((signal) => readRates(signal), {
share: "today", // concurrent callers with this key make one request
fallback: () => cached, // answer here if every attempt failed
});When the attempts run out, your last error is rethrown unchanged. Firefly's own errors (TimeoutError, CircuitOpenError, BulkheadFullError, RateLimitError) are only for its refusals.
To put the same policy under an HTTP client instead, wrap its fetch:
import { retryableTransportError, transport } from "firefly-limiter/http";
const api = new ApiClient({
baseUrl: "https://api.example.com/v1",
transport: transport(fetch, rates.policy),
});health() returns the current state without changing it, so a health endpoint can call it:
app.get("/health", () => rates.health());
// { name: "rates", circuit: "closed", failures: 0, inFlight: 0, queued: 0 }Not included
- State shared across processes. Counts live in one process, so 20 replicas means 20 breakers. See Boundaries.
- Use behind a service mesh. If Envoy or Istio already retries, the two layers of retries multiply.
- Protecting a server from its callers. This is for calls you make.
- I/O, caching, logging and metrics. You pass the call in, caching is Repletes, and
onEventgives you every decision for your own metrics.
Known quirks
- Inside a transport a 429 is a resolved
Response, not anHttpError, so ashouldRetrythat checks forHttpErrornever matches. UseretryableTransportErrorfor a dependency you hand totransport. - Create each
DependencyorCircuitBreakeronce and share it. One created inside a client factory counts only that client's calls, and one created per request never trips. stackthrows aTypeErrorwhen policies are in an order that cannot work, for exampletimeoutoutsideretry.Dependencybuilds the order for you.
Guides
Full API reference: https://smiduweorc.github.io/firefly/
- Concepts
- Dependency
- Policies
- HTTP, including use with Aphid and dung beetle clients
- Testing
- Boundaries
License
MIT
