Trait burger::retry::Policy

source ·
pub trait Policy<S, Request>
where S: Service<Request>,
{ type RequestState<'a>; // Required methods fn create(&self, request: &Request) -> Self::RequestState<'_>; async fn classify<'a>( &self, state: Self::RequestState<'a>, response: S::Response ) -> Result<S::Response, (Request, Self::RequestState<'a>)>; }
Expand description

A retry policy allows for customization of Retry.

§Example

use burger::*;
use http::{Request, Response};

struct FiniteRetries(usize);

struct Attempts<'a, BReq> {
    max: &'a usize,
    request: Request<BReq>,
    attempted: usize,
}

impl<S, BReq, BResp> retry::Policy<S, Request<BReq>> for FiniteRetries
where
    S: Service<Request<BReq>, Response = http::Response<BResp>>,
    BReq: Clone,
{
    type RequestState<'a> = Attempts<'a, BReq>;

    fn create(&self, request: &Request<BReq>) -> Self::RequestState<'_> {
        Attempts {
            max: &self.0,
            request: request.clone(),
            attempted: 0,
        }
    }

    async fn classify<'a>(
        &self,
        mut state: Self::RequestState<'a>,
        response: Response<BResp>,
    ) -> Result<Response<BResp>, (Request<BReq>, Self::RequestState<'a>)> {
        if response.status() == http::status::StatusCode::OK {
            return Ok(response);
        }

        state.attempted += 1;
        if state.attempted >= *state.max {
            return Ok(response);
        }

        Err((state.request.clone(), state))
    }
}

Required Associated Types§

source

type RequestState<'a>

The type of the request state.

Required Methods§

source

fn create(&self, request: &Request) -> Self::RequestState<'_>

Creates a new RequestState.

source

async fn classify<'a>( &self, state: Self::RequestState<'a>, response: S::Response ) -> Result<S::Response, (Request, Self::RequestState<'a>)>

Classifies the response, determining whether it was successful. On success returns Ok Service::Response, on failure returns the next request and the updated Policy::RequestState.

Object Safety§

This trait is not object safe.

Implementors§