https://github.com/dmexe/failsafe-rs
A circuit breaker implementation for rust
https://github.com/dmexe/failsafe-rs
circuit-breaker rust
Last synced: 3 months ago
JSON representation
A circuit breaker implementation for rust
- Host: GitHub
- URL: https://github.com/dmexe/failsafe-rs
- Owner: dmexe
- License: mit
- Created: 2018-09-03T20:17:31.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-07-16T16:56:17.000Z (almost 2 years ago)
- Last Synced: 2026-01-02T08:32:24.490Z (6 months ago)
- Topics: circuit-breaker, rust
- Language: Rust
- Homepage:
- Size: 145 KB
- Stars: 202
- Watchers: 2
- Forks: 15
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome - dmexe/failsafe-rs - A circuit breaker implementation for rust (<a name="Rust"></a>Rust)
README
# Failsafe
[](https://crates.io/crates/failsafe)
[](https://docs.rs/failsafe)
[](https://circleci.com/gh/dmexe/failsafe-rs)
[](https://ci.appveyor.com/project/dmexe/failsafe-rs/branch/master)
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.
* [https://martinfowler.com/bliki/CircuitBreaker.html](https://martinfowler.com/bliki/CircuitBreaker.html)
* [Read documentation](https://docs.rs/failsafe/1.3.0/failsafe)
# 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:
```toml
failsafe = "1.3.0"
```
# Example
Using default backoff strategy and failure accrual policy.
```rust
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:
```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::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();
```