Merge pull request #195 from seanmonstar/default-timeout

change default Client timeout to 30 seconds
This commit is contained in:
Sean McArthur
2017-08-30 16:24:41 -07:00
committed by GitHub

View File

@@ -53,7 +53,7 @@ pub struct Client {
/// ``` /// ```
pub struct ClientBuilder { pub struct ClientBuilder {
inner: async_impl::ClientBuilder, inner: async_impl::ClientBuilder,
timeout: Option<Duration>, timeout: Timeout,
} }
impl ClientBuilder { impl ClientBuilder {
@@ -65,7 +65,7 @@ impl ClientBuilder {
pub fn new() -> ::Result<ClientBuilder> { pub fn new() -> ::Result<ClientBuilder> {
async_impl::ClientBuilder::new().map(|builder| ClientBuilder { async_impl::ClientBuilder::new().map(|builder| ClientBuilder {
inner: builder, 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`. /// Set a timeout for connect, read and write operations of a `Client`.
///
/// Default is 30 seconds.
///
/// Pass `None` to disable timeout.
#[inline] #[inline]
pub fn timeout(&mut self, timeout: Duration) -> &mut ClientBuilder { pub fn timeout<T>(&mut self, timeout: T) -> &mut ClientBuilder
self.timeout = Some(timeout); where T: Into<Option<Duration>>,
{
self.timeout = Timeout(timeout.into());
self self
} }
} }
@@ -344,7 +350,7 @@ impl fmt::Debug for ClientBuilder {
#[derive(Clone)] #[derive(Clone)]
struct ClientHandle { struct ClientHandle {
timeout: Option<Duration>, timeout: Timeout,
inner: Arc<InnerClientHandle> inner: Arc<InnerClientHandle>
} }
@@ -404,7 +410,7 @@ impl ClientHandle {
let _ = core.run(work); 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 { let inner_handle = Arc::new(InnerClientHandle {
tx: Some(tx), tx: Some(tx),
@@ -432,7 +438,7 @@ impl ClientHandle {
try_!(body.send(), &url); try_!(body.send(), &url);
} }
let res = match wait::timeout(rx, self.timeout) { let res = match wait::timeout(rx, self.timeout.0) {
Ok(res) => res, Ok(res) => res,
Err(wait::Waited::TimedOut) => return Err(::error::timedout(Some(url))), Err(wait::Waited::TimedOut) => return Err(::error::timedout(Some(url))),
Err(wait::Waited::Err(_canceled)) => { Err(wait::Waited::Err(_canceled)) => {
@@ -445,11 +451,22 @@ impl ClientHandle {
} }
}; };
res.map(|res| { 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<Duration>);
impl Default for Timeout {
#[inline]
fn default() -> Timeout {
// default mentioned in ClientBuilder::timeout() doc comment
Timeout(Some(Duration::from_secs(30)))
}
}
// pub(crate) // pub(crate)
pub struct KeepCoreThreadAlive(Arc<InnerClientHandle>); pub struct KeepCoreThreadAlive(Arc<InnerClientHandle>);