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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//! Fallback resilience middleware for services, applications, and libraries.
//!
//! This module provides a fallback mechanism that replaces invalid service output
//! with a user-defined alternative. The primary types are [`Fallback`] and
//! [`FallbackLayer`]:
//!
//! - [`Fallback`] is the middleware that wraps an inner service and applies fallback logic
//! - [`FallbackLayer`] is used to configure and construct the fallback middleware
//!
//! # Quick Start
//!
//! ```rust
//! # use tick::Clock;
//! # use layered::{Execute, Service, Stack};
//! # use seatbelt::ResilienceContext;
//! # use seatbelt::fallback::Fallback;
//! # async fn example(clock: Clock) {
//! let context = ResilienceContext::new(&clock).name("my_service");
//!
//! let stack = (
//! Fallback::layer("fallback", &context)
//! .should_fallback(|output: &String| output == "bad")
//! .fallback(|_output: String, _args| "replacement".to_string()),
//! Execute::new(my_operation),
//! );
//!
//! let service = stack.into_service();
//! let result = service.execute("input".to_string()).await;
//! # }
//! # async fn my_operation(input: String) -> String { input }
//! ```
//!
//! # Configuration
//!
//! The [`FallbackLayer`] uses a type-state pattern to enforce that all required
//! properties are configured before the layer can be built. This compile-time
//! safety ensures that you cannot accidentally create a fallback layer without
//! properly specifying the predicate and the fallback action:
//!
//! - [`should_fallback`][FallbackLayer::should_fallback]: Required predicate that decides whether the output needs replacing
//! - [`fallback`][FallbackLayer::fallback] or [`fallback_async`][FallbackLayer::fallback_async]: Required action that produces the replacement output
//!
//! Each fallback layer requires an identifier for telemetry purposes. This
//! identifier should use `snake_case` naming convention to maintain consistency
//! across the codebase.
//!
//! # Sync and Async Fallback
//!
//! Fallback actions can be either synchronous or asynchronous:
//!
//! - [`fallback`][FallbackLayer::fallback]: Synchronous - the closure runs inline.
//! - [`fallback_async`][FallbackLayer::fallback_async]: Asynchronous - the closure
//! returns a `Future` that is `.await`ed.
//! - [`fallback_output`][FallbackLayer::fallback_output]: Convenience shorthand that
//! clones a fixed value on every invocation.
//!
//! Both [`fallback`][FallbackLayer::fallback] and [`fallback_async`][FallbackLayer::fallback_async]
//! receive the original (invalid) output and [`FallbackActionArgs`] with additional context,
//! and produce a replacement.
//!
//! # Defaults
//!
//! | Parameter | Default Value | Description | Configured By |
//! |-----------|---------------|-------------|---------------|
//! | Predicate | `None` (required) | Decides when fallback is needed | [`should_fallback`][FallbackLayer::should_fallback] |
//! | Action | `None` (required) | Produces the replacement output | [`fallback`][FallbackLayer::fallback], [`fallback_async`][FallbackLayer::fallback_async], [`fallback_output`][FallbackLayer::fallback_output] |
//! | Enable condition | Always enabled | Fallback is applied to all requests | [`enable_if`][FallbackLayer::enable_if], [`enable_always`][FallbackLayer::enable_always], [`disable`][FallbackLayer::disable] |
//!
//! # Thread Safety
//!
//! The [`Fallback`] type is thread-safe and implements both `Send` and `Sync` as
//! enforced by the `Service` trait it implements.
//!
//! # Telemetry
//!
//! ## Metrics
//!
//! - **Metric**: `resilience.event` (counter)
//! - **When**: Emitted when a fallback action is invoked
//! - **Attributes**:
//! - `resilience.pipeline.name`: Pipeline identifier from [`ResilienceContext::name`][crate::ResilienceContext::name]
//! - `resilience.strategy.name`: Fallback identifier from [`Fallback::layer`]
//! - `resilience.event.name`: Always `fallback`
//!
//! # Examples
//!
//! ## Basic Usage
//!
//! ```rust
//! # use tick::Clock;
//! # use layered::{Execute, Service, Stack};
//! # use seatbelt::ResilienceContext;
//! # use seatbelt::fallback::Fallback;
//! # async fn example(clock: Clock) {
//! let context = ResilienceContext::new(&clock);
//!
//! let stack = (
//! Fallback::layer("my_fallback", &context)
//! .should_fallback(|output: &String| output.is_empty())
//! .fallback_output("default_value".to_string()),
//! Execute::new(execute_unreliable_operation),
//! );
//!
//! let service = stack.into_service();
//! let result = service.execute("input".to_string()).await;
//! # }
//! # async fn execute_unreliable_operation(input: String) -> String { input }
//! ```
//!
//! ## Async Fallback
//!
//! ```rust
//! # use tick::Clock;
//! # use layered::{Execute, Service, Stack};
//! # use seatbelt::ResilienceContext;
//! # use seatbelt::fallback::Fallback;
//! # async fn example(clock: Clock) {
//! let context = ResilienceContext::new(&clock);
//!
//! let stack = (
//! Fallback::layer("my_fallback", &context)
//! .should_fallback(|output: &String| output == "error")
//! .fallback_async(|_output: String, _args| async {
//! "fetched_from_cache".to_string()
//! }),
//! Execute::new(execute_unreliable_operation),
//! );
//!
//! let service = stack.into_service();
//! let result = service.execute("input".to_string()).await;
//! # }
//! # async fn execute_unreliable_operation(input: String) -> String { input }
//! ```
//!
//! ## With Observability
//!
//! ```rust
//! # use tick::Clock;
//! # use layered::{Execute, Service, Stack};
//! # use seatbelt::ResilienceContext;
//! # use seatbelt::fallback::Fallback;
//! # async fn example(clock: Clock) {
//! let context = ResilienceContext::new(&clock);
//!
//! let stack = (
//! Fallback::layer("my_fallback", &context)
//! .should_fallback(|output: &String| output == "bad")
//! .fallback(|_output: String, _args| "safe_default".to_string())
//! .enable_if(|input: &String| !input.starts_with("bypass_")),
//! Execute::new(execute_unreliable_operation),
//! );
//!
//! let service = stack.into_service();
//! let result = service.execute("input".to_string()).await;
//! # }
//! # async fn execute_unreliable_operation(input: String) -> String { input }
//! ```
pub use FallbackActionArgs;
pub use ;
pub use FallbackLayer;
pub use Fallback;
pub use FallbackFuture;
pub use FallbackShared;