Skip to main content

Policy

Trait Policy 

Source
pub trait Policy<E>: Send + Sync {
    // Required method
    fn execute<'life0, 'async_trait, F, Fut, T>(
        &'life0 self,
        f: F,
    ) -> Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'async_trait>>
       where F: Fn() -> Fut + Send + Sync + 'async_trait,
             Fut: Future<Output = Result<T, E>> + Send + 'async_trait,
             T: Send + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

The core trait implemented by all resilience policies.

Policy<E> provides a uniform interface for wrapping async operations with resilience patterns like retry, circuit breaker, timeout, etc.

§Type Parameter

  • E - The error type that the policy can produce

§Examples

Using a policy to execute an operation:

use do_over::{policy::Policy, retry::RetryPolicy};
use std::time::Duration;

let policy = RetryPolicy::fixed(3, Duration::from_millis(100));

let result = policy.execute(|| async {
    // Your async operation here
    Ok::<_, std::io::Error>("success")
}).await?;

§Implementing Policy

To create a custom policy, implement this trait:

use do_over::policy::Policy;
use std::future::Future;

struct LoggingPolicy;

#[async_trait::async_trait]
impl<E: Send + Sync> Policy<E> for LoggingPolicy {
    async fn execute<F, Fut, T>(&self, f: F) -> Result<T, E>
    where
        F: Fn() -> Fut + Send + Sync,
        Fut: Future<Output = Result<T, E>> + Send,
        T: Send,
    {
        println!("Executing operation...");
        let result = f().await;
        println!("Operation completed");
        result
    }
}

Required Methods§

Source

fn execute<'life0, 'async_trait, F, Fut, T>( &'life0 self, f: F, ) -> Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'async_trait>>
where F: Fn() -> Fut + Send + Sync + 'async_trait, Fut: Future<Output = Result<T, E>> + Send + 'async_trait, T: Send + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Execute an async operation with this policy’s resilience behavior.

§Arguments
  • f - A closure that returns a Future producing Result<T, E>
§Returns

The result of the operation, potentially modified by the policy (e.g., retried, timed out, etc.)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<E> Policy<DoOverError<E>> for Bulkhead
where E: Send + Sync,

Source§

impl<E> Policy<DoOverError<E>> for CircuitBreaker
where E: Send + Sync,

Source§

impl<E> Policy<DoOverError<E>> for Hedge
where E: Send + Sync,

Source§

impl<E> Policy<DoOverError<E>> for RateLimiter
where E: Send + Sync,

Source§

impl<E> Policy<DoOverError<E>> for TimeoutPolicy
where E: Send + Sync,

Source§

impl<E> Policy<E> for RetryPolicy
where E: Send + Sync,

Source§

impl<F, T, E> Policy<E> for Fallback<F, T>
where F: Fn() -> T + Send + Sync, T: Send + Sync + Clone, E: Send + Sync,

Source§

impl<O, I, E> Policy<E> for Wrap<O, I>
where O: Policy<E>, I: Policy<E>, E: Send + Sync,

Source§

impl<T, E> Policy<E> for Cache<T>
where T: Clone + Send + Sync, E: Send + Sync,

Source§

impl<T, E> Policy<E> for FallbackValue<T>
where T: Clone + Send + Sync, E: Send + Sync,