diff --git a/CHANGES.md b/CHANGES.md index 310466b5a..72c89b3a8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,9 @@ ## 0.4.8 (2018-03-12) -* Do not log WouldBlock error from socket accept +* Allow to set read buffer capacity for server request + +* Handle WouldBlock error for socket accept call ## 0.4.7 (2018-03-11) diff --git a/Cargo.toml b/Cargo.toml index 9870c26bc..90653a152 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-web" -version = "0.4.7" +version = "0.4.8" authors = ["Nikolay Kim "] description = "Actix web is a simple, pragmatic, extremely fast, web framework for Rust." readme = "README.md" diff --git a/src/httprequest.rs b/src/httprequest.rs index 41d0cf9fb..f3ba8211c 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -389,6 +389,15 @@ impl HttpRequest { self.as_ref().method == Method::CONNECT } + /// Set read buffer capacity + /// + /// Default buffer capacity is 32Kb. + pub fn set_read_buffer_capacity(&mut self, cap: usize) { + if let Some(ref mut payload) = self.as_mut().payload { + payload.set_read_buffer_capacity(cap) + } + } + #[cfg(test)] pub(crate) fn payload(&self) -> &Payload { let msg = self.as_mut(); diff --git a/src/payload.rs b/src/payload.rs index 69f8aab61..13e48efca 100644 --- a/src/payload.rs +++ b/src/payload.rs @@ -8,6 +8,10 @@ use futures::{Async, Poll, Stream}; use error::PayloadError; +/// max buffer size 32k +pub(crate) const MAX_BUFFER_SIZE: usize = 32_768; + + #[derive(Debug, PartialEq)] pub(crate) enum PayloadStatus { Read, @@ -76,6 +80,14 @@ impl Payload { pub(crate) fn readall(&self) -> Option { self.inner.borrow_mut().readall() } + + #[inline] + /// Set read buffer capacity + /// + /// Default buffer capacity is 32Kb. + pub fn set_read_buffer_capacity(&mut self, cap: usize) { + self.inner.borrow_mut().capacity = cap; + } } impl Stream for Payload { @@ -161,6 +173,7 @@ struct Inner { err: Option, need_read: bool, items: VecDeque, + capacity: usize, } impl Inner { @@ -172,6 +185,7 @@ impl Inner { err: None, items: VecDeque::new(), need_read: true, + capacity: MAX_BUFFER_SIZE, } } @@ -188,8 +202,8 @@ impl Inner { #[inline] fn feed_data(&mut self, data: Bytes) { self.len += data.len(); - self.need_read = false; self.items.push_back(data); + self.need_read = self.len < self.capacity; } #[inline] @@ -222,6 +236,7 @@ impl Inner { fn readany(&mut self) -> Poll, PayloadError> { if let Some(data) = self.items.pop_front() { self.len -= data.len(); + self.need_read = self.len < self.capacity; Ok(Async::Ready(Some(data))) } else if let Some(err) = self.err.take() { Err(err) diff --git a/src/server/h1writer.rs b/src/server/h1writer.rs index 3ca735d45..a48c71b0b 100644 --- a/src/server/h1writer.rs +++ b/src/server/h1writer.rs @@ -229,7 +229,7 @@ impl Writer for H1Writer { } } - if self.buffer.len() > MAX_WRITE_BUFFER_SIZE { + if self.buffer.len() > self.buffer_capacity { Ok(WriterState::Pause) } else { Ok(WriterState::Done) diff --git a/src/server/h2writer.rs b/src/server/h2writer.rs index 6a6437b0b..ed97682e6 100644 --- a/src/server/h2writer.rs +++ b/src/server/h2writer.rs @@ -205,6 +205,6 @@ impl Writer for H2Writer { } } } - return Ok(Async::NotReady) + Ok(Async::NotReady) } } diff --git a/src/server/srv.rs b/src/server/srv.rs index 981e1a271..5d181c51e 100644 --- a/src/server/srv.rs +++ b/src/server/srv.rs @@ -172,7 +172,7 @@ impl HttpServer where H: IntoHttpHandler + 'static /// Get addresses of bound sockets. pub fn addrs(&self) -> Vec { - self.sockets.iter().map(|s| s.0.clone()).collect() + self.sockets.iter().map(|s| s.0).collect() } /// Use listener for accepting incoming connection requests