feat(all): add socket timeouts
Methods added to `Client` and `Server` to control read and write timeouts of the underlying socket. Keep-Alive is re-enabled by default on the server, with a default timeout of 5 seconds. BREAKING CHANGE: This adds 2 required methods to the `NetworkStream` trait, `set_read_timeout` and `set_write_timeout`. Any local implementations will need to add them.
This commit is contained in:
@@ -59,7 +59,6 @@ use std::default::Default;
|
||||
use std::io::{self, copy, Read};
|
||||
use std::iter::Extend;
|
||||
|
||||
#[cfg(feature = "timeouts")]
|
||||
use std::time::Duration;
|
||||
|
||||
use url::UrlParser;
|
||||
@@ -68,7 +67,7 @@ use url::ParseError as UrlError;
|
||||
use header::{Headers, Header, HeaderFormat};
|
||||
use header::{ContentLength, Location};
|
||||
use method::Method;
|
||||
use net::{NetworkConnector, NetworkStream, Fresh};
|
||||
use net::{NetworkConnector, NetworkStream};
|
||||
use {Url};
|
||||
use Error;
|
||||
|
||||
@@ -89,9 +88,7 @@ use http::h1::Http11Protocol;
|
||||
pub struct Client {
|
||||
protocol: Box<Protocol + Send + Sync>,
|
||||
redirect_policy: RedirectPolicy,
|
||||
#[cfg(feature = "timeouts")]
|
||||
read_timeout: Option<Duration>,
|
||||
#[cfg(feature = "timeouts")]
|
||||
write_timeout: Option<Duration>,
|
||||
}
|
||||
|
||||
@@ -113,16 +110,6 @@ impl Client {
|
||||
Client::with_protocol(Http11Protocol::with_connector(connector))
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "timeouts"))]
|
||||
/// Create a new client with a specific `Protocol`.
|
||||
pub fn with_protocol<P: Protocol + Send + Sync + 'static>(protocol: P) -> Client {
|
||||
Client {
|
||||
protocol: Box::new(protocol),
|
||||
redirect_policy: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "timeouts")]
|
||||
/// Create a new client with a specific `Protocol`.
|
||||
pub fn with_protocol<P: Protocol + Send + Sync + 'static>(protocol: P) -> Client {
|
||||
Client {
|
||||
@@ -139,13 +126,11 @@ impl Client {
|
||||
}
|
||||
|
||||
/// Set the read timeout value for all requests.
|
||||
#[cfg(feature = "timeouts")]
|
||||
pub fn set_read_timeout(&mut self, dur: Option<Duration>) {
|
||||
self.read_timeout = dur;
|
||||
}
|
||||
|
||||
/// Set the write timeout value for all requests.
|
||||
#[cfg(feature = "timeouts")]
|
||||
pub fn set_write_timeout(&mut self, dur: Option<Duration>) {
|
||||
self.write_timeout = dur;
|
||||
}
|
||||
@@ -273,19 +258,8 @@ impl<'a> RequestBuilder<'a> {
|
||||
let mut req = try!(Request::with_message(method.clone(), url.clone(), message));
|
||||
headers.as_ref().map(|headers| req.headers_mut().extend(headers.iter()));
|
||||
|
||||
#[cfg(not(feature = "timeouts"))]
|
||||
fn set_timeouts(_req: &mut Request<Fresh>, _client: &Client) -> ::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "timeouts")]
|
||||
fn set_timeouts(req: &mut Request<Fresh>, client: &Client) -> ::Result<()> {
|
||||
try!(req.set_write_timeout(client.write_timeout));
|
||||
try!(req.set_read_timeout(client.read_timeout));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
try!(set_timeouts(&mut req, &client));
|
||||
try!(req.set_write_timeout(client.write_timeout));
|
||||
try!(req.set_read_timeout(client.read_timeout));
|
||||
|
||||
match (can_have_body, body.as_ref()) {
|
||||
(true, Some(body)) => match body.size() {
|
||||
|
||||
@@ -5,7 +5,6 @@ use std::io::{self, Read, Write};
|
||||
use std::net::{SocketAddr, Shutdown};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[cfg(feature = "timeouts")]
|
||||
use std::time::Duration;
|
||||
|
||||
use net::{NetworkConnector, NetworkStream, DefaultConnector};
|
||||
@@ -176,13 +175,11 @@ impl<S: NetworkStream> NetworkStream for PooledStream<S> {
|
||||
self.inner.as_mut().unwrap().stream.peer_addr()
|
||||
}
|
||||
|
||||
#[cfg(feature = "timeouts")]
|
||||
#[inline]
|
||||
fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
|
||||
self.inner.as_ref().unwrap().stream.set_read_timeout(dur)
|
||||
}
|
||||
|
||||
#[cfg(feature = "timeouts")]
|
||||
#[inline]
|
||||
fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
|
||||
self.inner.as_ref().unwrap().stream.set_write_timeout(dur)
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
use std::marker::PhantomData;
|
||||
use std::io::{self, Write};
|
||||
|
||||
#[cfg(feature = "timeouts")]
|
||||
use std::time::Duration;
|
||||
|
||||
use url::Url;
|
||||
@@ -44,14 +43,12 @@ impl<W> Request<W> {
|
||||
pub fn method(&self) -> method::Method { self.method.clone() }
|
||||
|
||||
/// Set the write timeout.
|
||||
#[cfg(feature = "timeouts")]
|
||||
#[inline]
|
||||
pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
|
||||
self.message.set_write_timeout(dur)
|
||||
}
|
||||
|
||||
/// Set the read timeout.
|
||||
#[cfg(feature = "timeouts")]
|
||||
#[inline]
|
||||
pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
|
||||
self.message.set_read_timeout(dur)
|
||||
|
||||
Reference in New Issue
Block a user