feat(transport): Connect lazily in the load balanced channel (#493)
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
use super::super::{service, BoxFuture};
|
use super::super::service;
|
||||||
use super::connection::Connection;
|
use super::connection::Connection;
|
||||||
use crate::transport::Endpoint;
|
use crate::transport::Endpoint;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
future::Future,
|
|
||||||
hash::Hash,
|
hash::Hash,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
@@ -16,15 +15,11 @@ type DiscoverResult<K, S, E> = Result<Change<K, S>, E>;
|
|||||||
|
|
||||||
pub(crate) struct DynamicServiceStream<K: Hash + Eq + Clone> {
|
pub(crate) struct DynamicServiceStream<K: Hash + Eq + Clone> {
|
||||||
changes: Receiver<Change<K, Endpoint>>,
|
changes: Receiver<Change<K, Endpoint>>,
|
||||||
connecting: Option<(K, BoxFuture<Connection, crate::Error>)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: Hash + Eq + Clone> DynamicServiceStream<K> {
|
impl<K: Hash + Eq + Clone> DynamicServiceStream<K> {
|
||||||
pub(crate) fn new(changes: Receiver<Change<K, Endpoint>>) -> Self {
|
pub(crate) fn new(changes: Receiver<Change<K, Endpoint>>) -> Self {
|
||||||
Self {
|
Self { changes }
|
||||||
changes,
|
|
||||||
connecting: None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,39 +32,26 @@ impl<K: Hash + Eq + Clone> Discover for DynamicServiceStream<K> {
|
|||||||
mut self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Poll<DiscoverResult<Self::Key, Self::Service, Self::Error>> {
|
) -> Poll<DiscoverResult<Self::Key, Self::Service, Self::Error>> {
|
||||||
loop {
|
let c = &mut self.changes;
|
||||||
if let Some((key, connecting)) = &mut self.connecting {
|
match Pin::new(&mut *c).poll_next(cx) {
|
||||||
let svc = futures_core::ready!(Pin::new(connecting).poll(cx))?;
|
Poll::Pending | Poll::Ready(None) => Poll::Pending,
|
||||||
let key = key.to_owned();
|
Poll::Ready(Some(change)) => match change {
|
||||||
self.connecting = None;
|
Change::Insert(k, endpoint) => {
|
||||||
let change = Ok(Change::Insert(key, svc));
|
let mut http = hyper::client::connect::HttpConnector::new();
|
||||||
return Poll::Ready(change);
|
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 c = &mut self.changes;
|
#[cfg(not(feature = "tls"))]
|
||||||
match Pin::new(&mut *c).poll_next(cx) {
|
let connector = service::connector(http);
|
||||||
Poll::Pending => return Poll::Pending,
|
let connection = Connection::lazy(connector, endpoint);
|
||||||
Poll::Ready(None) => {
|
let change = Ok(Change::Insert(k, connection));
|
||||||
return Poll::Pending;
|
Poll::Ready(change)
|
||||||
}
|
}
|
||||||
Poll::Ready(Some(change)) => match change {
|
Change::Remove(k) => Poll::Ready(Ok(Change::Remove(k))),
|
||||||
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());
|
|
||||||
|
|
||||||
#[cfg(not(feature = "tls"))]
|
|
||||||
let connector = service::connector(http);
|
|
||||||
let fut = Connection::connect(connector, endpoint);
|
|
||||||
self.connecting = Some((k, Box::pin(fut)));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Change::Remove(k) => return Poll::Ready(Ok(Change::Remove(k))),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user