resilient
A composable async resilience toolkit for Rust — retry, timeout, circuit breaker, rate limiting, bulkheads, and fallbacks.
Installation
[]
= "0.1"
= { = "1", = ["time", "macros", "rt-multi-thread"] }
Policies
| Policy | What it does |
|---|---|
| Retry | Re-run a failed operation with configurable backoff |
| Timeout | Cancel if the operation takes too long |
| Circuit Breaker | Stop calling a broken dependency until it recovers |
| Rate Limiter | Cap how many calls per second reach a downstream |
| Bulkhead | Limit concurrent in-flight calls |
| Fallback | Return a default value when everything else fails |
Quick Start
use Pipeline;
use ;
let pipeline = new
.with_retry
.with_timeout
.with_circuit_breaker
.with_rate_limiter;
let result: = pipeline
.run
.await;
Individual Policy Usage
Each policy can be used standalone:
Retry
use RetryPolicy;
let policy = default.with_max_retries;
let result: = policy
.run
.await;
Timeout
use TimeoutPolicy;
use Duration;
let policy = default.with_timeout;
let result: = policy
.run
.await;
Circuit Breaker
use ;
let policy = default.with_failure_threshold;
let result: = policy
.run
.await;
Bulkhead
use Bulkhead;
let bulkhead = default.with_max_concurrent;
let result: = bulkhead
.run
.await;
Rate Limiter
use RateLimiter;
let limiter = default.with_capacity.with_refill;
let result: = limiter
.run
.await;
Using Pipeline (Recommended)
The Pipeline composes multiple policies together in the correct order:
use Pipeline;
use Builder as TimeoutBuilder;
use ;
let pipeline = new
.with_retry
.with_timeout
.with_circuit_breaker
.with_rate_limiter
.with_bulkhead;
let result = pipeline
.run
.await;
Note: When using timeout with Pipeline, your error type must implement From<resilient::timeout::TimeoutError>. Use Box<dyn std::error::Error + Send + Sync> or a custom error enum.
Fallback
Provide a fallback when the pipeline fails:
use Pipeline;
let pipeline = default
.with_retry
.or_else;
let result = pipeline
.run
.await;
assert!;
Feature Flags
| Flag | Description |
|---|---|
async-closure |
Enables async closure syntax (nightly only) |
License
MIT — see LICENSE.