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

Endpoint::lazy_connect always tries to reconnect #460

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions tonic/src/transport/channel/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ impl Endpoint {
#[cfg(not(feature = "tls"))]
let connector = service::connector(http);

Channel::connect(connector, self.clone()).await
let always_reconnect = false;
Channel::connect(connector, self.clone(), always_reconnect).await
}

/// Create a channel from this config.
Expand All @@ -234,7 +235,8 @@ impl Endpoint {
#[cfg(not(feature = "tls"))]
let connector = service::connector(http);

Channel::new(connector, self.clone())
let always_reconnect = true;
Channel::new(connector, self.clone(), always_reconnect)
}

/// Connect with a custom connector.
Expand All @@ -255,7 +257,8 @@ impl Endpoint {
#[cfg(not(feature = "tls"))]
let connector = service::connector(connector);

Channel::connect(connector, self.clone()).await
let always_reconnect = false;
Channel::connect(connector, self.clone(), always_reconnect).await
}

/// Get the endpoint uri.
Expand Down
17 changes: 13 additions & 4 deletions tonic/src/transport/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ 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,
always_reconnect: bool,
) -> Result<Self, super::Error>
where
C: Service<Uri> + Send + 'static,
C::Error: Into<crate::Error> + Send,
Expand All @@ -139,13 +143,18 @@ 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::new(connector, endpoint, always_reconnect)
.map_err(super::Error::from_source)?;
let svc = Buffer::new(Either::A(svc), buffer_size);

Ok(Channel { svc })
}

pub(crate) async fn connect<C>(connector: C, endpoint: Endpoint) -> Result<Self, super::Error>
pub(crate) async fn connect<C>(
connector: C,
endpoint: Endpoint,
always_reconnect: bool,
) -> Result<Self, super::Error>
where
C: Service<Uri> + Send + 'static,
C::Error: Into<crate::Error> + Send,
Expand All @@ -154,7 +163,7 @@ impl Channel {
{
let buffer_size = endpoint.buffer_size.clone().unwrap_or(DEFAULT_BUFFER_SIZE);

let svc = Connection::connect(connector, endpoint)
let svc = Connection::connect(connector, endpoint, always_reconnect)
.await
.map_err(super::Error::from_source)?;
let svc = Buffer::new(Either::A(svc), buffer_size);
Expand Down
18 changes: 14 additions & 4 deletions tonic/src/transport/service/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ pub(crate) struct Connection {
}

impl Connection {
pub(crate) fn new<C>(connector: C, endpoint: Endpoint) -> Result<Self, crate::Error>
pub(crate) fn new<C>(
connector: C,
endpoint: Endpoint,
always_reconnect: bool,
) -> Result<Self, crate::Error>
where
C: Service<Uri> + Send + 'static,
C::Error: Into<crate::Error> + Send,
Expand Down Expand Up @@ -61,7 +65,7 @@ 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(), always_reconnect);

let inner = stack.layer(conn);

Expand All @@ -70,14 +74,20 @@ impl Connection {
})
}

pub(crate) async fn connect<C>(connector: C, endpoint: Endpoint) -> Result<Self, crate::Error>
pub(crate) async fn connect<C>(
connector: C,
endpoint: Endpoint,
always_reconnect: bool,
) -> Result<Self, crate::Error>
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)?.ready_oneshot().await
Self::new(connector, endpoint, always_reconnect)?
.ready_oneshot()
.await
}
}

Expand Down
3 changes: 2 additions & 1 deletion tonic/src/transport/service/discover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ impl<K: Hash + Eq + Clone> Discover for DynamicServiceStream<K> {

#[cfg(not(feature = "tls"))]
let connector = service::connector(http);
let fut = Connection::connect(connector, endpoint);
let always_reconnect = false;
let fut = Connection::connect(connector, endpoint, always_reconnect);
self.connecting = Some((k, Box::pin(fut)));
continue;
}
Expand Down
6 changes: 4 additions & 2 deletions tonic/src/transport/service/reconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ 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, always_reconnect: bool) -> Self {
Reconnect {
mk_service,
state: State::Idle,
target,
error: None,
has_been_connected: false,
// If true, it makes sure that a failure (if any) on the first connection attempt
// will not result in an error, and we will keep trying reconnecting.
has_been_connected: always_reconnect,
}
}
}
Expand Down