Repository: seanmonstar/reqwest (language: rust)

Issue:
Support timeouts
First, thank you for this higher level crate, it's appreciated!

We're looking at switching from hyper's client to reqwest [in Rusoto](https://github.com/rusoto/rusoto/issues/486) which will be nice to avoid openssl issues and make our code more concise.  We use the [set_read_timeout](http://hyper.rs/hyper/v0.9.12/hyper/client/struct.Client.html#method.set_read_timeout) of hyper's client in a couple spots to avoid waiting around too long.  

Are there plans for adding this to reqwest?  At first glance adding it to RequestBuilder would be a good spot.  I've not looked at much of the reqwest code to see how it'd be added, though.

Rusoto use of set_read_timeout is [here](https://github.com/rusoto/rusoto/blob/master/credential/src/instance_metadata.rs#L20).


Discussion / hints:
I would definitely like to support timeouts. When looking at something like golang's HTTP Client, it only provides a single timeout, which acts more like a deadline for the whole operation. If you set the timeout to 20 seconds, if the combined operations take longer than 20 seconds, you will receive a timeout error. I feel that this functionality is more often expected from people, instead of how hyper currently does it, which is a timeout per read/write, meaning a very slow connection could still take minutes to complete.

Unfortunately, it is not possible to enforce this kind of timeout using hyper 0.9, because of its use of blocking IO. Once hyper 0.10 is released, this will be possible. I'm not quite sure how I should expose timeouts until then.

Fix patch:
diff --git a/src/client.rs b/src/client.rs
index 5f40fcfda..d83145a0d 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -1,7 +1,8 @@
 use std::fmt;
 use std::io::{self, Read};
-use std::sync::{Arc, Mutex};
+use std::sync::{Arc, Mutex, RwLock};
 use std::sync::atomic::{AtomicBool, Ordering};
+use std::time::Duration;
 
 use hyper::client::IntoUrl;
 use hyper::header::{Headers, ContentType, Location, Referer, UserAgent, Accept, ContentEncoding, Encoding, ContentLength,
@@ -38,7 +39,7 @@ impl Client {
         client.set_redirect_policy(::hyper::client::RedirectPolicy::FollowNone);
         Ok(Client {
             inner: Arc::new(ClientRef {
-                hyper: client,
+                hyper: RwLock::new(client),
                 redirect_policy: Mutex::new(RedirectPolicy::default()),
                 auto_ungzip: AtomicBool::new(true),
             }),
@@ -55,6 +56,13 @@ impl Client {
         *self.inner.redirect_policy.lock().unwrap() = policy;
     }
 
+    /// Set a timeout for both the read and write operations of a client.
+    pub fn timeout(&mut self, timeout: Duration) {
+        let mut client = self.inner.hyper.write().unwrap();
+        client.set_read_timeout(Some(timeout));
+        client.set_write_timeout(Some(timeout));
+    }
+
     /// Convenience method to make a `GET` request to a URL.
     pub fn get<U: IntoUrl>(&self, url: U) -> RequestBuilder {
         self.request(Method::Get, url)
@@ -113,7 +121,7 @@ impl fmt::Debug for Client {
 }
 
 struct ClientRef {
-    hyper: ::hyper::Client,
+    hyper: RwLock<::hyper::Client>,
     redirect_policy: Mutex<RedirectPolicy>,
     auto_ungzip: AtomicBool,
 }
@@ -241,7 +249,8 @@ impl RequestBuilder {
         loop {
             let res = {
                 debug!("request {:?} \"{}\"", method, url);
-                let mut req = client.hyper.request(method.clone(), url.clone())
+                let c = client.hyper.read().unwrap();
+                let mut req = c.request(method.clone(), url.clone())
                     .headers(headers.clone());
 
                 if let Some(ref mut b) = body {


Test patch: