From cf133f79190a590c891e526db1847e5c63a3bfe7 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Wed, 30 Aug 2017 13:46:13 -0700 Subject: [PATCH] change default Client timeout to 30 seconds Closes #181 --- src/client.rs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/client.rs b/src/client.rs index e75e82d..d27174d 100644 --- a/src/client.rs +++ b/src/client.rs @@ -53,7 +53,7 @@ pub struct Client { /// ``` pub struct ClientBuilder { inner: async_impl::ClientBuilder, - timeout: Option, + timeout: Timeout, } impl ClientBuilder { @@ -65,7 +65,7 @@ impl ClientBuilder { pub fn new() -> ::Result { async_impl::ClientBuilder::new().map(|builder| ClientBuilder { inner: builder, - timeout: None, + timeout: Timeout::default(), }) } @@ -212,9 +212,15 @@ impl ClientBuilder { } /// Set a timeout for connect, read and write operations of a `Client`. + /// + /// Default is 30 seconds. + /// + /// Pass `None` to disable timeout. #[inline] - pub fn timeout(&mut self, timeout: Duration) -> &mut ClientBuilder { - self.timeout = Some(timeout); + pub fn timeout(&mut self, timeout: T) -> &mut ClientBuilder + where T: Into>, + { + self.timeout = Timeout(timeout.into()); self } } @@ -344,7 +350,7 @@ impl fmt::Debug for ClientBuilder { #[derive(Clone)] struct ClientHandle { - timeout: Option, + timeout: Timeout, inner: Arc } @@ -404,7 +410,7 @@ impl ClientHandle { let _ = core.run(work); })); - wait::timeout(spawn_rx, timeout).expect("core thread cancelled")?; + wait::timeout(spawn_rx, timeout.0).expect("core thread cancelled")?; let inner_handle = Arc::new(InnerClientHandle { tx: Some(tx), @@ -432,7 +438,7 @@ impl ClientHandle { try_!(body.send(), &url); } - let res = match wait::timeout(rx, self.timeout) { + let res = match wait::timeout(rx, self.timeout.0) { Ok(res) => res, Err(wait::Waited::TimedOut) => return Err(::error::timedout(Some(url))), Err(wait::Waited::Err(_canceled)) => { @@ -445,11 +451,22 @@ impl ClientHandle { } }; res.map(|res| { - response::new(res, self.timeout, KeepCoreThreadAlive(self.inner.clone())) + response::new(res, self.timeout.0, KeepCoreThreadAlive(self.inner.clone())) }) } } +#[derive(Clone, Copy)] +struct Timeout(Option); + +impl Default for Timeout { + #[inline] + fn default() -> Timeout { + // default mentioned in ClientBuilder::timeout() doc comment + Timeout(Some(Duration::from_secs(30))) + } +} + // pub(crate) pub struct KeepCoreThreadAlive(Arc);