@@ -214,6 +214,25 @@ impl Endpoint {
|
|||||||
Channel::connect(connector, self.clone()).await
|
Channel::connect(connector, self.clone()).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a channel from this config.
|
||||||
|
///
|
||||||
|
/// The channel returned by this method does not attempt to connect to the endpoint until first
|
||||||
|
/// use.
|
||||||
|
pub fn connect_lazy(&self) -> Result<Channel, Error> {
|
||||||
|
let mut http = hyper::client::connect::HttpConnector::new();
|
||||||
|
http.enforce_http(false);
|
||||||
|
http.set_nodelay(self.tcp_nodelay);
|
||||||
|
http.set_keepalive(self.tcp_keepalive);
|
||||||
|
|
||||||
|
#[cfg(feature = "tls")]
|
||||||
|
let connector = service::connector(http, self.tls.clone());
|
||||||
|
|
||||||
|
#[cfg(not(feature = "tls"))]
|
||||||
|
let connector = service::connector(http);
|
||||||
|
|
||||||
|
Channel::new(connector, self.clone())
|
||||||
|
}
|
||||||
|
|
||||||
/// Connect with a custom connector.
|
/// Connect with a custom connector.
|
||||||
pub async fn connect_with_connector<C>(&self, connector: C) -> Result<Channel, Error>
|
pub async fn connect_with_connector<C>(&self, connector: C) -> Result<Channel, Error>
|
||||||
where
|
where
|
||||||
|
|||||||
@@ -130,6 +130,21 @@ impl Channel {
|
|||||||
(Self::balance(list, DEFAULT_BUFFER_SIZE), tx)
|
(Self::balance(list, DEFAULT_BUFFER_SIZE), tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn new<C>(connector: C, endpoint: Endpoint) -> Result<Self, super::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,
|
||||||
|
{
|
||||||
|
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 = 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) -> Result<Self, super::Error>
|
||||||
where
|
where
|
||||||
C: Service<Uri> + Send + 'static,
|
C: Service<Uri> + Send + 'static,
|
||||||
@@ -139,10 +154,9 @@ impl Channel {
|
|||||||
{
|
{
|
||||||
let buffer_size = endpoint.buffer_size.clone().unwrap_or(DEFAULT_BUFFER_SIZE);
|
let buffer_size = endpoint.buffer_size.clone().unwrap_or(DEFAULT_BUFFER_SIZE);
|
||||||
|
|
||||||
let svc = Connection::new(connector, endpoint)
|
let svc = Connection::connect(connector, endpoint)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| super::Error::from_source(e))?;
|
.map_err(super::Error::from_source)?;
|
||||||
|
|
||||||
let svc = Buffer::new(Either::A(svc), buffer_size);
|
let svc = Buffer::new(Either::A(svc), buffer_size);
|
||||||
|
|
||||||
Ok(Channel { svc })
|
Ok(Channel { svc })
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ use tower::{
|
|||||||
limit::{concurrency::ConcurrencyLimitLayer, rate::RateLimitLayer},
|
limit::{concurrency::ConcurrencyLimitLayer, rate::RateLimitLayer},
|
||||||
timeout::TimeoutLayer,
|
timeout::TimeoutLayer,
|
||||||
util::BoxService,
|
util::BoxService,
|
||||||
ServiceBuilder,
|
ServiceBuilder, ServiceExt,
|
||||||
};
|
};
|
||||||
use tower_load::Load;
|
use tower_load::Load;
|
||||||
use tower_service::Service;
|
use tower_service::Service;
|
||||||
@@ -29,7 +29,7 @@ pub(crate) struct Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Connection {
|
impl Connection {
|
||||||
pub(crate) async fn new<C>(connector: C, endpoint: Endpoint) -> Result<Self, crate::Error>
|
pub(crate) fn new<C>(connector: C, endpoint: Endpoint) -> Result<Self, crate::Error>
|
||||||
where
|
where
|
||||||
C: Service<Uri> + Send + 'static,
|
C: Service<Uri> + Send + 'static,
|
||||||
C::Error: Into<crate::Error> + Send,
|
C::Error: Into<crate::Error> + Send,
|
||||||
@@ -60,9 +60,8 @@ impl Connection {
|
|||||||
.optional_layer(endpoint.rate_limit.map(|(l, d)| RateLimitLayer::new(l, d)))
|
.optional_layer(endpoint.rate_limit.map(|(l, d)| RateLimitLayer::new(l, d)))
|
||||||
.into_inner();
|
.into_inner();
|
||||||
|
|
||||||
let mut connector = HyperConnect::new(connector, settings);
|
let connector = HyperConnect::new(connector, settings);
|
||||||
let initial_conn = connector.call(endpoint.uri.clone()).await?;
|
let conn = Reconnect::new(connector, endpoint.uri.clone());
|
||||||
let conn = Reconnect::new(initial_conn, connector, endpoint.uri.clone());
|
|
||||||
|
|
||||||
let inner = stack.layer(conn);
|
let inner = stack.layer(conn);
|
||||||
|
|
||||||
@@ -70,6 +69,16 @@ impl Connection {
|
|||||||
inner: BoxService::new(inner),
|
inner: BoxService::new(inner),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn connect<C>(connector: C, endpoint: Endpoint) -> 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Service<Request> for Connection {
|
impl Service<Request> for Connection {
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ impl<K: Hash + Eq + Clone> Discover for DynamicServiceStream<K> {
|
|||||||
|
|
||||||
#[cfg(not(feature = "tls"))]
|
#[cfg(not(feature = "tls"))]
|
||||||
let connector = service::connector(http);
|
let connector = service::connector(http);
|
||||||
let fut = Connection::new(connector, endpoint);
|
let fut = Connection::connect(connector, endpoint);
|
||||||
self.connecting = Some((k, Box::pin(fut)));
|
self.connecting = Some((k, Box::pin(fut)));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,16 +31,10 @@ impl<M, Target> Reconnect<M, Target>
|
|||||||
where
|
where
|
||||||
M: Service<Target>,
|
M: Service<Target>,
|
||||||
{
|
{
|
||||||
pub(crate) fn new<S, Request>(initial_connection: S, mk_service: M, target: Target) -> Self
|
pub(crate) fn new(mk_service: M, target: Target) -> Self {
|
||||||
where
|
|
||||||
M: Service<Target, Response = S>,
|
|
||||||
S: Service<Request>,
|
|
||||||
Error: From<M::Error> + From<S::Error>,
|
|
||||||
Target: Clone,
|
|
||||||
{
|
|
||||||
Reconnect {
|
Reconnect {
|
||||||
mk_service,
|
mk_service,
|
||||||
state: State::Connected(initial_connection),
|
state: State::Idle,
|
||||||
target,
|
target,
|
||||||
error: None,
|
error: None,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user