GithubHelp home page GithubHelp logo

failsafe-rs's Introduction

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();

failsafe-rs's People

Contributors

dependabot[bot] avatar dmexe avatar fredxia avatar leshow avatar mjkillough avatar niklasad1 avatar sitegui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

failsafe-rs's Issues

Consider Expanding?

Have you considered expanding this library to add other distributed reliability tools, like BulkHeads, RateLimiters, and Retry(s)? It would probably require a serious re-org to move all the stuff in the base module into a more reasonable place, but it also would be nice to have a Rust alternative to r4j or Hystrix.

Edit: I'd be willing to help, but I don't want to sink any time into it if you're not interested in taking the library in that direction and/or supporting it.

License?

Does this project have a license? Maybe MIT License?

Example for sharing StateMachine

Thank you very much for this crate!
I have one question, could you please add an example how the circuit breaker is shared via multiple functions... eg

use failsafe::{CircuitBreaker, Config, Error};
use rand::{thread_rng, Rng};

fn dangerous_call() -> Result<(), ()> {
    if thread_rng().gen_range(0, 2) == 0 {
        return Err(());
    }
    Ok(())
}

fn caller(circuit_breaker: XXXX) {
    for n in 0..100 {
        match circuit_breaker.call(|| dangerous_call()) {
            Err(Error::Inner(_)) => {
                eprintln!("{}: fail", n);
            }
            Err(Error::Rejected) => {
                eprintln!("{}: rejected", n);
                break;
            }
            _ => {}
        }
    }
}

fn main() {
    caller(Config::new().build());
}

I have no clue what the type XXXX would be. Maybe others have the same issue.
Thanks a lot.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.