Skip to content

Commit 27eb736

Browse files
committed
Clean up Service implementation
1 parent 3f8136a commit 27eb736

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

src/lib.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,33 +59,28 @@ where
5959
}
6060

6161
fn call(&mut self, dst: Uri) -> Self::Future {
62+
let connect_timeout = self.connect_timeout;
6263
let read_timeout = self.read_timeout;
6364
let write_timeout = self.write_timeout;
6465
let connecting = self.connector.call(dst);
6566

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

88-
let mut tm = TimeoutConnectorStream::new(TimeoutStream::new(io));
83+
let mut tm = TimeoutConnectorStream::new(stream);
8984
tm.set_read_timeout(read_timeout);
9085
tm.set_write_timeout(write_timeout);
9186
Ok(Box::pin(tm))

0 commit comments

Comments
 (0)