GitXplorerGitXplorer
d

failsafe-rs

public
184 stars
15 forks
2 issues

Commits

List of commits on branch master.
Verified
5bf89775ea320f288f11935f3b5526e63a865d2a

Create LICENSE (#39)

ddmexe committed 6 months ago
Verified
0fd1f22c1fd4fb6eeed3c2306721b4594ce3c8fe

Use pin_project_lite (#37)

ddmexe committed 6 months ago
Verified
4ef13c04092bca425d772b702f9c8d2b00670e98

Release 1.3.0 (#36)

ddmexe committed 6 months ago
Verified
1400561d4556acc18cfebae851b9ab9f43596207

add BreakerStream (#33)

lleshow committed 6 months ago
Verified
3fc9df51bc65a8051b6dc0024003b123f2423a8e

Export failure_predicate::Any (#32)

lleshow committed 6 months ago
Verified
381e101ff3cf6b9e1cdc53afc0e764b277025b76

fix clippy lints (#34)

lleshow committed 6 months ago

README

The README file for this repository.

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

  • Working with both Fn() -> Result and Future (optional via default futures-support feature).
  • Backoff strategies: constant, exponential, equal_jittered, full_jittered
  • Failure detection policies: consecutive_failures, success_rate_over_time_window
  • Minimum rust version: 1.63

Usage

Add this to your Cargo.toml:

failsafe = "1.3.0"

Example

Using default backoff strategy and failure accrual policy.

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

// A function that sometimes failed.
fn dangerous_call() -> Result<(), ()> {
  if thread_rng().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 circuit_breaker.call(|| dangerous_call()) {
    Err(Error::Inner(_)) => {
      eprintln!("{}: fail", n);
    },
    Err(Error::Rejected) => {
       eprintln!("{}: rejected", n);
       break;
    },
    _ => {}
  }
}

Or configure custom backoff and policy:

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::from_secs(10), Duration::from_secs(60));

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

// Creates a circuit breaker with given policy.
let circuit_breaker = Config::new()
  .failure_policy(policy)
  .build();