1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-30 18:34:36 +01:00

Allow to set read buffer capacity for server request

This commit is contained in:
Nikolay Kim 2018-03-12 10:01:56 -07:00
parent 46b9a9c887
commit 6657446433
7 changed files with 32 additions and 6 deletions

View File

@ -2,7 +2,9 @@
## 0.4.8 (2018-03-12) ## 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) ## 0.4.7 (2018-03-11)

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-web" name = "actix-web"
version = "0.4.7" version = "0.4.8"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix web is a simple, pragmatic, extremely fast, web framework for Rust." description = "Actix web is a simple, pragmatic, extremely fast, web framework for Rust."
readme = "README.md" readme = "README.md"

View File

@ -389,6 +389,15 @@ impl<S> HttpRequest<S> {
self.as_ref().method == Method::CONNECT 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)] #[cfg(test)]
pub(crate) fn payload(&self) -> &Payload { pub(crate) fn payload(&self) -> &Payload {
let msg = self.as_mut(); let msg = self.as_mut();

View File

@ -8,6 +8,10 @@ use futures::{Async, Poll, Stream};
use error::PayloadError; use error::PayloadError;
/// max buffer size 32k
pub(crate) const MAX_BUFFER_SIZE: usize = 32_768;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub(crate) enum PayloadStatus { pub(crate) enum PayloadStatus {
Read, Read,
@ -76,6 +80,14 @@ impl Payload {
pub(crate) fn readall(&self) -> Option<Bytes> { pub(crate) fn readall(&self) -> Option<Bytes> {
self.inner.borrow_mut().readall() 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 { impl Stream for Payload {
@ -161,6 +173,7 @@ struct Inner {
err: Option<PayloadError>, err: Option<PayloadError>,
need_read: bool, need_read: bool,
items: VecDeque<Bytes>, items: VecDeque<Bytes>,
capacity: usize,
} }
impl Inner { impl Inner {
@ -172,6 +185,7 @@ impl Inner {
err: None, err: None,
items: VecDeque::new(), items: VecDeque::new(),
need_read: true, need_read: true,
capacity: MAX_BUFFER_SIZE,
} }
} }
@ -188,8 +202,8 @@ impl Inner {
#[inline] #[inline]
fn feed_data(&mut self, data: Bytes) { fn feed_data(&mut self, data: Bytes) {
self.len += data.len(); self.len += data.len();
self.need_read = false;
self.items.push_back(data); self.items.push_back(data);
self.need_read = self.len < self.capacity;
} }
#[inline] #[inline]
@ -222,6 +236,7 @@ impl Inner {
fn readany(&mut self) -> Poll<Option<Bytes>, PayloadError> { fn readany(&mut self) -> Poll<Option<Bytes>, PayloadError> {
if let Some(data) = self.items.pop_front() { if let Some(data) = self.items.pop_front() {
self.len -= data.len(); self.len -= data.len();
self.need_read = self.len < self.capacity;
Ok(Async::Ready(Some(data))) Ok(Async::Ready(Some(data)))
} else if let Some(err) = self.err.take() { } else if let Some(err) = self.err.take() {
Err(err) Err(err)

View File

@ -229,7 +229,7 @@ impl<T: AsyncWrite> Writer for H1Writer<T> {
} }
} }
if self.buffer.len() > MAX_WRITE_BUFFER_SIZE { if self.buffer.len() > self.buffer_capacity {
Ok(WriterState::Pause) Ok(WriterState::Pause)
} else { } else {
Ok(WriterState::Done) Ok(WriterState::Done)

View File

@ -205,6 +205,6 @@ impl Writer for H2Writer {
} }
} }
} }
return Ok(Async::NotReady) Ok(Async::NotReady)
} }
} }

View File

@ -172,7 +172,7 @@ impl<H> HttpServer<H> where H: IntoHttpHandler + 'static
/// Get addresses of bound sockets. /// Get addresses of bound sockets.
pub fn addrs(&self) -> Vec<net::SocketAddr> { pub fn addrs(&self) -> Vec<net::SocketAddr> {
self.sockets.iter().map(|s| s.0.clone()).collect() self.sockets.iter().map(|s| s.0).collect()
} }
/// Use listener for accepting incoming connection requests /// Use listener for accepting incoming connection requests