Skip to content

Commit 22e2e95

Browse files
authored
Merge pull request #19 from hjr3/duration
Duration
2 parents 1aecd5a + 27eb736 commit 22e2e95

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

src/lib.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use std::future::Future;
22
use std::io;
33
use std::pin::Pin;
44
use std::task::{Context, Poll};
5+
use std::time::Duration;
56

67
use tokio::io::{AsyncRead, AsyncWrite};
7-
use tokio::time::{timeout, Duration};
8+
use tokio::time::timeout;
89
use tokio_io_timeout::TimeoutStream;
910

1011
use hyper::client::connect::{Connect, Connected, Connection};
@@ -58,33 +59,28 @@ where
5859
}
5960

6061
fn call(&mut self, dst: Uri) -> Self::Future {
62+
let connect_timeout = self.connect_timeout;
6163
let read_timeout = self.read_timeout;
6264
let write_timeout = self.write_timeout;
6365
let connecting = self.connector.call(dst);
6466

65-
if self.connect_timeout.is_none() {
66-
let fut = async move {
67-
let io = connecting.await.map_err(Into::into)?;
68-
69-
let mut tm = TimeoutConnectorStream::new(TimeoutStream::new(io));
70-
tm.set_read_timeout(read_timeout);
71-
tm.set_write_timeout(write_timeout);
72-
Ok(Box::pin(tm))
73-
};
74-
75-
return Box::pin(fut);
76-
}
77-
78-
let connect_timeout = self.connect_timeout.expect("Connect timeout should be set");
79-
let timeout = timeout(connect_timeout, connecting);
80-
8167
let fut = async move {
82-
let connecting = timeout
83-
.await
84-
.map_err(|e| io::Error::new(io::ErrorKind::TimedOut, e))?;
85-
let io = connecting.map_err(Into::into)?;
68+
let stream = match connect_timeout {
69+
None => {
70+
let io = connecting.await.map_err(Into::into)?;
71+
TimeoutStream::new(io)
72+
}
73+
Some(connect_timeout) => {
74+
let timeout = timeout(connect_timeout, connecting);
75+
let connecting = timeout
76+
.await
77+
.map_err(|e| io::Error::new(io::ErrorKind::TimedOut, e))?;
78+
let io = connecting.map_err(Into::into)?;
79+
TimeoutStream::new(io)
80+
}
81+
};
8682

87-
let mut tm = TimeoutConnectorStream::new(TimeoutStream::new(io));
83+
let mut tm = TimeoutConnectorStream::new(stream);
8884
tm.set_read_timeout(read_timeout);
8985
tm.set_write_timeout(write_timeout);
9086
Ok(Box::pin(tm))

0 commit comments

Comments
 (0)