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

feat(transport): Connect lazily in the load balanced channel #493

Merged
merged 3 commits into from
Nov 18, 2020
Merged
Changes from 1 commit
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
Next Next commit
Connect lazily in the load balanced channel.
  • Loading branch information
Luca Palmieri committed Nov 17, 2020
commit 9a226007ce10f21169674f71f2fbacece6cfd522
32 changes: 9 additions & 23 deletions tonic/src/transport/service/discover.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use super::super::{service, BoxFuture};
use super::super::{service};
use super::connection::Connection;
use crate::transport::Endpoint;

use std::{
future::Future,
hash::Hash,
pin::Pin,
task::{Context, Poll},
Expand All @@ -16,14 +15,12 @@ type DiscoverResult<K, S, E> = Result<Change<K, S>, E>;

pub(crate) struct DynamicServiceStream<K: Hash + Eq + Clone> {
changes: Receiver<Change<K, Endpoint>>,
connecting: Option<(K, BoxFuture<Connection, crate::Error>)>,
}

impl<K: Hash + Eq + Clone> DynamicServiceStream<K> {
pub(crate) fn new(changes: Receiver<Change<K, Endpoint>>) -> Self {
Self {
changes,
connecting: None,
}
}
}
Expand All @@ -38,36 +35,25 @@ impl<K: Hash + Eq + Clone> Discover for DynamicServiceStream<K> {
cx: &mut Context<'_>,
) -> Poll<DiscoverResult<Self::Key, Self::Service, Self::Error>> {
loop {
if let Some((key, connecting)) = &mut self.connecting {
let svc = futures_core::ready!(Pin::new(connecting).poll(cx))?;
let key = key.to_owned();
self.connecting = None;
let change = Ok(Change::Insert(key, svc));
return Poll::Ready(change);
};

let c = &mut self.changes;
match Pin::new(&mut *c).poll_next(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(None) => {
return Poll::Pending;
}
return match Pin::new(&mut *c).poll_next(cx) {
Poll::Pending | Poll::Ready(None) => Poll::Pending,
Poll::Ready(Some(change)) => match change {
Change::Insert(k, endpoint) => {
let mut http = hyper::client::connect::HttpConnector::new();
http.set_nodelay(endpoint.tcp_nodelay);
http.set_keepalive(endpoint.tcp_keepalive);
http.enforce_http(false);
#[cfg(feature = "tls")]
let connector = service::connector(http, endpoint.tls.clone());
let connector = service::connector(http, endpoint.tls.clone());

#[cfg(not(feature = "tls"))]
let connector = service::connector(http);
let fut = Connection::connect(connector, endpoint);
self.connecting = Some((k, Box::pin(fut)));
continue;
let connector = service::connector(http);
let connection = Connection::lazy(connector, endpoint);
let change = Ok(Change::Insert(k, connection));
Poll::Ready(change)
}
Change::Remove(k) => return Poll::Ready(Ok(Change::Remove(k))),
Change::Remove(k) => Poll::Ready(Ok(Change::Remove(k))),
},
}
}
Expand Down