From 926c4cf3233e956b6bd7d2b487ee6a18521efe1c Mon Sep 17 00:00:00 2001 From: Oliver Gould Date: Thu, 19 Apr 2018 11:00:28 -0700 Subject: [PATCH] proxy: Make control::discovery::Bind generic over its Endpoint type (#796) Previously, `Bind` required that it bind to `SocketAddr` (and `SocketAddr` only). This makes it hard to pass additional information from service discovery into the client's stack. To resolve this, `Bind` now has an additional `Endpoint` trait-generic type, and `Bind::bind` accepts an `Endpoint` rather than a `SocketAddr`. No additional endpoints have been introduced yet. There are no functional changes in this refactor. --- proxy/src/bind.rs | 1 + proxy/src/control/discovery.rs | 27 ++++++--------------------- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/proxy/src/bind.rs b/proxy/src/bind.rs index 013fda82d..fe31ec314 100644 --- a/proxy/src/bind.rs +++ b/proxy/src/bind.rs @@ -231,6 +231,7 @@ impl control::discovery::Bind for BindProtocol, B> where B: tower_h2::Body + 'static, { + type Endpoint = SocketAddr; type Request = http::Request; type Response = HttpResponse; type Error = as tower::Service>::Error; diff --git a/proxy/src/control/discovery.rs b/proxy/src/control/discovery.rs index 4ec6702b5..cc1fb5d25 100644 --- a/proxy/src/control/discovery.rs +++ b/proxy/src/control/discovery.rs @@ -134,6 +134,9 @@ enum Update { /// Bind a `SocketAddr` with a protocol. pub trait Bind { + /// The type of endpoint upon which a `Service` is bound. + type Endpoint; + /// Requests handled by the discovered services type Request; @@ -148,8 +151,8 @@ pub trait Bind { /// The discovered `Service` instance. type Service: Service; - /// Bind a socket address with a service. - fn bind(&self, addr: &SocketAddr) -> Result; + /// Bind a service from an endpoint. + fn bind(&self, addr: &Self::Endpoint) -> Result; } /// Creates a "channel" of `Discovery` to `Background` handles. @@ -215,7 +218,7 @@ impl Watch { impl Discover for Watch where - B: Bind>, + B: Bind>, { type Key = SocketAddr; type Request = B::Request; @@ -658,24 +661,6 @@ impl > DestinationSet { } } -// ===== impl Bind ===== - -impl Bind for F -where - F: Fn(&SocketAddr) -> Result, - S: Service, -{ - type Request = S::Request; - type Response = S::Response; - type Error = S::Error; - type Service = S; - type BindError = E; - - fn bind(&self, addr: &SocketAddr) -> Result { - (*self)(addr) - } -} - // ===== impl UpdateRx ===== impl Stream for UpdateRx