diff --git a/awc/CHANGES.md b/awc/CHANGES.md index dcc38c31..cd97ed86 100644 --- a/awc/CHANGES.md +++ b/awc/CHANGES.md @@ -1,17 +1,18 @@ # Changes -## [0.1.0-alpha.2] - 2019-03-xx +## [0.1.0-alpha.2] - 2019-03-29 ### Added -* Request timeout. - -* Re-export `actix_http::client::Connector`. +* Per request and session wide request timeout. * Session wide headers. * Session wide basic and bearer auth. +* Re-export `actix_http::client::Connector`. + + ### Changed * Export `ws` sub-module with websockets related types diff --git a/awc/src/builder.rs b/awc/src/builder.rs index 6ef145bf..dcea5595 100644 --- a/awc/src/builder.rs +++ b/awc/src/builder.rs @@ -87,7 +87,7 @@ impl ClientBuilder { self } - /// Add default header. Headers adds byt this method + /// Add default header. Headers added by this method /// get added to every request. pub fn header(mut self, key: K, value: V) -> Self where diff --git a/awc/src/request.rs b/awc/src/request.rs index 170be75f..a29c3e60 100644 --- a/awc/src/request.rs +++ b/awc/src/request.rs @@ -1,6 +1,7 @@ use std::fmt; use std::io::Write; use std::rc::Rc; +use std::time::Duration; use bytes::{BufMut, Bytes, BytesMut}; #[cfg(feature = "cookies")] @@ -62,6 +63,7 @@ pub struct ClientRequest { cookies: Option, default_headers: bool, response_decompress: bool, + timeout: Option, config: Rc, } @@ -86,6 +88,7 @@ impl ClientRequest { config, #[cfg(feature = "cookies")] cookies: None, + timeout: None, default_headers: true, response_decompress: true, } @@ -309,6 +312,15 @@ impl ClientRequest { self } + /// Set request timeout. Overrides client wide timeout setting. + /// + /// Request timeout is the total time before a response must be received. + /// Default value is 5 seconds. + pub fn timeout(mut self, timeout: Duration) -> Self { + self.timeout = Some(timeout); + self + } + /// This method calls provided closure with builder reference if /// value is `true`. pub fn if_true(mut self, value: bool, f: F) -> Self @@ -443,10 +455,10 @@ impl ClientRequest { } } + let config = slf.config; let response_decompress = slf.response_decompress; - let fut = slf - .config + let fut = config .connector .borrow_mut() .send_request(head, body.into()) @@ -461,7 +473,7 @@ impl ClientRequest { }); // set request timeout - if let Some(timeout) = slf.config.timeout { + if let Some(timeout) = slf.timeout.or_else(|| config.timeout.clone()) { Either::B(Either::A(Timeout::new(fut, timeout).map_err(|e| { if let Some(e) = e.into_inner() { e diff --git a/awc/tests/test_client.rs b/awc/tests/test_client.rs index b2d6f8e9..51791d67 100644 --- a/awc/tests/test_client.rs +++ b/awc/tests/test_client.rs @@ -83,6 +83,32 @@ fn test_timeout() { } } +#[test] +fn test_timeout_override() { + let mut srv = TestServer::new(|| { + HttpService::new(App::new().service(web::resource("/").route(web::to_async( + || { + tokio_timer::sleep(Duration::from_millis(200)) + .then(|_| Ok::<_, Error>(HttpResponse::Ok().body(STR))) + }, + )))) + }); + + let client = srv.execute(|| { + awc::Client::build() + .timeout(Duration::from_millis(50000)) + .finish() + }); + let request = client + .get(srv.url("/")) + .timeout(Duration::from_millis(50)) + .send(); + match srv.block_on(request) { + Err(SendRequestError::Timeout) => (), + _ => panic!(), + } +} + // #[test] // fn test_connection_close() { // let mut srv =