1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-27 10:30:33 +00: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)
* 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)

View file

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

View file

@ -389,6 +389,15 @@ impl<S> HttpRequest<S> {
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();

View file

@ -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<Bytes> {
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<PayloadError>,
need_read: bool,
items: VecDeque<Bytes>,
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<Option<Bytes>, 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)

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)
} else {
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.
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