Expand description
The ServiceExt::retry
combinator returns Retry
, which retries following a specified
Policy
.
A Policy
is used to instantiate the per request state and classify whether a request was
successful or not.
The Service::acquire
on Retry
waits to acquire the inner Service::Permit
. The
Service::call
then:
- Calls
Policy::create
to producePolicy::RequestState
. - Uses the inner permit to
Service::call
the innerService
. - Calls
Policy::classify
, with thePolicy::RequestState
from (1). - If
Ok
then returns theService::Response
, ifErr
then returns retries usingServiceExt::oneshot
to obtain the next permit.
§Example
use burger::*;
use http::{Request, Response};
struct RetryServiceUnavailable;
struct State<BReq>(Request<BReq>);
impl<S, BReq, BResp> retry::Policy<S, Request<BReq>> for RetryServiceUnavailable
where
S: Service<Request<BReq>, Response = Response<BResp>> + 'static,
BReq: Clone,
{
type RequestState<'a> = State<BReq>;
fn create(&self, request: &Request<BReq>) -> State<BReq> {
State(request.clone())
}
async fn classify<'a>(
&self,
mut state: State<BReq>,
response: Response<BResp>,
) -> Result<Response<BResp>, (Request<BReq>, State<BReq>)> {
if response.status() != http::StatusCode::SERVICE_UNAVAILABLE {
return Ok(response);
}
if let Some(some) = response.headers().get("retry-after") {
Err((state.0.clone(), state))
} else {
Ok(response)
}
}
}
§Load
The Load::load
on Retry
defers to the inner service.
Structs§
- A wrapper for the
ServiceExt::retry
combinator. - The
Service::Permit
type forRetry
.
Traits§
- A retry policy allows for customization of Retry.