I am trying to use the circuit breaker pattern within my application. I chose to use the failsafe
Rust crate. However, I do not understand how to execute an async function. I could not find any examples, and I am new to Rust. Any help would be greatly appreciated.
This is what I have right now.
use failsafe::futures::CircuitBreaker;
use failsafe::{Config, Error, FailurePolicy, Instrument, StateMachine};
use crate::error;
#[derive(Clone)]
pub struct ConnectionBreaker<P, I>
where
P: FailurePolicy,
I: Instrument
{
inner: StateMachine<P, I>,
}
impl<P, I> ConnectionBreaker<P, I>
where
P: FailurePolicy,
I: Instrument
{
pub fn new(policy: P, instrument: I) -> Self {
Self {
inner: Config::new().failure_policy(policy).instrument(instrument).build(),
}
}
pub async fn execute<F, T, Fut>(&self, f: F) -> error::Result<T>
where
F: FnOnce() -> Fut + Send + 'static,
Fut: Future<Output = error::Result<T>> + Send,
T: Send + 'static,
{
self.inner.call(f).await.map_err(|e| match e {
Error::Inner(e) => e,
Error::Rejected => error::AppError::CircuitBreakerOpen,
})
}
}
The IDE shows the following error on the call
function:
the method `call` exists for struct `StateMachine<P, I>`, but its trait bounds were not satisfied [E0599] method cannot be called on `StateMachine<P, I>` due to unsatisfied trait bounds Note: the following trait bounds were not satisfied: `P: Send` which is required by `StateMachine<P, I>: failsafe::futures::CircuitBreaker` `P: Sync` which is required by `StateMachine<P, I>: failsafe::futures::CircuitBreaker` `I: Send` which is required by `StateMachine<P, I>: failsafe::futures::CircuitBreaker` `I: Sync` which is required by `StateMachine<P, I>: failsafe::futures::CircuitBreaker` Help: items from traits can only be used if the trait is in scope