1- use std:: { error:: Error , task:: Context , time:: Duration } ;
1+ use std:: { error:: Error as StdError , task:: Context , time:: Duration } ;
22
3- use crate :: connect:: { Connect as TcpConnect , Connector as TcpConnector } ;
3+ use crate :: connect:: { Connect as TcpConnect , Connector2 as TcpConnector } ;
44use crate :: service:: { Service , ServiceCtx , ServiceFactory , apply_fn_factory, boxed} ;
5- use crate :: { SharedCfg , http:: Uri , io:: IoBoxed , time:: Seconds , util:: join} ;
5+ use crate :: { SharedCfg , error :: Error , http:: Uri , io:: IoBoxed , time:: Seconds , util:: join} ;
66
7- use super :: { Connect , Connection , error:: ConnectError , pool:: ConnectionPool } ;
7+ use super :: error:: { ClientError , ConnectError } ;
8+ use super :: { Connect , Connection , pool:: ConnectionPool } ;
89
910#[ cfg( feature = "openssl" ) ]
1011use tls_openssl:: ssl:: SslConnector as OpensslConnector ;
1112
1213#[ cfg( feature = "rustls" ) ]
1314use tls_rustls:: ClientConfig ;
1415
15- type BoxedConnector =
16- boxed:: BoxServiceFactory < SharedCfg , Connect , IoBoxed , ConnectError , Box < dyn Error > > ;
16+ type BoxedConnector = boxed:: BoxServiceFactory <
17+ SharedCfg ,
18+ Connect ,
19+ IoBoxed ,
20+ Error < ConnectError > ,
21+ Box < dyn StdError > ,
22+ > ;
1723
1824#[ derive( Debug ) ]
1925/// Manages http client network connectivity.
@@ -49,8 +55,8 @@ impl Connector {
4955 svc. call ( TcpConnect :: new ( msg. uri ) . set_addr ( msg. addr ) ) . await
5056 } )
5157 . map ( IoBoxed :: from)
52- . map_err ( ConnectError :: from)
53- . map_init_err ( |e| Box :: new ( e) as Box < dyn Error > ) ,
58+ . map_err ( |e| e . map ( ConnectError :: from) )
59+ . map_init_err ( |e| Box :: new ( e) as Box < dyn StdError > ) ,
5460 ) ,
5561 secure_svc : None ,
5662 conn_lifetime : Duration :: from_secs ( 75 ) ,
@@ -96,18 +102,18 @@ impl Connector {
96102 #[ cfg( feature = "openssl" ) ]
97103 /// Use openssl connector for secured connections.
98104 pub fn openssl ( self , connector : OpensslConnector ) -> Self {
99- use crate :: connect:: openssl:: SslConnector ;
105+ use crate :: connect:: openssl:: SslConnector2 ;
100106
101- self . secure_connector ( SslConnector :: new ( connector) )
107+ self . secure_connector ( SslConnector2 :: new ( connector) )
102108 }
103109
104110 #[ must_use]
105111 #[ cfg( feature = "rustls" ) ]
106112 /// Use rustls connector for secured connections.
107113 pub fn rustls ( self , connector : ClientConfig ) -> Self {
108- use crate :: connect:: rustls:: TlsConnector ;
114+ use crate :: connect:: rustls:: TlsConnector2 ;
109115
110- self . secure_connector ( TlsConnector :: new ( connector) )
116+ self . secure_connector ( TlsConnector2 :: new ( connector) )
111117 }
112118
113119 #[ must_use]
@@ -147,18 +153,21 @@ impl Connector {
147153 /// Use custom connector to open un-secured connections.
148154 pub fn connector < T > ( mut self , connector : T ) -> Self
149155 where
150- T : ServiceFactory < TcpConnect < Uri > , SharedCfg , Error = crate :: connect:: ConnectError >
151- + ' static ,
152- T :: InitError : Error ,
156+ T : ServiceFactory <
157+ TcpConnect < Uri > ,
158+ SharedCfg ,
159+ Error = Error < crate :: connect:: ConnectError > ,
160+ > + ' static ,
161+ T :: InitError : StdError ,
153162 IoBoxed : From < T :: Response > ,
154163 {
155164 self . svc = boxed:: factory (
156165 apply_fn_factory ( connector, async move |msg : Connect , svc| {
157166 svc. call ( TcpConnect :: new ( msg. uri ) . set_addr ( msg. addr ) ) . await
158167 } )
159168 . map ( IoBoxed :: from)
160- . map_err ( ConnectError :: from)
161- . map_init_err ( |e| Box :: new ( e) as Box < dyn Error > ) ,
169+ . map_err ( |e| e . map ( ConnectError :: from) )
170+ . map_init_err ( |e| Box :: new ( e) as Box < dyn StdError > ) ,
162171 ) ;
163172 self
164173 }
@@ -167,28 +176,31 @@ impl Connector {
167176 /// Use custom connector to open secure connections.
168177 pub fn secure_connector < T > ( mut self , connector : T ) -> Self
169178 where
170- T : ServiceFactory < TcpConnect < Uri > , SharedCfg , Error = crate :: connect:: ConnectError >
171- + ' static ,
172- T :: InitError : Error ,
179+ T : ServiceFactory <
180+ TcpConnect < Uri > ,
181+ SharedCfg ,
182+ Error = Error < crate :: connect:: ConnectError > ,
183+ > + ' static ,
184+ T :: InitError : StdError ,
173185 IoBoxed : From < T :: Response > ,
174186 {
175187 self . secure_svc = Some ( boxed:: factory (
176188 apply_fn_factory ( connector, async move |msg : Connect , svc| {
177189 svc. call ( TcpConnect :: new ( msg. uri ) . set_addr ( msg. addr ) ) . await
178190 } )
179191 . map ( IoBoxed :: from)
180- . map_err ( ConnectError :: from)
181- . map_init_err ( |e| Box :: new ( e) as Box < dyn Error > ) ,
192+ . map_err ( |e| e . map ( ConnectError :: from) )
193+ . map_init_err ( |e| Box :: new ( e) as Box < dyn StdError > ) ,
182194 ) ) ;
183195 self
184196 }
185197}
186198
187199impl ServiceFactory < Connect , SharedCfg > for Connector {
188200 type Response = Connection ;
189- type Error = ConnectError ;
201+ type Error = Error < ClientError > ;
190202 type Service = ConnectorService ;
191- type InitError = Box < dyn Error > ;
203+ type InitError = Box < dyn StdError > ;
192204
193205 async fn create ( & self , cfg : SharedCfg ) -> Result < Self :: Service , Self :: InitError > {
194206 let ssl_pool = if let Some ( ref svc) = self . secure_svc {
@@ -207,39 +219,44 @@ impl ServiceFactory<Connect, SharedCfg> for Connector {
207219 self . conn_lifetime ,
208220 self . conn_keep_alive ,
209221 self . limit ,
210- cfg,
222+ cfg. clone ( ) ,
211223 ) ;
212- Ok ( ConnectorService { tcp_pool, ssl_pool } )
224+ Ok ( ConnectorService {
225+ cfg,
226+ tcp_pool,
227+ ssl_pool,
228+ } )
213229 }
214230}
215231
216232/// Manages http client network connectivity.
217233#[ derive( Clone , Debug ) ]
218234pub struct ConnectorService {
235+ cfg : SharedCfg ,
219236 tcp_pool : ConnectionPool ,
220237 ssl_pool : Option < ConnectionPool > ,
221238}
222239
223240impl Service < Connect > for ConnectorService {
224241 type Response = Connection ;
225- type Error = ConnectError ;
242+ type Error = Error < ClientError > ;
226243
227244 #[ inline]
228245 async fn ready ( & self , ctx : ServiceCtx < ' _ , Self > ) -> Result < ( ) , Self :: Error > {
229246 if let Some ( ref ssl_pool) = self . ssl_pool {
230247 let ( r1, r2) = join ( ctx. ready ( & self . tcp_pool ) , ctx. ready ( ssl_pool) ) . await ;
231- r1?;
232- r2
248+ r1. map_err ( ClientError :: from ) ?;
249+ Ok ( r2 . map_err ( ClientError :: from ) ? )
233250 } else {
234- ctx. ready ( & self . tcp_pool ) . await
251+ Ok ( ctx. ready ( & self . tcp_pool ) . await . map_err ( ClientError :: from ) ? )
235252 }
236253 }
237254
238255 #[ inline]
239256 fn poll ( & self , cx : & mut Context < ' _ > ) -> Result < ( ) , Self :: Error > {
240- self . tcp_pool . poll ( cx) ?;
257+ self . tcp_pool . poll ( cx) . map_err ( ClientError :: from ) ?;
241258 if let Some ( ref ssl_pool) = self . ssl_pool {
242- ssl_pool. poll ( cx) ?;
259+ ssl_pool. poll ( cx) . map_err ( ClientError :: from ) ?;
243260 }
244261 Ok ( ( ) )
245262 }
@@ -259,13 +276,20 @@ impl Service<Connect> for ConnectorService {
259276 match req. uri . scheme_str ( ) {
260277 Some ( "https" | "wss" ) => {
261278 if let Some ( ref conn) = self . ssl_pool {
262- ctx. call ( conn, req) . await
279+ ctx. call ( conn, req) . await . map_err ( ClientError :: from )
263280 } else {
264- Err ( ConnectError :: SslIsNotSupported )
281+ Err ( ClientError :: from (
282+ Error :: from ( ConnectError :: SslIsNotSupported )
283+ . set_service ( self . cfg . service ( ) ) ,
284+ ) )
265285 }
266286 }
267- _ => ctx. call ( & self . tcp_pool , req) . await ,
287+ _ => ctx
288+ . call ( & self . tcp_pool , req)
289+ . await
290+ . map_err ( ClientError :: from) ,
268291 }
292+ . map_err ( Error :: from)
269293 }
270294}
271295
0 commit comments