1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 09:42:57 +01:00

add per request timeout

This commit is contained in:
Nikolay Kim 2019-03-29 14:07:37 -07:00
parent 058b1d56e6
commit 744d82431d
4 changed files with 47 additions and 8 deletions

View File

@ -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

View File

@ -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<K, V>(mut self, key: K, value: V) -> Self
where

View File

@ -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<CookieJar>,
default_headers: bool,
response_decompress: bool,
timeout: Option<Duration>,
config: Rc<ClientConfig>,
}
@ -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<F>(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

View File

@ -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 =