Overview
The configuration system provides fine-grained control over retry behavior, timeouts, backoff strategies, circuit breakers, and observability hooks.ResilienceConfig
Main configuration object forwithResilience.
src/global.d.ts:34-43.
Configuration Options
Optional name for the wrapped function. Used in:
- Hook callbacks for identification
- Error messages (e.g., “CircuitOpenError: function-name”)
- Metrics tracking
fn.name, then “anonymous”Maximum execution time in milliseconds before timing out.When exceeded:
- Execution is cancelled (if
useAbortSignalis true) - Throws an
Error('TimeoutError') - Counts as a failure for circuit breaker and retries
Number of retry attempts after the initial failure.
- Total attempts =
retries + 1(initial + retries) - Each retry can have a backoff delay
- Retries respect the
retryOnpredicate
0 (no retries)Delay strategy between retry attempts. See BackoffStrategy for details.Default: No delay between retries
Predicate function to determine if an error should trigger a retry.
- Called with the error object after each failure
- Return
trueto retry,falseto fail immediately - Only called if retries are remaining
() => true (retry all errors)Circuit breaker configuration to prevent cascading failures. See CircuitBreakerConfig for details.Default: No circuit breaker
Lifecycle hooks for observability, metrics, and logging. See Hooks for full reference.Default: No hooks
Enable automatic abort signal management for cancellation support.When enabled:
- Creates an
AbortControllerfor each execution - Makes the signal available to
sleepandresilientFetch - Aborts the signal when timeout is reached
- Enables automatic cancellation of async operations
falseBackoffStrategy
Defines the delay between retry attempts.src/global.d.ts:15-17.
Fixed Backoff
Constant delay between retries.Specifies fixed backoff strategy.
Delay in milliseconds between each retry attempt.
Exponential Backoff
Delay increases exponentially with each retry.Specifies exponential backoff strategy.
Base delay in milliseconds. The actual delay is calculated as:
Maximum delay in milliseconds. Caps the exponential growth.
Add random jitter to prevent thundering herd.When enabled, the delay is a random value between
0 and the calculated delay.src/index.ts:72-81):
CircuitBreakerConfig
Configuration for circuit breaker pattern to prevent cascading failures.src/global.d.ts:19-22.
Number of consecutive failures before opening the circuit.Once the threshold is reached:
- Circuit state changes to “OPEN”
- All attempts are rejected immediately with
CircuitOpenError - No actual function execution occurs
Time in milliseconds to wait before attempting to close the circuit.After this timeout:
- Circuit state changes to “HALF_OPEN”
- Next attempt is allowed (probe)
- If probe succeeds: circuit closes
- If probe fails: circuit reopens
Circuit States
src/global.d.ts:3.
Normal operation. All requests are allowed. Failures are counted.
Circuit is tripped. All requests are rejected immediately with
CircuitOpenError. No function execution occurs.Testing if service recovered. One request is allowed as a probe. Success closes the circuit, failure reopens it.
Complete Configuration Example
Configuration Patterns
Aggressive Retries for Critical Operations
Conservative Retries for External APIs
Fast Fail for User-Facing Operations
Background Job Processing
Related
- withResilience - Main function using this configuration
- Hooks - Full hooks API reference
- WrapperInit - Helper for managing configuration with metrics

