Failsafe
A circuit breaker implementation which used to detect failures and encapsulates the logic of preventing a failure from constantly recurring, during maintenance, temporary external system failure or unexpected system difficulties.
Features
- Working with both
Fn() -> Result
andFuture
. - Backoff strategies:
constant
,exponential
,equal_jittered
,full_jittered
- Failure detection policies:
consecutive_failures
,success_rate_over_time_window
- Minimum rust version: 1.27
Usage
Add this to your Cargo.toml:
= "0.2.0"
and this to your crate root:
extern crate failsafe;
Example
Using default backoff strategy and failure accrual policy.
use ;
// A function that sometimes failed.
// Create a circuit breaker which configured by reasonable default backoff and
// failure accrual policy.
let circuit_breaker = default;
// Call the function in a loop, after some iterations the circuit breaker will
// be in a open state and reject next calls.
for n in 0..100
Or configure custom backoff and policy:
use Duration;
use ;
// Create an exponential growth backoff which starts from 10s and ends with 60s.
let backoff = exponential;
// Create a policy which failed when three consecutive failures were made.
let policy = consecutive_failures;
// Creates a circuit breaker with given policy.
let circuit_breaker = builder
.failure_policy
.build;