1- use crate :: proxy:: http:: {
2- glue:: { HttpBody , HyperServerSvc } ,
3- upgrade, Version as HttpVersion ,
1+ use crate :: {
2+ drain,
3+ proxy:: {
4+ core:: Accept ,
5+ detect,
6+ http:: {
7+ glue:: { HttpBody , HyperServerSvc } ,
8+ h2:: Settings as H2Settings ,
9+ upgrade, Version as HttpVersion ,
10+ } ,
11+ } ,
12+ svc:: { MakeService , Service , ServiceExt } ,
13+ transport:: { self , io:: BoxedIo , labels:: Key as TransportKey , metrics:: TransportLabels , tls} ,
14+ Error , Never ,
415} ;
5- use crate :: proxy:: { detect, tcp} ;
6- use crate :: svc:: { MakeService , Service } ;
7- use crate :: transport:: {
8- self , io:: BoxedIo , labels:: Key as TransportKey , metrics:: TransportLabels , tls,
9- } ;
10- use futures:: future:: { self , Either } ;
11- use futures:: { Future , Poll } ;
16+ use futures:: { future:: Either , Future , Poll } ;
1217use http;
1318use hyper;
1419use indexmap:: IndexSet ;
15- use linkerd2_drain as drain;
16- use linkerd2_error:: { Error , Never } ;
17- use linkerd2_proxy_core:: listen:: Accept ;
18- use linkerd2_proxy_http:: h2:: Settings as H2Settings ;
19- use std:: net:: SocketAddr ;
2020use std:: sync:: Arc ;
21- use std:: { error, fmt} ;
22- use tokio:: io:: { AsyncRead , AsyncWrite } ;
2321use tracing:: { info_span, trace} ;
2422use tracing_futures:: Instrument ;
2523
@@ -77,7 +75,7 @@ impl detect::Detect<tls::accept::Meta> for ProtocolDetect {
7775///
7876/// * Otherwise, an `H`-typed `Service` is used to build a service that
7977/// can route HTTP requests for the `tls::accept::Meta`.
80- pub struct Server < L , C , H , B >
78+ pub struct Server < L , F , H , B >
8179where
8280 // Used when forwarding a TCP stream (e.g. with telemetry, timeouts).
8381 L : TransportLabels < Protocol , Labels = TransportKey > ,
@@ -94,57 +92,12 @@ where
9492 h2_settings : H2Settings ,
9593 transport_labels : L ,
9694 transport_metrics : transport:: MetricsRegistry ,
97- connect : ForwardConnect < C > ,
95+ forward_tcp : F ,
9896 make_http : H ,
9997 drain : drain:: Watch ,
10098}
10199
102- /// Establishes connections for forwarded connections.
103- ///
104- /// Fails to produce a `Connect` if this would connect to the listener that
105- /// already accepted this.
106- #[ derive( Clone , Debug ) ]
107- struct ForwardConnect < C > ( C ) ;
108-
109- /// An error indicating an accepted socket did not have an SO_ORIGINAL_DST
110- /// address and therefore could not be forwarded.
111- #[ derive( Clone , Debug ) ]
112- pub struct NoForwardTarget ;
113-
114- impl < C > Service < tls:: accept:: Meta > for ForwardConnect < C >
115- where
116- C : Service < SocketAddr > ,
117- C :: Error : Into < Error > ,
118- {
119- type Response = C :: Response ;
120- type Error = Error ;
121- type Future = future:: Either <
122- future:: FutureResult < C :: Response , Error > ,
123- future:: MapErr < C :: Future , fn ( C :: Error ) -> Error > ,
124- > ;
125-
126- fn poll_ready ( & mut self ) -> Poll < ( ) , Self :: Error > {
127- self . 0 . poll_ready ( ) . map_err ( Into :: into)
128- }
129-
130- fn call ( & mut self , meta : tls:: accept:: Meta ) -> Self :: Future {
131- if meta. addrs . target_addr_is_local ( ) {
132- return future:: Either :: A ( future:: err ( NoForwardTarget . into ( ) ) ) ;
133- }
134-
135- future:: Either :: B ( self . 0 . call ( meta. addrs . target_addr ( ) ) . map_err ( Into :: into) )
136- }
137- }
138-
139- impl error:: Error for NoForwardTarget { }
140-
141- impl fmt:: Display for NoForwardTarget {
142- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
143- write ! ( f, "Could not forward to a target address" )
144- }
145- }
146-
147- impl < L , C , H , B > Server < L , C , H , B >
100+ impl < L , F , H , B > Server < L , F , H , B >
148101where
149102 L : TransportLabels < Protocol , Labels = TransportKey > ,
150103 H : MakeService <
@@ -160,7 +113,7 @@ where
160113 pub fn new (
161114 transport_labels : L ,
162115 transport_metrics : transport:: MetricsRegistry ,
163- connect : C ,
116+ forward_tcp : F ,
164117 make_http : H ,
165118 h2_settings : H2Settings ,
166119 drain : drain:: Watch ,
@@ -173,21 +126,19 @@ where
173126 h2_settings,
174127 transport_labels,
175128 transport_metrics,
176- connect : ForwardConnect ( connect ) ,
129+ forward_tcp ,
177130 make_http,
178131 drain,
179132 } ,
180133 )
181134 }
182135}
183136
184- impl < L , C , H , B > Service < Connection > for Server < L , C , H , B >
137+ impl < L , F , H , B > Service < Connection > for Server < L , F , H , B >
185138where
186139 L : TransportLabels < Protocol , Labels = TransportKey > ,
187- C : Service < SocketAddr > + Clone + Send + ' static ,
188- C :: Response : AsyncRead + AsyncWrite + Send + ' static ,
189- C :: Future : Send + ' static ,
190- C :: Error : Into < Error > ,
140+ F : Accept < ( tls:: accept:: Meta , transport:: metrics:: Io < BoxedIo > ) > + Clone + Send + ' static ,
141+ F :: Future : Send + ' static ,
191142 H : MakeService <
192143 tls:: accept:: Meta ,
193144 http:: Request < HttpBody > ,
@@ -228,8 +179,12 @@ where
228179 Some ( http) => http,
229180 None => {
230181 trace ! ( "did not detect protocol; forwarding TCP" ) ;
231- let fwd = tcp:: forward ( io, self . connect . clone ( ) , proto. tls ) ;
232- return Box :: new ( drain. watch ( fwd, |_| { } ) ) ;
182+ let fwd = self
183+ . forward_tcp
184+ . clone ( )
185+ . into_service ( )
186+ . oneshot ( ( proto. tls , io) ) ;
187+ return Box :: new ( drain. watch ( fwd. map_err ( Into :: into) , |_| { } ) ) ;
233188 }
234189 } ;
235190
@@ -279,10 +234,10 @@ where
279234 }
280235}
281236
282- impl < L , C , H , B > Clone for Server < L , C , H , B >
237+ impl < L , F , H , B > Clone for Server < L , F , H , B >
283238where
284239 L : TransportLabels < Protocol , Labels = TransportKey > + Clone ,
285- C : Clone ,
240+ F : Clone ,
286241 H : MakeService <
287242 tls:: accept:: Meta ,
288243 http:: Request < HttpBody > ,
@@ -297,7 +252,7 @@ where
297252 h2_settings : self . h2_settings . clone ( ) ,
298253 transport_labels : self . transport_labels . clone ( ) ,
299254 transport_metrics : self . transport_metrics . clone ( ) ,
300- connect : self . connect . clone ( ) ,
255+ forward_tcp : self . forward_tcp . clone ( ) ,
301256 make_http : self . make_http . clone ( ) ,
302257 drain : self . drain . clone ( ) ,
303258 }
0 commit comments