Code Monkey home page Code Monkey logo

crius's Introduction

Crius Build Status Cargo Version

Crius is a simple hystrix-like circuit breaker for rust.

"In the midst of chaos, there is also opportunity"

Usage

Simple command

use crius::{command, Config, CriusError};

#[derive(PartialEq, Debug)]
struct ExampleError;
impl From<CriusError> 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

use crius::{command_with_fallback, Config, CriusError};

#[derive(PartialEq, Debug)]
struct ExampleError;
impl From<CriusError> 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

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

crius's People

Contributors

reneweb avatar tazjin avatar

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.