fix(transport): reconnect lazy connections after first failure (#458)
* fix(transport): reconnect lazy connections after first failure Channels created with lazy connections never try to reconnect if the first connection attempt fails. This is because `Reconnect` returns `Poll::Ready(Err)` on poll_ready and the service is considered dead. This change passes a flag to Reconnect to signal if the connection is intended to be lazy, in which case reconnect returns the error on the next call. fixes #452
This commit is contained in:
@@ -234,7 +234,7 @@ impl Endpoint {
|
||||
#[cfg(not(feature = "tls"))]
|
||||
let connector = service::connector(http);
|
||||
|
||||
Channel::new(connector, self.clone())
|
||||
Ok(Channel::new(connector, self.clone()))
|
||||
}
|
||||
|
||||
/// Connect with a custom connector.
|
||||
|
||||
@@ -130,7 +130,7 @@ impl Channel {
|
||||
(Self::balance(list, DEFAULT_BUFFER_SIZE), tx)
|
||||
}
|
||||
|
||||
pub(crate) fn new<C>(connector: C, endpoint: Endpoint) -> Result<Self, super::Error>
|
||||
pub(crate) fn new<C>(connector: C, endpoint: Endpoint) -> Self
|
||||
where
|
||||
C: Service<Uri> + Send + 'static,
|
||||
C::Error: Into<crate::Error> + Send,
|
||||
@@ -139,10 +139,10 @@ impl Channel {
|
||||
{
|
||||
let buffer_size = endpoint.buffer_size.clone().unwrap_or(DEFAULT_BUFFER_SIZE);
|
||||
|
||||
let svc = Connection::new(connector, endpoint).map_err(super::Error::from_source)?;
|
||||
let svc = Connection::lazy(connector, endpoint);
|
||||
let svc = Buffer::new(Either::A(svc), buffer_size);
|
||||
|
||||
Ok(Channel { svc })
|
||||
Channel { svc }
|
||||
}
|
||||
|
||||
pub(crate) async fn connect<C>(connector: C, endpoint: Endpoint) -> Result<Self, super::Error>
|
||||
|
||||
@@ -29,7 +29,7 @@ pub(crate) struct Connection {
|
||||
}
|
||||
|
||||
impl Connection {
|
||||
pub(crate) fn new<C>(connector: C, endpoint: Endpoint) -> Result<Self, crate::Error>
|
||||
fn new<C>(connector: C, endpoint: Endpoint, is_lazy: bool) -> Self
|
||||
where
|
||||
C: Service<Uri> + Send + 'static,
|
||||
C::Error: Into<crate::Error> + Send,
|
||||
@@ -61,13 +61,13 @@ impl Connection {
|
||||
.into_inner();
|
||||
|
||||
let connector = HyperConnect::new(connector, settings);
|
||||
let conn = Reconnect::new(connector, endpoint.uri.clone());
|
||||
let conn = Reconnect::new(connector, endpoint.uri.clone(), is_lazy);
|
||||
|
||||
let inner = stack.layer(conn);
|
||||
|
||||
Ok(Self {
|
||||
Self {
|
||||
inner: BoxService::new(inner),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn connect<C>(connector: C, endpoint: Endpoint) -> Result<Self, crate::Error>
|
||||
@@ -77,7 +77,17 @@ impl Connection {
|
||||
C::Future: Unpin + Send,
|
||||
C::Response: AsyncRead + AsyncWrite + HyperConnection + Unpin + Send + 'static,
|
||||
{
|
||||
Self::new(connector, endpoint)?.ready_oneshot().await
|
||||
Self::new(connector, endpoint, false).ready_oneshot().await
|
||||
}
|
||||
|
||||
pub(crate) fn lazy<C>(connector: C, endpoint: Endpoint) -> Self
|
||||
where
|
||||
C: Service<Uri> + Send + 'static,
|
||||
C::Error: Into<crate::Error> + Send,
|
||||
C::Future: Unpin + Send,
|
||||
C::Response: AsyncRead + AsyncWrite + HyperConnection + Unpin + Send + 'static,
|
||||
{
|
||||
Self::new(connector, endpoint, true)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ where
|
||||
target: Target,
|
||||
error: Option<M::Error>,
|
||||
has_been_connected: bool,
|
||||
is_lazy: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -32,13 +33,14 @@ impl<M, Target> Reconnect<M, Target>
|
||||
where
|
||||
M: Service<Target>,
|
||||
{
|
||||
pub(crate) fn new(mk_service: M, target: Target) -> Self {
|
||||
pub(crate) fn new(mk_service: M, target: Target, is_lazy: bool) -> Self {
|
||||
Reconnect {
|
||||
mk_service,
|
||||
state: State::Idle,
|
||||
target,
|
||||
error: None,
|
||||
has_been_connected: false,
|
||||
is_lazy,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,11 +91,11 @@ where
|
||||
|
||||
state = State::Idle;
|
||||
|
||||
if self.has_been_connected {
|
||||
if !(self.has_been_connected || self.is_lazy) {
|
||||
return Poll::Ready(Err(e.into()));
|
||||
} else {
|
||||
self.error = Some(e.into());
|
||||
break;
|
||||
} else {
|
||||
return Poll::Ready(Err(e.into()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user