Skip to main content

FallbackConfigBuilder

Struct FallbackConfigBuilder 

Source
pub struct FallbackConfigBuilder<Req, Res, E> { /* private fields */ }
Expand description

Builder for constructing a FallbackLayer.

Implementations§

Source§

impl<Req, Res, E> FallbackConfigBuilder<Req, Res, E>

Source

pub fn new() -> Self

Creates a new builder with default settings.

Source

pub fn name(self, name: impl Into<String>) -> Self

Sets the name for this fallback instance (used in metrics and events).

Source

pub fn value(self, value: Res) -> Self
where Res: Clone,

Sets a static fallback value.

Note: This requires Res: Clone. If your response type doesn’t implement Clone, use value_fn instead.

Source

pub fn value_fn<F>(self, f: F) -> Self
where F: Fn() -> Res + Send + Sync + 'static,

Sets a fallback value generator function.

Unlike value, this doesn’t require Res: Clone since the function generates a fresh value for each fallback.

§Example
use tower_resilience_fallback::FallbackLayer;

let layer: FallbackLayer<String, MyResponse, MyError> = FallbackLayer::builder()
    .value_fn(|| MyResponse { data: vec![0; 1024] })
    .build();
Source

pub fn from_error<F>(self, f: F) -> Self
where F: Fn(&E) -> Res + Send + Sync + 'static,

Sets a fallback function that computes a response from the error.

Source

pub fn from_request_error<F>(self, f: F) -> Self
where F: Fn(&Req, &E) -> Res + Send + Sync + 'static,

Sets a fallback function that has access to both request and error.

Source

pub fn service<S, Fut>(self, service: S) -> Self
where S: Fn(Req) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Res, E>> + Send + 'static,

Sets a backup service to call on failure.

Source

pub fn exception<F>(self, f: F) -> Self
where F: Fn(E) -> E + Send + Sync + 'static,

Sets an error transformation function.

Source

pub fn handle<F>(self, predicate: F) -> Self
where F: Fn(&E) -> bool + Send + Sync + 'static,

Only trigger fallback for errors matching this predicate.

Errors that don’t match the predicate will be propagated as-is.

Source

pub fn handle_response<F>(self, predicate: F) -> Self
where F: Fn(&Res) -> bool + Send + Sync + 'static,

Trigger fallback for successful responses matching this predicate.

When this predicate returns true for a response, the fallback strategy is applied as if the service had returned an error. This is useful when errors are encoded inside successful responses, such as:

  • JSON-RPC error codes in the response body
  • HTTP 200 responses containing error payloads
  • Responses indicating degraded or stale data
§Example
use tower_resilience_fallback::FallbackLayer;

#[derive(Debug, Clone)]
struct MyError;

#[derive(Clone)]
struct ApiResponse {
    is_stale: bool,
    data: String,
}

let layer: FallbackLayer<String, ApiResponse, MyError> = FallbackLayer::builder()
    .value_fn(|| ApiResponse { is_stale: false, data: "default".to_string() })
    .handle_response(|resp: &ApiResponse| resp.is_stale)
    .build();
Source

pub fn on_event<F>(self, listener: F) -> Self
where F: Fn(&FallbackEvent) + Send + Sync + 'static,

Adds an event listener.

Source

pub fn build(self) -> FallbackLayer<Req, Res, E>

Builds the fallback layer.

§Panics

Panics if no fallback strategy was configured.

Trait Implementations§

Source§

impl<Req, Res, E> Default for FallbackConfigBuilder<Req, Res, E>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Req, Res, E> Freeze for FallbackConfigBuilder<Req, Res, E>
where Res: Freeze,

§

impl<Req, Res, E> !RefUnwindSafe for FallbackConfigBuilder<Req, Res, E>

§

impl<Req, Res, E> Send for FallbackConfigBuilder<Req, Res, E>
where Res: Send,

§

impl<Req, Res, E> Sync for FallbackConfigBuilder<Req, Res, E>
where Res: Sync,

§

impl<Req, Res, E> Unpin for FallbackConfigBuilder<Req, Res, E>
where Res: Unpin,

§

impl<Req, Res, E> UnsafeUnpin for FallbackConfigBuilder<Req, Res, E>
where Res: UnsafeUnpin,

§

impl<Req, Res, E> !UnwindSafe for FallbackConfigBuilder<Req, Res, E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.