1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 01:32:57 +01:00

allow set client request/ws timeout

This commit is contained in:
Nikolay Kim 2018-03-18 19:27:51 -07:00
parent 6b1a79fab8
commit f4a47ef71e
3 changed files with 34 additions and 1 deletions

View File

@ -4,6 +4,10 @@
* Use `Error` instead of `InternalError` for `error::ErrorXXXX` methods * Use `Error` instead of `InternalError` for `error::ErrorXXXX` methods
* Allow to set client request timeout
* Allow to set client websocket handshake timeout
## 0.4.9 (2018-03-16) ## 0.4.9 (2018-03-16)

View File

@ -1,6 +1,7 @@
use std::{fmt, mem}; use std::{fmt, mem};
use std::fmt::Write as FmtWrite; use std::fmt::Write as FmtWrite;
use std::io::Write; use std::io::Write;
use std::time::Duration;
use actix::{Addr, Unsync}; use actix::{Addr, Unsync};
use cookie::{Cookie, CookieJar}; use cookie::{Cookie, CookieJar};
@ -26,6 +27,7 @@ pub struct ClientRequest {
body: Body, body: Body,
chunked: bool, chunked: bool,
upgrade: bool, upgrade: bool,
timeout: Option<Duration>,
encoding: ContentEncoding, encoding: ContentEncoding,
response_decompress: bool, response_decompress: bool,
buffer_capacity: usize, buffer_capacity: usize,
@ -49,6 +51,7 @@ impl Default for ClientRequest {
body: Body::Empty, body: Body::Empty,
chunked: false, chunked: false,
upgrade: false, upgrade: false,
timeout: None,
encoding: ContentEncoding::Auto, encoding: ContentEncoding::Auto,
response_decompress: true, response_decompress: true,
buffer_capacity: 32_768, buffer_capacity: 32_768,
@ -204,10 +207,16 @@ impl ClientRequest {
/// ///
/// This method returns future that resolves to a ClientResponse /// This method returns future that resolves to a ClientResponse
pub fn send(mut self) -> SendRequest { pub fn send(mut self) -> SendRequest {
match mem::replace(&mut self.conn, ConnectionType::Default) { let timeout = self.timeout.take();
let send = match mem::replace(&mut self.conn, ConnectionType::Default) {
ConnectionType::Default => SendRequest::new(self), ConnectionType::Default => SendRequest::new(self),
ConnectionType::Connector(conn) => SendRequest::with_connector(self, conn), ConnectionType::Connector(conn) => SendRequest::with_connector(self, conn),
ConnectionType::Connection(conn) => SendRequest::with_connection(self, conn), ConnectionType::Connection(conn) => SendRequest::with_connection(self, conn),
};
if let Some(timeout) = timeout {
send.timeout(timeout)
} else {
send
} }
} }
} }
@ -476,6 +485,17 @@ impl ClientRequestBuilder {
self self
} }
/// Set request timeout
///
/// Request timeout is a total time before response should be received.
/// Default value is 5 seconds.
pub fn timeout(&mut self, timeout: Duration) -> &mut Self {
if let Some(parts) = parts(&mut self.request, &self.err) {
parts.timeout = Some(timeout);
}
self
}
/// Send request using custom connector /// Send request using custom connector
pub fn with_connector(&mut self, conn: Addr<Unsync, ClientConnector>) -> &mut Self { pub fn with_connector(&mut self, conn: Addr<Unsync, ClientConnector>) -> &mut Self {
if let Some(parts) = parts(&mut self.request, &self.err) { if let Some(parts) = parts(&mut self.request, &self.err) {

View File

@ -209,6 +209,15 @@ impl Client {
self self
} }
/// Set websocket handshake timeout
///
/// Handshake timeout is a total time for successful handshake.
/// Default value is 5 seconds.
pub fn timeout(mut self, timeout: Duration) -> Self {
self.request.timeout(timeout);
self
}
/// Connect to websocket server and do ws handshake /// Connect to websocket server and do ws handshake
pub fn connect(&mut self) -> ClientHandshake { pub fn connect(&mut self) -> ClientHandshake {
if let Some(e) = self.err.take() { if let Some(e) = self.err.take() {