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§
sourcetype RequestState<'a>
type RequestState<'a>
The type of the request state.
Required Methods§
sourcefn create(&self, request: &Request) -> Self::RequestState<'_>
fn create(&self, request: &Request) -> Self::RequestState<'_>
Creates a new RequestState.
sourceasync fn classify<'a>(
&self,
state: Self::RequestState<'a>,
response: S::Response
) -> Result<S::Response, (Request, Self::RequestState<'a>)>
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.