11use std:: fmt;
22use std:: io:: { self , Read } ;
3- use std:: sync:: { Arc , Mutex } ;
3+ use std:: sync:: { Arc , Mutex , RwLock } ;
44use std:: sync:: atomic:: { AtomicBool , Ordering } ;
5+ use std:: time:: Duration ;
56
67use hyper:: client:: IntoUrl ;
78use hyper:: header:: { Headers , ContentType , Location , Referer , UserAgent , Accept , ContentEncoding , Encoding , ContentLength ,
@@ -38,7 +39,7 @@ impl Client {
3839 client. set_redirect_policy ( :: hyper:: client:: RedirectPolicy :: FollowNone ) ;
3940 Ok ( Client {
4041 inner : Arc :: new ( ClientRef {
41- hyper : client,
42+ hyper : RwLock :: new ( client) ,
4243 redirect_policy : Mutex :: new ( RedirectPolicy :: default ( ) ) ,
4344 auto_ungzip : AtomicBool :: new ( true ) ,
4445 } ) ,
@@ -55,6 +56,13 @@ impl Client {
5556 * self . inner . redirect_policy . lock ( ) . unwrap ( ) = policy;
5657 }
5758
59+ /// Set a timeout for both the read and write operations of a client.
60+ pub fn timeout ( & mut self , timeout : Duration ) {
61+ let mut client = self . inner . hyper . write ( ) . unwrap ( ) ;
62+ client. set_read_timeout ( Some ( timeout) ) ;
63+ client. set_write_timeout ( Some ( timeout) ) ;
64+ }
65+
5866 /// Convenience method to make a `GET` request to a URL.
5967 pub fn get < U : IntoUrl > ( & self , url : U ) -> RequestBuilder {
6068 self . request ( Method :: Get , url)
@@ -113,7 +121,7 @@ impl fmt::Debug for Client {
113121}
114122
115123struct ClientRef {
116- hyper : :: hyper:: Client ,
124+ hyper : RwLock < :: hyper:: Client > ,
117125 redirect_policy : Mutex < RedirectPolicy > ,
118126 auto_ungzip : AtomicBool ,
119127}
@@ -241,7 +249,8 @@ impl RequestBuilder {
241249 loop {
242250 let res = {
243251 debug ! ( "request {:?} \" {}\" " , method, url) ;
244- let mut req = client. hyper . request ( method. clone ( ) , url. clone ( ) )
252+ let c = client. hyper . read ( ) . unwrap ( ) ;
253+ let mut req = c. request ( method. clone ( ) , url. clone ( ) )
245254 . headers ( headers. clone ( ) ) ;
246255
247256 if let Some ( ref mut b) = body {
0 commit comments