tower_fault/
lib.rs

1#![warn(missing_debug_implementations, missing_docs, unreachable_pub)]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3//! # Fault injection utilities for `tower`
4//!
5//! This crate provides [`tower::Layer`]s that can be used to inject various
6//! faults into a [`tower::Service`].
7//!
8//! ## Layers
9//!
10//! You can use the following layers to inject faults into a service:
11//!
12//! * [`ErrorLayer`](error/struct.ErrorLayer.html) - randomly inject errors into a service.
13//! * [`LatencyLayer`](latency/struct.LatencyLayer.html) - randomly add latency into a service.
14//!
15//! ## Example
16//!
17//! ```rust
18//! use tower_fault::{
19//! error::ErrorLayer,
20//! latency::LatencyLayer,
21//! };
22//! use tower::{service_fn, ServiceBuilder};
23//!
24//! # struct MyRequest {
25//! #     value: u64,
26//! # }
27//!
28//! # async fn my_service(req: MyRequest) -> Result<(), String> {
29//! #     Ok(())
30//! # }
31//!
32//! // LatencyLayer with a 10% probability of injecting 200 to 500 milliseconds
33//! // of latency.
34//! let latency_layer = LatencyLayer::new(0.1, 200..500);
35//!
36//! // ErrorLayer that injects an error if the request value is greater than 10.
37//! let error_layer = ErrorLayer::new(
38//!     |req: &MyRequest| req.value > 10,
39//!     |_: &MyRequest| String::from("error")
40//! );
41//!
42//! let service = ServiceBuilder::new()
43//!     .layer(latency_layer)
44//!     .layer(error_layer)
45//!     .service(service_fn(my_service));
46//! ```
47
48#[cfg(feature = "error")]
49#[cfg_attr(docsrs, doc(cfg(feature = "error")))]
50pub mod error;
51
52#[cfg(feature = "latency")]
53#[cfg_attr(docsrs, doc(cfg(feature = "latency")))]
54pub mod latency;
55
56pub mod decider;
57
58#[cfg(test)]
59mod test_utils;