tower_resilience_fallback/
config.rs1use crate::{FallbackEvent, FallbackStrategy, HandlePredicate, HandleResponsePredicate};
4use tower_resilience_core::{EventListeners, FnListener};
5
6pub struct FallbackConfig<Req, Res, E> {
8 pub(crate) name: String,
9 pub(crate) strategy: FallbackStrategy<Req, Res, E>,
10 pub(crate) handle_predicate: Option<HandlePredicate<E>>,
11 pub(crate) handle_response_predicate: Option<HandleResponsePredicate<Res>>,
12 pub(crate) event_listeners: EventListeners<FallbackEvent>,
13}
14
15pub struct FallbackConfigBuilder<Req, Res, E> {
17 name: String,
18 strategy: Option<FallbackStrategy<Req, Res, E>>,
19 handle_predicate: Option<HandlePredicate<E>>,
20 handle_response_predicate: Option<HandleResponsePredicate<Res>>,
21 event_listeners: EventListeners<FallbackEvent>,
22}
23
24impl<Req, Res, E> Default for FallbackConfigBuilder<Req, Res, E> {
25 fn default() -> Self {
26 Self::new()
27 }
28}
29
30impl<Req, Res, E> FallbackConfigBuilder<Req, Res, E> {
31 pub fn new() -> Self {
33 Self {
34 name: "fallback".to_string(),
35 strategy: None,
36 handle_predicate: None,
37 handle_response_predicate: None,
38 event_listeners: EventListeners::new(),
39 }
40 }
41
42 pub fn name(mut self, name: impl Into<String>) -> Self {
44 self.name = name.into();
45 self
46 }
47
48 pub fn value(mut self, value: Res) -> Self
53 where
54 Res: Clone,
55 {
56 self.strategy = Some(FallbackStrategy::Value(value));
57 self
58 }
59
60 pub fn value_fn<F>(mut self, f: F) -> Self
78 where
79 F: Fn() -> Res + Send + Sync + 'static,
80 {
81 self.strategy = Some(FallbackStrategy::ValueFn(std::sync::Arc::new(f)));
82 self
83 }
84
85 pub fn from_error<F>(mut self, f: F) -> Self
87 where
88 F: Fn(&E) -> Res + Send + Sync + 'static,
89 {
90 self.strategy = Some(FallbackStrategy::FromError(std::sync::Arc::new(f)));
91 self
92 }
93
94 pub fn from_request_error<F>(mut self, f: F) -> Self
96 where
97 F: Fn(&Req, &E) -> Res + Send + Sync + 'static,
98 {
99 self.strategy = Some(FallbackStrategy::FromRequestError(std::sync::Arc::new(f)));
100 self
101 }
102
103 pub fn service<S, Fut>(mut self, service: S) -> Self
105 where
106 S: Fn(Req) -> Fut + Send + Sync + 'static,
107 Fut: std::future::Future<Output = Result<Res, E>> + Send + 'static,
108 {
109 self.strategy = Some(FallbackStrategy::Service(std::sync::Arc::new(move |req| {
110 Box::pin(service(req))
111 })));
112 self
113 }
114
115 pub fn exception<F>(mut self, f: F) -> Self
117 where
118 F: Fn(E) -> E + Send + Sync + 'static,
119 {
120 self.strategy = Some(FallbackStrategy::Exception(std::sync::Arc::new(f)));
121 self
122 }
123
124 pub fn handle<F>(mut self, predicate: F) -> Self
128 where
129 F: Fn(&E) -> bool + Send + Sync + 'static,
130 {
131 self.handle_predicate = Some(std::sync::Arc::new(predicate));
132 self
133 }
134
135 pub fn handle_response<F>(mut self, predicate: F) -> Self
164 where
165 F: Fn(&Res) -> bool + Send + Sync + 'static,
166 {
167 self.handle_response_predicate = Some(std::sync::Arc::new(predicate));
168 self
169 }
170
171 pub fn on_event<F>(mut self, listener: F) -> Self
173 where
174 F: Fn(&FallbackEvent) + Send + Sync + 'static,
175 {
176 self.event_listeners.add(FnListener::new(listener));
177 self
178 }
179
180 pub fn build(self) -> crate::FallbackLayer<Req, Res, E> {
186 let config = FallbackConfig {
187 name: self.name,
188 strategy: self.strategy.expect("fallback strategy must be set"),
189 handle_predicate: self.handle_predicate,
190 handle_response_predicate: self.handle_response_predicate,
191 event_listeners: self.event_listeners,
192 };
193 crate::FallbackLayer::new(config)
194 }
195}