1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! # Fault injection utilities for `tower`
//!
//! This crate provides [`tower::Layer`]s that can be used to inject various
//! faults into a [`tower::Service`].
//!
//! ## Layers
//!
//! You can use the following layers to inject faults into a service:
//!
//! * [`ErrorLayer`](error/struct.ErrorLayer.html) - randomly inject errors into a service.
//! * [`LatencyLayer`](latency/struct.LatencyLayer.html) - randomly add latency into a service.
//!
//! ## Example
//!
//! ```rust
//! 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)
//! .layer(error_layer)
//! .service(service_fn(my_service));
//! ```