mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-30 18:44:35 +01:00
Allow connection timeout to be set
This commit is contained in:
parent
e708f51156
commit
be7e8d159b
@ -1,5 +1,9 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## 0.4.5
|
||||||
|
|
||||||
|
* Allow connection timeout to be set
|
||||||
|
|
||||||
## 0.4.4 (2018-03-04)
|
## 0.4.4 (2018-03-04)
|
||||||
|
|
||||||
* Allow to use Arc<Vec<u8>> as response/request body
|
* Allow to use Arc<Vec<u8>> as response/request body
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use std::{io, time};
|
use std::{io, time};
|
||||||
use std::net::Shutdown;
|
use std::net::Shutdown;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use actix::{fut, Actor, ActorFuture, Context,
|
use actix::{fut, Actor, ActorFuture, Context,
|
||||||
Handler, Message, ActorResponse, Supervised};
|
Handler, Message, ActorResponse, Supervised};
|
||||||
@ -25,12 +26,18 @@ use server::IoStream;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
/// `Connect` type represents message that can be send to `ClientConnector`
|
/// `Connect` type represents message that can be send to `ClientConnector`
|
||||||
/// with connection request.
|
/// with connection request.
|
||||||
pub struct Connect(pub Uri);
|
pub struct Connect {
|
||||||
|
pub uri: Uri,
|
||||||
|
pub connection_timeout: Duration
|
||||||
|
}
|
||||||
|
|
||||||
impl Connect {
|
impl Connect {
|
||||||
/// Create `Connect` message for specified `Uri`
|
/// Create `Connect` message for specified `Uri`
|
||||||
pub fn new<U>(uri: U) -> Result<Connect, HttpError> where Uri: HttpTryFrom<U> {
|
pub fn new<U>(uri: U) -> Result<Connect, HttpError> where Uri: HttpTryFrom<U> {
|
||||||
Ok(Connect(Uri::try_from(uri).map_err(|e| e.into())?))
|
Ok(Connect {
|
||||||
|
uri: Uri::try_from(uri).map_err(|e| e.into())?,
|
||||||
|
connection_timeout: Duration::from_secs(1)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,7 +166,8 @@ impl Handler<Connect> for ClientConnector {
|
|||||||
type Result = ActorResponse<ClientConnector, Connection, ClientConnectorError>;
|
type Result = ActorResponse<ClientConnector, Connection, ClientConnectorError>;
|
||||||
|
|
||||||
fn handle(&mut self, msg: Connect, _: &mut Self::Context) -> Self::Result {
|
fn handle(&mut self, msg: Connect, _: &mut Self::Context) -> Self::Result {
|
||||||
let uri = &msg.0;
|
let uri = &msg.uri;
|
||||||
|
let connection_timeout = msg.connection_timeout;
|
||||||
|
|
||||||
// host name is required
|
// host name is required
|
||||||
if uri.host().is_none() {
|
if uri.host().is_none() {
|
||||||
@ -185,7 +193,7 @@ impl Handler<Connect> for ClientConnector {
|
|||||||
|
|
||||||
ActorResponse::async(
|
ActorResponse::async(
|
||||||
Connector::from_registry()
|
Connector::from_registry()
|
||||||
.send(ResolveConnect::host_and_port(&host, port))
|
.send(ResolveConnect::host_and_port(&host, port).timeout(connection_timeout))
|
||||||
.into_actor(self)
|
.into_actor(self)
|
||||||
.map_err(|_, _, _| ClientConnectorError::Disconnected)
|
.map_err(|_, _, _| ClientConnectorError::Disconnected)
|
||||||
.and_then(move |res, _act, _| {
|
.and_then(move |res, _act, _| {
|
||||||
|
@ -86,7 +86,10 @@ impl Future for SendRequest {
|
|||||||
|
|
||||||
match state {
|
match state {
|
||||||
State::New =>
|
State::New =>
|
||||||
self.state = State::Connect(self.conn.send(Connect(self.req.uri().clone()))),
|
self.state = State::Connect(self.conn.send(Connect {
|
||||||
|
uri: self.req.uri().clone(),
|
||||||
|
connection_timeout: self.req.connection_timeout()
|
||||||
|
})),
|
||||||
State::Connect(mut conn) => match conn.poll() {
|
State::Connect(mut conn) => match conn.poll() {
|
||||||
Ok(Async::NotReady) => {
|
Ok(Async::NotReady) => {
|
||||||
self.state = State::Connect(conn);
|
self.state = State::Connect(conn);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use std::{fmt, mem};
|
use std::{fmt, mem};
|
||||||
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};
|
||||||
@ -28,6 +29,7 @@ pub struct ClientRequest {
|
|||||||
response_decompress: bool,
|
response_decompress: bool,
|
||||||
buffer_capacity: Option<(usize, usize)>,
|
buffer_capacity: Option<(usize, usize)>,
|
||||||
conn: ConnectionType,
|
conn: ConnectionType,
|
||||||
|
connection_timeout: Duration
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,6 +54,7 @@ impl Default for ClientRequest {
|
|||||||
response_decompress: true,
|
response_decompress: true,
|
||||||
buffer_capacity: None,
|
buffer_capacity: None,
|
||||||
conn: ConnectionType::Default,
|
conn: ConnectionType::Default,
|
||||||
|
connection_timeout: Duration::from_secs(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -102,7 +105,7 @@ impl ClientRequest {
|
|||||||
request: Some(ClientRequest::default()),
|
request: Some(ClientRequest::default()),
|
||||||
err: None,
|
err: None,
|
||||||
cookies: None,
|
cookies: None,
|
||||||
default_headers: true,
|
default_headers: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,6 +115,11 @@ impl ClientRequest {
|
|||||||
&self.uri
|
&self.uri
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn connection_timeout(&self) -> Duration {
|
||||||
|
self.connection_timeout
|
||||||
|
}
|
||||||
|
|
||||||
/// Set client request uri
|
/// Set client request uri
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn set_uri(&mut self, uri: Uri) {
|
pub fn set_uri(&mut self, uri: Uri) {
|
||||||
@ -236,7 +244,7 @@ pub struct ClientRequestBuilder {
|
|||||||
request: Option<ClientRequest>,
|
request: Option<ClientRequest>,
|
||||||
err: Option<HttpError>,
|
err: Option<HttpError>,
|
||||||
cookies: Option<CookieJar>,
|
cookies: Option<CookieJar>,
|
||||||
default_headers: bool,
|
default_headers: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClientRequestBuilder {
|
impl ClientRequestBuilder {
|
||||||
@ -361,6 +369,15 @@ impl ClientRequestBuilder {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set connection timeout
|
||||||
|
#[inline]
|
||||||
|
pub fn connection_timeout(&mut self, connection_timeout: Duration) -> &mut Self {
|
||||||
|
if let Some(ref mut request) = self.request {
|
||||||
|
request.connection_timeout = connection_timeout;
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set request's content type
|
/// Set request's content type
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn content_type<V>(&mut self, value: V) -> &mut Self
|
pub fn content_type<V>(&mut self, value: V) -> &mut Self
|
||||||
@ -562,7 +579,7 @@ impl ClientRequestBuilder {
|
|||||||
request: self.request.take(),
|
request: self.request.take(),
|
||||||
err: self.err.take(),
|
err: self.err.take(),
|
||||||
cookies: self.cookies.take(),
|
cookies: self.cookies.take(),
|
||||||
default_headers: self.default_headers,
|
default_headers: self.default_headers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user