Failsafe

Сrate Вocumentation CircleCI Appveyor

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

Usage

Add this to your Cargo.toml:

toml failsafe = "1.1.0"

Example

Using default backoff strategy and failure accrual policy.

```rust use failsafe::{Config, CircuitBreaker, Error};

// A function that sometimes failed. fn dangerouscall() -> Result<(), ()> { if threadrng().gen_range(0, 2) == 0 { return Err(()) } Ok(()) }

// Create a circuit breaker which configured by reasonable default backoff and // failure accrual policy. let circuit_breaker = Config::new().build();

// 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 { match circuitbreaker.call(|| dangerouscall()) { Err(Error::Inner(_)) => { eprintln!("{}: fail", n); }, Err(Error::Rejected) => { eprintln!("{}: rejected", n); break; }, _ => {} } } ```

Or configure custom backoff and policy:

```rust use std::time::Duration; use failsafe::{backoff, failure_policy, CircuitBreaker};

// Create an exponential growth backoff which starts from 10s and ends with 60s. let backoff = backoff::exponential(Duration::fromsecs(10), Duration::fromsecs(60));

// Create a policy which failed when three consecutive failures were made. let policy = failurepolicy::consecutivefailures(3, backoff);

// Creates a circuit breaker with given policy. let circuitbreaker = Config::new() .failurepolicy(policy) .build(); ```