https://github.com/reneweb/crius
Crius is a simple hystrix-like circuit breaker for rust.
https://github.com/reneweb/crius
circuit-breaker command rust
Last synced: 3 months ago
JSON representation
Crius is a simple hystrix-like circuit breaker for rust.
- Host: GitHub
- URL: https://github.com/reneweb/crius
- Owner: reneweb
- License: apache-2.0
- Created: 2017-06-04T00:33:59.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-17T16:58:06.000Z (almost 7 years ago)
- Last Synced: 2025-05-23T06:27:29.368Z (5 months ago)
- Topics: circuit-breaker, command, rust
- Language: Rust
- Homepage: https://crates.io/crates/crius
- Size: 45.9 KB
- Stars: 14
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Crius [](https://travis-ci.org/reneweb/crius) [](https://crates.io/crates/crius)
Crius is a simple hystrix-like circuit breaker for rust.
_"In the midst of chaos, there is also opportunity"_
## Usage
### Simple command
```rust
use crius::{command, Config, CriusError};#[derive(PartialEq, Debug)]
struct ExampleError;
impl From for ExampleError {
fn from(_: CriusError) -> Self { ExampleError }
}// Define a simple circuit breaker command:
let mut cmd = command(Config::default(), |n| {
if n > 10 {
Err(ExampleError)
} else {
Ok(n * 2)
}}).unwrap();// and run it with an example input:
let result = cmd.run(10);
assert_eq!(Ok(20), result);
```### Command with fallback
```rust
use crius::{command_with_fallback, Config, CriusError};#[derive(PartialEq, Debug)]
struct ExampleError;
impl From for ExampleError {
fn from(_: CriusError) -> Self { ExampleError }
}let double_if_lt_ten = |n| if n > 10 {
Err(ExampleError)
} else {
Ok(n * 2)
};// Define a simple circuit breaker command:
let mut cmd = command_with_fallback(
Config::default(),
double_if_lt_ten,// Define a fallback:
|_err| 4, // It's always four.
).unwrap();// and run it with an example input:
let result = cmd.run(11);
assert_eq!(Ok(4), result);
```### Command with custom configuration
```rust
use crius::{command, Config, CriusError};let config = *Config::default()
.circuit_open_ms(5000)
.error_threshold(10)
.error_threshold_percentage(50)
.buckets_in_window(100)
.bucket_size_in_ms(1000);let mut cmd = command(config, |n| {
if n > 10 {
Err(ExampleError)
} else {
Ok(n * 2)
}}).unwrap();// and run it with an example input:
let result = cmd.run(10);
assert_eq!(Ok(20), result);
```## Configuration
`circuit_open_ms` - Time in ms commands are rejected after the circuit opened - Default 5000
`error_threshold` - Minimum amount of errors for the circuit to break - Default 10
`error_threshold_percentage` - Minimum error percentage for the circuit to break - Default 50
`buckets_in_window` - Rolling window to track success/error calls, this property defines the amount of buckets in a window (buckets_in_window * bucket_size_in_ms is the overall length in ms of the window) - Default 10
`bucket_size_in_ms` - This property defines the ms a bucket is long, i.e. each x ms a new bucket will be created (buckets_in_window * bucket_size_in_ms is the overall length in ms of the window) - Default 1000
`circuit_breaker_enabled` - Defines if the circuit breaker is enabled or not - Default true