@@ -7,6 +7,12 @@ pub struct ControlAddr {
77 pub identity : crate :: transport:: tls:: PeerIdentity ,
88}
99
10+ impl Into < Addr > for ControlAddr {
11+ fn into ( self ) -> Addr {
12+ self . addr
13+ }
14+ }
15+
1016impl fmt:: Display for ControlAddr {
1117 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1218 fmt:: Display :: fmt ( & self . addr , f)
@@ -145,174 +151,74 @@ pub mod add_origin {
145151 }
146152}
147153
148- /// Resolves the controller's `addr` once before building a client.
149154pub mod resolve {
150- use super :: { client, ControlAddr } ;
151- use crate :: svc;
152- use futures:: { ready, TryFuture } ;
153- use linkerd2_addr:: Addr ;
154- use linkerd2_dns as dns;
155- use pin_project:: pin_project;
156- use std:: future:: Future ;
155+ use super :: client:: Target ;
156+ use crate :: {
157+ dns,
158+ proxy:: { discover, dns_resolve:: DnsResolve , resolve:: map_endpoint} ,
159+ svc,
160+ } ;
157161 use std:: net:: SocketAddr ;
158- use std:: pin:: Pin ;
159- use std:: task:: { Context , Poll } ;
160- use std:: { error, fmt} ;
161162
162- #[ derive( Clone , Debug ) ]
163- pub struct Layer {
163+ pub fn layer < M > (
164164 dns : dns:: Resolver ,
165+ ) -> impl svc:: Layer <
166+ M ,
167+ Service = discover:: MakeEndpoint <
168+ discover:: FromResolve < map_endpoint:: Resolve < IntoTarget , DnsResolve > , Target > ,
169+ M ,
170+ > ,
171+ > {
172+ discover:: resolve ( map_endpoint:: Resolve :: new (
173+ IntoTarget ( ( ) ) ,
174+ DnsResolve :: new ( dns) ,
175+ ) )
165176 }
166177
167- #[ derive( Clone , Debug ) ]
168- pub struct Resolve < M > {
169- dns : dns:: Resolver ,
170- inner : M ,
171- }
172-
173- #[ pin_project]
174- pub struct Init < M >
175- where
176- M : tower:: Service < client:: Target > ,
177- {
178- #[ pin]
179- state : State < M > ,
180- }
181-
182- #[ pin_project( project = StateProj ) ]
183- enum State < M >
184- where
185- M : tower:: Service < client:: Target > ,
186- {
187- Resolve ( #[ pin] dns:: IpAddrFuture , Option < ( M , ControlAddr ) > ) ,
188- NotReady ( M , Option < ( SocketAddr , ControlAddr ) > ) ,
189- Inner ( #[ pin] M :: Future ) ,
190- }
191-
192- #[ derive( Debug ) ]
193- pub enum Error < I > {
194- Dns ( dns:: Error ) ,
195- Inner ( I ) ,
196- }
197-
198- // === impl Layer ===
199-
200- pub fn layer < M > ( dns : dns:: Resolver ) -> impl svc:: Layer < M , Service = Resolve < M > > + Clone
201- where
202- M : tower:: Service < client:: Target > + Clone ,
203- {
204- svc:: layer:: mk ( move |inner| Resolve {
205- dns : dns. clone ( ) ,
206- inner,
207- } )
208- }
178+ #[ derive( Copy , Clone , Debug ) ]
179+ pub struct IntoTarget ( ( ) ) ;
209180
210- // === impl Resolve ===
181+ impl map_endpoint:: MapEndpoint < super :: ControlAddr , ( ) > for IntoTarget {
182+ type Out = Target ;
211183
212- impl < M > tower:: Service < ControlAddr > for Resolve < M >
213- where
214- M : tower:: Service < client:: Target > + Clone ,
215- {
216- type Response = M :: Response ;
217- type Error = <Init < M > as TryFuture >:: Error ;
218- type Future = Init < M > ;
219-
220- fn poll_ready ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
221- self . inner . poll_ready ( cx) . map_err ( Error :: Inner )
222- }
223-
224- fn call ( & mut self , target : ControlAddr ) -> Self :: Future {
225- let state = match target. addr {
226- Addr :: Socket ( sa) => State :: make_inner ( sa, & target, & mut self . inner ) ,
227- Addr :: Name ( ref na) => {
228- // The inner service is ready, but we are going to do
229- // additional work before using it. In case the inner
230- // service has acquired resources (like a lock), we
231- // relinquish our claim on the service by replacing it.
232- self . inner = self . inner . clone ( ) ;
233-
234- let future = self . dns . resolve_one_ip ( na. name ( ) ) ;
235- State :: Resolve ( future, Some ( ( self . inner . clone ( ) , target. clone ( ) ) ) )
236- }
237- } ;
238-
239- Init { state }
240- }
241- }
242-
243- // === impl Init ===
244-
245- impl < M > Future for Init < M >
246- where
247- M : tower:: Service < client:: Target > ,
248- {
249- type Output = Result < M :: Response , Error < M :: Error > > ;
250-
251- fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
252- let mut this = self . project ( ) ;
253- loop {
254- match this. state . as_mut ( ) . project ( ) {
255- StateProj :: Resolve ( fut, stack) => {
256- let ip = ready ! ( fut. poll( cx) . map_err( Error :: Dns ) ) ?;
257- let ( svc, config) = stack. take ( ) . unwrap ( ) ;
258- let addr = SocketAddr :: from ( ( ip, config. addr . port ( ) ) ) ;
259- this. state
260- . as_mut ( )
261- . set ( State :: NotReady ( svc, Some ( ( addr, config) ) ) ) ;
262- }
263- StateProj :: NotReady ( svc, cfg) => {
264- ready ! ( svc. poll_ready( cx) . map_err( Error :: Inner ) ) ?;
265- let ( addr, config) = cfg. take ( ) . unwrap ( ) ;
266- let state = State :: make_inner ( addr, & config, svc) ;
267- this. state . as_mut ( ) . set ( state) ;
268- }
269- StateProj :: Inner ( fut) => return fut. poll ( cx) . map_err ( Error :: Inner ) ,
270- } ;
271- }
184+ fn map_endpoint ( & self , control : & super :: ControlAddr , addr : SocketAddr , _: ( ) ) -> Self :: Out {
185+ Target :: new ( addr, control. identity . clone ( ) )
272186 }
273187 }
188+ }
274189
275- impl < M > State < M >
276- where
277- M : tower:: Service < client:: Target > ,
278- {
279- fn make_inner ( addr : SocketAddr , dst : & ControlAddr , mk_svc : & mut M ) -> Self {
280- let target = client:: Target {
281- addr,
282- server_name : dst. identity . clone ( ) ,
283- } ;
284-
285- State :: Inner ( mk_svc. call ( target) )
286- }
287- }
190+ pub mod balance {
191+ use crate :: proxy:: http;
192+ use std:: time:: Duration ;
288193
289- // === impl Error ===
194+ const EWMA_DEFAULT_RTT : Duration = Duration :: from_millis ( 30 ) ;
195+ const EWMA_DECAY : Duration = Duration :: from_secs ( 10 ) ;
290196
291- impl < I : fmt:: Display > fmt:: Display for Error < I > {
292- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
293- match self {
294- Error :: Dns ( dns:: Error :: NoAddressesFound ) => write ! ( f, "no addresses found" ) ,
295- Error :: Dns ( e) => fmt:: Display :: fmt ( & e, f) ,
296- Error :: Inner ( ref e) => fmt:: Display :: fmt ( & e, f) ,
297- }
298- }
197+ pub fn layer < A , B > ( ) -> http:: balance:: Layer < A , B > {
198+ http:: balance:: layer ( EWMA_DEFAULT_RTT , EWMA_DECAY )
299199 }
300-
301- impl < I : fmt:: Debug + fmt:: Display > error:: Error for Error < I > { }
302200}
303201
304202/// Creates a client suitable for gRPC.
305203pub mod client {
306204 use crate :: transport:: { connect, tls} ;
307205 use crate :: { proxy:: http, svc} ;
308206 use linkerd2_proxy_http:: h2:: Settings as H2Settings ;
309- use std:: net:: SocketAddr ;
310- use std:: task:: { Context , Poll } ;
207+ use std:: {
208+ net:: SocketAddr ,
209+ task:: { Context , Poll } ,
210+ } ;
311211
312- #[ derive( Clone , Debug ) ]
212+ #[ derive( Clone , Hash , Debug , Eq , PartialEq ) ]
313213 pub struct Target {
314- pub ( super ) addr : SocketAddr ,
315- pub ( super ) server_name : tls:: PeerIdentity ,
214+ addr : SocketAddr ,
215+ server_name : tls:: PeerIdentity ,
216+ }
217+
218+ impl Target {
219+ pub ( super ) fn new ( addr : SocketAddr , server_name : tls:: PeerIdentity ) -> Self {
220+ Self { addr, server_name }
221+ }
316222 }
317223
318224 #[ derive( Debug ) ]
@@ -356,12 +262,10 @@ pub mod client {
356262 type Error = <http:: h2:: Connect < C , B > as tower:: Service < Target > >:: Error ;
357263 type Future = <http:: h2:: Connect < C , B > as tower:: Service < Target > >:: Future ;
358264
359- #[ inline]
360265 fn poll_ready ( & mut self , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
361266 self . inner . poll_ready ( cx)
362267 }
363268
364- #[ inline]
365269 fn call ( & mut self , target : Target ) -> Self :: Future {
366270 self . inner . call ( target)
367271 }
0 commit comments