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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//! Composable resilience and fault-tolerance middleware for Tower services.
//!
//! `tower-resilience` provides a collection of resilience patterns inspired by
//! [Resilience4j](https://resilience4j.readme.io/). Each pattern is available as both an
//! individual crate and as a feature in this meta-crate.
//!
//! # Quick Start
//!
//! ```toml
//! [dependencies]
//! tower-resilience = { version = "0.9", features = ["circuitbreaker", "bulkhead"] }
//! ```
//!
//! # Presets: Get Started Immediately
//!
//! Every pattern includes **preset configurations** with sensible defaults.
//! Start immediately without tuning parameters - customize later when needed:
//!
//! ```rust,no_run
//! # #[cfg(all(feature = "retry", feature = "circuitbreaker", feature = "ratelimiter", feature = "bulkhead"))]
//! # {
//! use tower_resilience::retry::RetryLayer;
//! use tower_resilience::circuitbreaker::CircuitBreakerLayer;
//! use tower_resilience::ratelimiter::RateLimiterLayer;
//! use tower_resilience::bulkhead::BulkheadLayer;
//!
//! # #[derive(Debug, Clone)]
//! # struct MyError;
//! // Retry: 3 attempts with 100ms exponential backoff
//! let retry = RetryLayer::<(), (), MyError>::exponential_backoff().build();
//!
//! // Circuit breaker: balanced defaults (50% threshold, 100 call window)
//! let breaker = CircuitBreakerLayer::standard().build();
//!
//! // Rate limiter: 100 requests per second
//! let limiter = RateLimiterLayer::per_second(100).build();
//!
//! // Bulkhead: 50 concurrent calls
//! let bulkhead = BulkheadLayer::medium().build();
//! # }
//! ```
//!
//! ## Available Presets
//!
//! | Pattern | Presets |
//! |---------|---------|
//! | **Bulkhead** | [`small()`], [`medium()`], [`large()`] |
//! | **Circuit Breaker** | [`standard()`], [`fast_fail()`], [`tolerant()`] |
//! | **Hedge** | [`conservative()`][h_conservative], [`standard()`][h_standard], [`aggressive()`][h_aggressive] |
//! | **Rate Limiter** | [`per_second(n)`], [`per_minute(n)`], [`burst(rate, size)`] |
//! | **Retry** | [`exponential_backoff()`], [`aggressive()`], [`conservative()`] |
//! | **Time Limiter** | [`fast()`], [`standard()`][tl_standard], [`slow()`], [`streaming()`] |
//!
//! Presets return builders, so you can customize any setting:
//!
//! ```rust,no_run
//! # #[cfg(feature = "circuitbreaker")]
//! # {
//! use tower_resilience::circuitbreaker::CircuitBreakerLayer;
//! use std::time::Duration;
//!
//! let breaker = CircuitBreakerLayer::fast_fail()
//! .name("payment-api")
//! .wait_duration_in_open(Duration::from_secs(30))
//! .build();
//! # }
//! ```
//!
//! [`small()`]: bulkhead::BulkheadLayer::small
//! [`medium()`]: bulkhead::BulkheadLayer::medium
//! [`large()`]: bulkhead::BulkheadLayer::large
//! [`standard()`]: circuitbreaker::CircuitBreakerLayer::standard
//! [`fast_fail()`]: circuitbreaker::CircuitBreakerLayer::fast_fail
//! [`tolerant()`]: circuitbreaker::CircuitBreakerLayer::tolerant
//! [h_conservative]: hedge::HedgeLayer::conservative
//! [h_standard]: hedge::HedgeLayer::standard
//! [h_aggressive]: hedge::HedgeLayer::aggressive
//! [`per_second(n)`]: ratelimiter::RateLimiterLayer::per_second
//! [`per_minute(n)`]: ratelimiter::RateLimiterLayer::per_minute
//! [`burst(rate, size)`]: ratelimiter::RateLimiterLayer::burst
//! [`exponential_backoff()`]: retry::RetryLayer::exponential_backoff
//! [`aggressive()`]: retry::RetryLayer::aggressive
//! [`conservative()`]: retry::RetryLayer::conservative
//! [`fast()`]: timelimiter::TimeLimiterLayer::fast
//! [tl_standard]: timelimiter::TimeLimiterLayer::standard
//! [`slow()`]: timelimiter::TimeLimiterLayer::slow
//! [`streaming()`]: timelimiter::TimeLimiterLayer::streaming
//!
//! # Resilience Patterns
//!
//! - **[Adaptive]** - Dynamic concurrency limiting using AIMD or Vegas algorithms
//! - **[Bulkhead]** - Isolates resources to prevent system-wide failures
//! - **[Cache]** - Response memoization to reduce load
//! - **[Circuit Breaker]** - Prevents cascading failures by stopping calls to failing services
//! - **[Coalesce]** - Deduplicates concurrent identical requests (singleflight)
//! - **[Executor]** - Delegates request processing to dedicated executors
//! - **[Fallback]** - Provides alternative responses when services fail
//! - **[Hedge]** - Reduces tail latency by firing parallel requests
//! - **[Health Check]** - Proactive health monitoring with intelligent resource selection
//! - **[Outlier Detection]** - Fleet-aware instance ejection based on health tracking
//! - **[Rate Limiter]** - Controls request rate to protect services
//! - **[Reconnect]** - Automatic reconnection with configurable backoff strategies
//! - **[Retry]** - Intelligent retry with exponential backoff and jitter
//! - **[Router]** - Weighted traffic routing for canary deployments and progressive rollout
//! - **[Time Limiter]** - Advanced timeout handling with cancellation support
//!
//! [Adaptive]: https://docs.rs/tower-resilience-adaptive
//! [Bulkhead]: https://docs.rs/tower-resilience-bulkhead
//! [Cache]: https://docs.rs/tower-resilience-cache
//! [Circuit Breaker]: https://docs.rs/tower-resilience-circuitbreaker
//! [Coalesce]: https://docs.rs/tower-resilience-coalesce
//! [Executor]: https://docs.rs/tower-resilience-executor
//! [Fallback]: https://docs.rs/tower-resilience-fallback
//! [Hedge]: https://docs.rs/tower-resilience-hedge
//! [Health Check]: https://docs.rs/tower-resilience-healthcheck
//! [Outlier Detection]: https://docs.rs/tower-resilience-outlier
//! [Rate Limiter]: https://docs.rs/tower-resilience-ratelimiter
//! [Reconnect]: https://docs.rs/tower-resilience-reconnect
//! [Retry]: https://docs.rs/tower-resilience-retry
//! [Router]: https://docs.rs/tower-resilience-router
//! [Time Limiter]: https://docs.rs/tower-resilience-timelimiter
//!
//! # Documentation Guides
//!
//! ## Getting Started
//!
//! - **[Tower Primer](tower_primer)** - Introduction to Tower concepts (Service, Layer, composition)
//! - **[Pattern Guides](patterns)** - Detailed guides for each pattern with examples and anti-patterns
//! - **[Composition Guide](composition)** - How to combine patterns effectively
//! - **[Use Cases](use_cases)** - Real-world scenarios and recommendations
//!
//! ## Observability
//!
//! - **[Metrics](observability::metrics)** - Prometheus metrics for all patterns
//! - **[Tracing](observability::tracing_guide)** - Structured logging setup
//! - **[Events](observability::events)** - Custom event listeners
//!
//! # Example
//!
//! ```rust,no_run
//! # #[cfg(all(feature = "circuitbreaker", feature = "retry"))]
//! # {
//! use tower::{ServiceBuilder, Layer};
//! use tower_resilience::circuitbreaker::CircuitBreakerLayer;
//! use tower_resilience::retry::RetryLayer;
//! use std::time::Duration;
//!
//! # #[derive(Debug, Clone)]
//! # struct MyError;
//! # impl std::fmt::Display for MyError {
//! # fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
//! # write!(f, "error")
//! # }
//! # }
//! # impl std::error::Error for MyError {}
//! # async fn example() {
//! # let http_client = tower::service_fn(|_req: ()| async { Ok::<_, MyError>(()) });
//! // Build a resilient HTTP client
//! let circuit_breaker = CircuitBreakerLayer::builder()
//! .name("api-client")
//! .failure_rate_threshold(0.5)
//! .sliding_window_size(100)
//! .build();
//!
//! let retry = RetryLayer::<(), (), MyError>::builder()
//! .name("api-retry")
//! .max_attempts(3)
//! .exponential_backoff(Duration::from_millis(100))
//! .build();
//!
//! // Compose manually for reliability
//! let resilient_client = retry.layer(http_client);
//! let resilient_client = circuit_breaker.layer(resilient_client);
//! # }
//! # }
//! ```
//!
//! # Performance
//!
//! All patterns have low overhead in the happy path:
//!
//! - Retry: ~80-100ns (lightest)
//! - Time Limiter: ~107ns
//! - Rate Limiter: ~124ns
//! - Bulkhead: ~162ns
//! - Cache (hit): ~250ns
//! - Circuit Breaker: ~298ns (heaviest)
//!
//! See [benchmarks] for detailed measurements.
//!
//! [benchmarks]: https://github.com/joshrotenberg/tower-resilience#performance
//!
//! # Error Handling
//!
//! When composing multiple resilience layers, each layer has its own error type.
//! [`core::ResilienceError<E>`] unifies these into a single error type, eliminating
//! boilerplate `From` implementations.
//!
//! ## Quick Setup
//!
//! ```rust
//! use tower_resilience_core::ResilienceError;
//!
//! // Your application error
//! #[derive(Debug, Clone)]
//! enum AppError {
//! DatabaseDown,
//! InvalidRequest,
//! }
//!
//! impl std::fmt::Display for AppError {
//! fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
//! match self {
//! AppError::DatabaseDown => write!(f, "Database down"),
//! AppError::InvalidRequest => write!(f, "Invalid request"),
//! }
//! }
//! }
//!
//! impl std::error::Error for AppError {}
//!
//! // Use ResilienceError as your service error type - zero From impls needed!
//! type ServiceError = ResilienceError<AppError>;
//! ```
//!
//! ## Pattern Matching
//!
//! ```rust
//! use tower_resilience_core::ResilienceError;
//!
//! # #[derive(Debug)]
//! # struct AppError;
//! # impl std::fmt::Display for AppError {
//! # fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Ok(()) }
//! # }
//! # impl std::error::Error for AppError {}
//! fn handle_error(error: ResilienceError<AppError>) {
//! match error {
//! ResilienceError::Timeout { layer } => {
//! eprintln!("Timeout in {}", layer);
//! }
//! ResilienceError::CircuitOpen { name } => {
//! eprintln!("Circuit breaker {:?} is open", name);
//! }
//! ResilienceError::BulkheadFull { concurrent_calls, max_concurrent } => {
//! eprintln!("Bulkhead full: {}/{}", concurrent_calls, max_concurrent);
//! }
//! ResilienceError::RateLimited { retry_after } => {
//! eprintln!("Rate limited, retry after {:?}", retry_after);
//! }
//! ResilienceError::InstanceEjected { name } => {
//! eprintln!("Instance '{}' ejected by outlier detection", name);
//! }
//! ResilienceError::Application(app_err) => {
//! eprintln!("Application error: {}", app_err);
//! }
//! }
//! }
//! ```
//!
//! ## Helper Methods
//!
//! ```rust
//! # use tower_resilience_core::ResilienceError;
//! # #[derive(Debug)]
//! # struct AppError;
//! # impl std::fmt::Display for AppError {
//! # fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Ok(()) }
//! # }
//! # impl std::error::Error for AppError {}
//! # fn example(error: ResilienceError<AppError>) {
//! // Quickly check error categories
//! if error.is_timeout() {
//! // Handle timeout from TimeLimiter or Bulkhead
//! } else if error.is_circuit_open() {
//! // Circuit breaker protecting the system
//! } else if error.is_rate_limited() {
//! // Backpressure - slow down
//! } else if error.is_application() {
//! // Extract the underlying application error
//! let app_err = error.application_error();
//! }
//! # }
//! ```
//!
//! For complete documentation, see [`core::ResilienceError`].
// Documentation modules
// Re-export core (always available)
pub use tower_resilience_core as core;
// Re-export patterns based on features (alphabetical)
pub use tower_resilience_adaptive as adaptive;
pub use tower_resilience_bulkhead as bulkhead;
pub use tower_resilience_cache as cache;
pub use tower_resilience_chaos as chaos;
pub use tower_resilience_circuitbreaker as circuitbreaker;
pub use tower_resilience_coalesce as coalesce;
pub use tower_resilience_executor as executor;
pub use tower_resilience_fallback as fallback;
pub use tower_resilience_hedge as hedge;
pub use tower_resilience_healthcheck as healthcheck;
pub use tower_resilience_outlier as outlier;
pub use tower_resilience_ratelimiter as ratelimiter;
pub use tower_resilience_reconnect as reconnect;
pub use tower_resilience_retry as retry;
pub use tower_resilience_router as router;
pub use tower_resilience_timelimiter as timelimiter;
// Re-export unified error layer types
pub use ;