Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reconnect: Fix maker error to return in future #386

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 33 additions & 7 deletions tower-reconnect/src/future.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Error;
use pin_project::pin_project;
use pin_project::{pin_project, project};
use std::{
future::Future,
pin::Pin,
Expand All @@ -9,25 +9,51 @@ use std::{
/// Future that resolves to the response or failure to connect.
#[pin_project]
#[derive(Debug)]
pub struct ResponseFuture<F> {
pub struct ResponseFuture<F, E> {
#[pin]
inner: F,
inner: Inner<F, E>,
}

impl<F> ResponseFuture<F> {
#[pin_project]
#[derive(Debug)]
enum Inner<F, E> {
Future(#[pin] F),
Error(Option<E>),
}

impl<F, E> ResponseFuture<F, E> {
pub(crate) fn new(inner: F) -> Self {
ResponseFuture { inner }
ResponseFuture {
inner: Inner::Future(inner),
}
}

pub(crate) fn error(error: E) -> Self {
ResponseFuture {
inner: Inner::Error(Some(error)),
}
}
}

impl<F, T, E> Future for ResponseFuture<F>
impl<F, T, E, ME> Future for ResponseFuture<F, ME>
where
F: Future<Output = Result<T, E>>,
E: Into<Error>,
ME: Into<Error>,
{
type Output = Result<T, Error>;

#[project]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.project().inner.poll(cx).map_err(Into::into)
//self.project().inner.poll(cx).map_err(Into::into)
let me = self.project();
#[project]
match me.inner.project() {
Inner::Future(fut) => fut.poll(cx).map_err(Into::into),
Inner::Error(e) => {
let e = e.take().expect("Polled after ready.").into();
Poll::Ready(Err(e))
}
}
}
}
29 changes: 15 additions & 14 deletions tower-reconnect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ where
mk_service: M,
state: State<M::Future, M::Response>,
target: Target,
error: Option<M::Error>,
}

type Error = Box<dyn std::error::Error + Send + Sync>;
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

#[derive(Debug)]
enum State<F, S> {
Expand All @@ -51,6 +52,7 @@ where
mk_service,
state: State::Idle,
target,
error: None,
}
}

Expand All @@ -60,6 +62,7 @@ where
mk_service,
state: State::Connected(init_conn),
target,
error: None,
}
}
}
Expand All @@ -74,14 +77,11 @@ where
{
type Response = S::Response;
type Error = Error;
type Future = ResponseFuture<S::Future>;
type Future = ResponseFuture<S::Future, M::Error>;

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let ret;
let mut state;

loop {
match self.state {
match &mut self.state {
State::Idle => {
trace!("poll_ready; idle");
match self.mk_service.poll_ready(cx) {
Expand All @@ -100,16 +100,16 @@ where
trace!("poll_ready; connecting");
match Pin::new(f).poll(cx) {
Poll::Ready(Ok(service)) => {
state = State::Connected(service);
self.state = State::Connected(service);
}
Poll::Pending => {
trace!("poll_ready; not ready");
return Poll::Pending;
}
Poll::Ready(Err(e)) => {
trace!("poll_ready; error");
state = State::Idle;
ret = Err(e.into());
self.state = State::Idle;
self.error = Some(e.into());
break;
}
}
Expand All @@ -127,20 +127,21 @@ where
}
Poll::Ready(Err(_)) => {
trace!("poll_ready; error");
state = State::Idle;
self.state = State::Idle;
}
}
}
}

self.state = state;
}

self.state = state;
Poll::Ready(ret)
Poll::Ready(Ok(()))
}

fn call(&mut self, request: Request) -> Self::Future {
if let Some(error) = self.error.take() {
return ResponseFuture::error(error);
}

let service = match self.state {
State::Connected(ref mut service) => service,
_ => panic!("service not ready; poll_ready must be called first"),
Expand Down