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:
Sean McArthur
2015-11-24 10:27:46 -08:00
parent 21c4f51ad5
commit fec6e3e873
12 changed files with 24 additions and 109 deletions

View File

@@ -3,9 +3,7 @@ use std::io::{self, Read, Write, Cursor};
use std::cell::RefCell;
use std::net::{SocketAddr, Shutdown};
use std::sync::{Arc, Mutex};
#[cfg(feature = "timeouts")]
use std::time::Duration;
#[cfg(feature = "timeouts")]
use std::cell::Cell;
use solicit::http::HttpScheme;
@@ -24,9 +22,7 @@ pub struct MockStream {
pub is_closed: bool,
pub error_on_write: bool,
pub error_on_read: bool,
#[cfg(feature = "timeouts")]
pub read_timeout: Cell<Option<Duration>>,
#[cfg(feature = "timeouts")]
pub write_timeout: Cell<Option<Duration>>,
}
@@ -45,7 +41,6 @@ impl MockStream {
MockStream::with_responses(vec![input])
}
#[cfg(feature = "timeouts")]
pub fn with_responses(mut responses: Vec<&[u8]>) -> MockStream {
MockStream {
read: Cursor::new(responses.remove(0).to_vec()),
@@ -58,18 +53,6 @@ impl MockStream {
write_timeout: Cell::new(None),
}
}
#[cfg(not(feature = "timeouts"))]
pub fn with_responses(mut responses: Vec<&[u8]>) -> MockStream {
MockStream {
read: Cursor::new(responses.remove(0).to_vec()),
next_reads: responses.into_iter().map(|arr| arr.to_vec()).collect(),
write: vec![],
is_closed: false,
error_on_write: false,
error_on_read: false,
}
}
}
impl Read for MockStream {
@@ -111,13 +94,11 @@ impl NetworkStream for MockStream {
Ok("127.0.0.1:1337".parse().unwrap())
}
#[cfg(feature = "timeouts")]
fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
self.read_timeout.set(dur);
Ok(())
}
#[cfg(feature = "timeouts")]
fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
self.write_timeout.set(dur);
Ok(())
@@ -167,12 +148,10 @@ impl NetworkStream for CloneableMockStream {
self.inner.lock().unwrap().peer_addr()
}
#[cfg(feature = "timeouts")]
fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
self.inner.lock().unwrap().set_read_timeout(dur)
}
#[cfg(feature = "timeouts")]
fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
self.inner.lock().unwrap().set_write_timeout(dur)
}