tower-fault

Crates.iotower-fault
lib.rstower-fault
version0.0.5
sourcesrc
created_at2022-02-24 16:26:13.450127
updated_at2022-02-24 16:26:13.450127
descriptionTower layers for fault injection
homepage
repositoryhttps://github.com/nmoutschen/tower-fault
max_upload_size
id538622
size49,443
Nicolas Moutschen (nmoutschen)

documentation

README

tower-fault Latest Version

tower-fault is a library for injecting various faults into a tower::Service.

Layers

You can use the following layers to inject faults into a service:

  • ErrorLayer - randomly inject errors into a service.
  • LatencyLayer - randomly add latency into a service.

Example usage

use tower_fault::{
    error::ErrorLayer,
    latency::LatencyLayer,
};
use tower::{service_fn, ServiceBuilder};

struct MyRequest {
    value: u64
}

async fn my_service(req: MyRequest) -> Result<(), String> {
    Ok(())
}

// LatencyLayer with a 10% probability of injecting 200 to 500 milliseconds
// of latency.
let latency_layer = LatencyLayer::new(0.1, 200..500);

// ErrorLayer that injects an error if the request value is greater than 10.
let error_layer = ErrorLayer::new(|req: &MyRequest| req.value > 10, |_: &MyRequest| String::from("error"));

let service = ServiceBuilder::new()
    .layer(latency_layer)
    .service(service_fn(my_service));
Commit count: 20

cargo fmt