@@ -4,20 +4,19 @@ use std::pin::Pin;
4
4
use std:: task:: { Context , Poll } ;
5
5
use std:: time:: Duration ;
6
6
7
- use tokio :: io :: { AsyncRead , AsyncWrite } ;
7
+ use hyper :: rt :: { Read , Write } ;
8
8
use tokio:: time:: timeout;
9
- use tokio_io_timeout:: TimeoutStream ;
10
9
11
- use hyper:: client:: connect:: { Connected , Connection } ;
12
- use hyper:: { service:: Service , Uri } ;
10
+ use hyper:: Uri ;
11
+ use hyper_util:: client:: legacy:: connect:: { Connected , Connection } ;
12
+ use tower_service:: Service ;
13
13
14
14
mod stream;
15
-
16
- use stream:: TimeoutConnectorStream ;
15
+ use stream:: TimeoutStream ;
17
16
18
17
type BoxError = Box < dyn std:: error:: Error + Send + Sync > ;
19
18
20
- /// A connector that enforces as connection timeout
19
+ /// A connector that enforces a connection timeout
21
20
#[ derive( Debug , Clone ) ]
22
21
pub struct TimeoutConnector < T > {
23
22
/// A connector implementing the `Connect` trait
@@ -33,7 +32,7 @@ pub struct TimeoutConnector<T> {
33
32
impl < T > TimeoutConnector < T >
34
33
where
35
34
T : Service < Uri > + Send ,
36
- T :: Response : AsyncRead + AsyncWrite + Send + Unpin ,
35
+ T :: Response : Read + Write + Send + Unpin ,
37
36
T :: Future : Send + ' static ,
38
37
T :: Error : Into < BoxError > ,
39
38
{
@@ -51,11 +50,11 @@ where
51
50
impl < T > Service < Uri > for TimeoutConnector < T >
52
51
where
53
52
T : Service < Uri > + Send ,
54
- T :: Response : AsyncRead + AsyncWrite + Connection + Send + Unpin ,
53
+ T :: Response : Read + Write + Connection + Send + Unpin ,
55
54
T :: Future : Send + ' static ,
56
55
T :: Error : Into < BoxError > ,
57
56
{
58
- type Response = Pin < Box < TimeoutConnectorStream < T :: Response > > > ;
57
+ type Response = Pin < Box < TimeoutStream < T :: Response > > > ;
59
58
type Error = BoxError ;
60
59
#[ allow( clippy:: type_complexity) ]
61
60
type Future = Pin < Box < dyn Future < Output = Result < Self :: Response , Self :: Error > > + Send > > ;
71
70
let connecting = self . connector . call ( dst) ;
72
71
73
72
let fut = async move {
74
- let stream = match connect_timeout {
73
+ let mut stream = match connect_timeout {
75
74
None => {
76
75
let io = connecting. await . map_err ( Into :: into) ?;
77
76
TimeoutStream :: new ( io)
85
84
TimeoutStream :: new ( io)
86
85
}
87
86
} ;
88
-
89
- let mut tm = TimeoutConnectorStream :: new ( stream) ;
90
- tm. set_read_timeout ( read_timeout) ;
91
- tm. set_write_timeout ( write_timeout) ;
92
- Ok ( Box :: pin ( tm) )
87
+ stream. set_read_timeout ( read_timeout) ;
88
+ stream. set_write_timeout ( write_timeout) ;
89
+ Ok ( Box :: pin ( stream) )
93
90
} ;
94
91
95
92
Box :: pin ( fut)
@@ -124,8 +121,8 @@ impl<T> TimeoutConnector<T> {
124
121
125
122
impl < T > Connection for TimeoutConnector < T >
126
123
where
127
- T : AsyncRead + AsyncWrite + Connection + Service < Uri > + Send + Unpin ,
128
- T :: Response : AsyncRead + AsyncWrite + Send + Unpin ,
124
+ T : Read + Write + Connection + Service < Uri > + Send + Unpin ,
125
+ T :: Response : Read + Write + Send + Unpin ,
129
126
T :: Future : Send + ' static ,
130
127
T :: Error : Into < BoxError > ,
131
128
{
@@ -136,12 +133,15 @@ where
136
133
137
134
#[ cfg( test) ]
138
135
mod tests {
139
- use std:: error:: Error ;
140
- use std:: io;
141
136
use std:: time:: Duration ;
137
+ use std:: { error:: Error , io} ;
142
138
143
- use hyper:: client:: HttpConnector ;
144
- use hyper:: Client ;
139
+ use http_body_util:: Empty ;
140
+ use hyper:: body:: Bytes ;
141
+ use hyper_util:: {
142
+ client:: legacy:: { connect:: HttpConnector , Client } ,
143
+ rt:: TokioExecutor ,
144
+ } ;
145
145
146
146
use super :: TimeoutConnector ;
147
147
@@ -154,7 +154,7 @@ mod tests {
154
154
let mut connector = TimeoutConnector :: new ( http) ;
155
155
connector. set_connect_timeout ( Some ( Duration :: from_millis ( 1 ) ) ) ;
156
156
157
- let client = Client :: builder ( ) . build :: < _ , hyper :: Body > ( connector) ;
157
+ let client = Client :: builder ( TokioExecutor :: new ( ) ) . build :: < _ , Empty < Bytes > > ( connector) ;
158
158
159
159
let res = client. get ( url) . await ;
160
160
@@ -179,19 +179,17 @@ mod tests {
179
179
// A 1 ms read timeout should be so short that we trigger a timeout error
180
180
connector. set_read_timeout ( Some ( Duration :: from_millis ( 1 ) ) ) ;
181
181
182
- let client = Client :: builder ( ) . build :: < _ , hyper :: Body > ( connector) ;
182
+ let client = Client :: builder ( TokioExecutor :: new ( ) ) . build :: < _ , Empty < Bytes > > ( connector) ;
183
183
184
184
let res = client. get ( url) . await ;
185
185
186
- match res {
187
- Ok ( _) => panic ! ( "Expected a timeout" ) ,
188
- Err ( e) => {
189
- if let Some ( io_e) = e. source ( ) . unwrap ( ) . downcast_ref :: < io:: Error > ( ) {
190
- assert_eq ! ( io_e. kind( ) , io:: ErrorKind :: TimedOut ) ;
191
- } else {
192
- panic ! ( "Expected timeout error" ) ;
186
+ if let Err ( client_e) = res {
187
+ if let Some ( hyper_e) = client_e. source ( ) {
188
+ if let Some ( io_e) = hyper_e. source ( ) . unwrap ( ) . downcast_ref :: < io:: Error > ( ) {
189
+ return assert_eq ! ( io_e. kind( ) , io:: ErrorKind :: TimedOut ) ;
193
190
}
194
191
}
195
192
}
193
+ panic ! ( "Expected timeout error" ) ;
196
194
}
197
195
}
0 commit comments