2019-04-24 15:24:39 -07:00
|
|
|
//! Future types
|
|
|
|
|
2020-03-31 10:10:47 -07:00
|
|
|
use super::{Policy, Retry};
|
2019-09-09 12:10:46 -07:00
|
|
|
use futures_core::ready;
|
|
|
|
use pin_project::{pin_project, project};
|
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2019-04-24 15:24:39 -07:00
|
|
|
use tower_service::Service;
|
|
|
|
|
|
|
|
/// The `Future` returned by a `Retry` service.
|
2019-09-09 12:10:46 -07:00
|
|
|
#[pin_project]
|
2019-04-24 15:24:39 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ResponseFuture<P, S, Request>
|
|
|
|
where
|
|
|
|
P: Policy<Request, S::Response, S::Error>,
|
|
|
|
S: Service<Request>,
|
|
|
|
{
|
|
|
|
request: Option<Request>,
|
2019-09-09 12:10:46 -07:00
|
|
|
#[pin]
|
2019-04-24 15:24:39 -07:00
|
|
|
retry: Retry<P, S>,
|
2019-09-09 12:10:46 -07:00
|
|
|
#[pin]
|
|
|
|
state: State<S::Future, P::Future>,
|
2019-04-24 15:24:39 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 12:10:46 -07:00
|
|
|
#[pin_project]
|
2019-04-24 15:24:39 -07:00
|
|
|
#[derive(Debug)]
|
2019-09-09 12:10:46 -07:00
|
|
|
enum State<F, P> {
|
2019-04-24 15:24:39 -07:00
|
|
|
/// Polling the future from `Service::call`
|
2019-09-09 12:10:46 -07:00
|
|
|
Called(#[pin] F),
|
2019-04-24 15:24:39 -07:00
|
|
|
/// Polling the future from `Policy::retry`
|
2019-09-09 12:10:46 -07:00
|
|
|
Checking(#[pin] P),
|
2019-04-24 15:24:39 -07:00
|
|
|
/// Polling `Service::poll_ready` after `Checking` was OK.
|
|
|
|
Retrying,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P, S, Request> ResponseFuture<P, S, Request>
|
|
|
|
where
|
|
|
|
P: Policy<Request, S::Response, S::Error>,
|
|
|
|
S: Service<Request>,
|
|
|
|
{
|
|
|
|
pub(crate) fn new(
|
|
|
|
request: Option<Request>,
|
|
|
|
retry: Retry<P, S>,
|
|
|
|
future: S::Future,
|
|
|
|
) -> ResponseFuture<P, S, Request> {
|
|
|
|
ResponseFuture {
|
|
|
|
request,
|
|
|
|
retry,
|
|
|
|
state: State::Called(future),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P, S, Request> Future for ResponseFuture<P, S, Request>
|
|
|
|
where
|
|
|
|
P: Policy<Request, S::Response, S::Error> + Clone,
|
|
|
|
S: Service<Request> + Clone,
|
|
|
|
{
|
2019-09-09 12:10:46 -07:00
|
|
|
type Output = Result<S::Response, S::Error>;
|
2019-04-24 15:24:39 -07:00
|
|
|
|
2019-09-09 12:10:46 -07:00
|
|
|
#[project]
|
2019-09-30 11:58:27 -07:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-09-09 12:10:46 -07:00
|
|
|
let mut this = self.project();
|
2019-04-24 15:24:39 -07:00
|
|
|
|
2019-09-09 12:10:46 -07:00
|
|
|
loop {
|
|
|
|
#[project]
|
2019-09-30 11:58:27 -07:00
|
|
|
match this.state.as_mut().project() {
|
2019-09-09 12:10:46 -07:00
|
|
|
State::Called(future) => {
|
|
|
|
let result = ready!(future.poll(cx));
|
|
|
|
if let Some(ref req) = this.request {
|
|
|
|
match this.retry.policy.retry(req, result.as_ref()) {
|
|
|
|
Some(checking) => {
|
|
|
|
this.state.set(State::Checking(checking));
|
|
|
|
}
|
|
|
|
None => return Poll::Ready(result),
|
2019-04-24 15:24:39 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// request wasn't cloned, so no way to retry it
|
2019-09-09 12:10:46 -07:00
|
|
|
return Poll::Ready(result);
|
2019-04-24 15:24:39 -07:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 12:10:46 -07:00
|
|
|
State::Checking(future) => {
|
2019-09-30 11:58:27 -07:00
|
|
|
this.retry
|
|
|
|
.as_mut()
|
|
|
|
.project()
|
|
|
|
.policy
|
|
|
|
.set(ready!(future.poll(cx)));
|
2019-09-09 12:10:46 -07:00
|
|
|
this.state.set(State::Retrying);
|
2019-04-24 15:24:39 -07:00
|
|
|
}
|
|
|
|
State::Retrying => {
|
2019-09-09 12:10:46 -07:00
|
|
|
// NOTE: we assume here that
|
|
|
|
//
|
|
|
|
// this.retry.poll_ready()
|
|
|
|
//
|
|
|
|
// is equivalent to
|
|
|
|
//
|
|
|
|
// this.retry.service.poll_ready()
|
|
|
|
//
|
|
|
|
// we need to make that assumption to avoid adding an Unpin bound to the Policy
|
|
|
|
// in Ready to make it Unpin so that we can get &mut Ready as needed to call
|
|
|
|
// poll_ready on it.
|
2019-09-30 11:58:27 -07:00
|
|
|
ready!(this.retry.as_mut().project().service.poll_ready(cx))?;
|
2019-09-09 12:10:46 -07:00
|
|
|
let req = this
|
2019-04-24 15:24:39 -07:00
|
|
|
.request
|
|
|
|
.take()
|
|
|
|
.expect("retrying requires cloned request");
|
2019-09-09 12:10:46 -07:00
|
|
|
*this.request = this.retry.policy.clone_request(&req);
|
2019-09-30 11:58:27 -07:00
|
|
|
this.state.set(State::Called(
|
|
|
|
this.retry.as_mut().project().service.call(req),
|
|
|
|
));
|
2019-04-24 15:24:39 -07:00
|
|
|
}
|
2019-09-09 12:10:46 -07:00
|
|
|
}
|
2019-04-24 15:24:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|