mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 09:59:21 +02:00
update guide
This commit is contained in:
@ -6,8 +6,8 @@ use futures::Stream;
|
||||
|
||||
use error::Error;
|
||||
|
||||
pub(crate) type BodyStream = Box<Stream<Item=Bytes, Error=Error>>;
|
||||
|
||||
/// Type represent streaming body
|
||||
pub type BodyStream = Box<Stream<Item=Bytes, Error=Error>>;
|
||||
|
||||
/// Represents various types of http message body.
|
||||
pub enum Body {
|
||||
|
@ -213,6 +213,9 @@ impl From<IoError> for PayloadError {
|
||||
}
|
||||
}
|
||||
|
||||
/// `InternalServerError` for `PayloadError`
|
||||
impl ResponseError for PayloadError {}
|
||||
|
||||
/// Return `BadRequest` for `cookie::ParseError`
|
||||
impl ResponseError for cookie::ParseError {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
|
@ -254,7 +254,7 @@ impl<T, H> Http1<T, H>
|
||||
if self.keepalive_timer.is_none() {
|
||||
trace!("Start keep-alive timer");
|
||||
let mut to = Timeout::new(
|
||||
Duration::new(keep_alive as u64, 0),
|
||||
Duration::new(keep_alive, 0),
|
||||
Arbiter::handle()).unwrap();
|
||||
// register timeout
|
||||
let _ = to.poll();
|
||||
|
@ -159,7 +159,7 @@ impl<T, H> Http2<T, H>
|
||||
if keep_alive > 0 && self.keepalive_timer.is_none() {
|
||||
trace!("Start keep-alive timer");
|
||||
let mut timeout = Timeout::new(
|
||||
Duration::new(keep_alive as u64, 0),
|
||||
Duration::new(keep_alive, 0),
|
||||
Arbiter::handle()).unwrap();
|
||||
// register timeout
|
||||
let _ = timeout.poll();
|
||||
|
@ -309,7 +309,7 @@ impl HttpResponseBuilder {
|
||||
|
||||
/// Enables automatic chunked transfer encoding
|
||||
#[inline]
|
||||
pub fn enable_chunked(&mut self) -> &mut Self {
|
||||
pub fn chunked(&mut self) -> &mut Self {
|
||||
if let Some(parts) = parts(&mut self.response, &self.err) {
|
||||
parts.chunked = true;
|
||||
}
|
||||
|
@ -128,6 +128,7 @@ pub mod dev {
|
||||
//! use actix_web::dev::*;
|
||||
//! ```
|
||||
|
||||
pub use body::BodyStream;
|
||||
pub use info::ConnectionInfo;
|
||||
pub use handler::Handler;
|
||||
pub use router::{Router, Pattern};
|
||||
|
@ -7,6 +7,7 @@ use bytes::{Bytes, BytesMut};
|
||||
use futures::{Async, Poll, Stream};
|
||||
use futures::task::{Task, current as current_task};
|
||||
|
||||
use body::BodyStream;
|
||||
use actix::ResponseType;
|
||||
use error::PayloadError;
|
||||
|
||||
@ -121,6 +122,11 @@ impl Payload {
|
||||
pub fn set_buffer_size(&self, size: usize) {
|
||||
self.inner.borrow_mut().set_buffer_size(size)
|
||||
}
|
||||
|
||||
/// Convert payload into BodyStream
|
||||
pub fn stream(self) -> BodyStream {
|
||||
Box::new(self.map(|item| item.0).map_err(|e| e.into()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for Payload {
|
||||
|
@ -94,7 +94,7 @@ pub struct HttpServer<T, A, H, U>
|
||||
io: PhantomData<T>,
|
||||
addr: PhantomData<A>,
|
||||
threads: usize,
|
||||
keep_alive: Option<u16>,
|
||||
keep_alive: Option<u64>,
|
||||
factory: Arc<Fn() -> U + Send + Sync>,
|
||||
workers: Vec<SyncAddress<Worker<H>>>,
|
||||
}
|
||||
@ -152,7 +152,7 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
|
||||
/// - `Some(0)` - disable
|
||||
///
|
||||
/// - `None` - use `SO_KEEPALIVE` socket option
|
||||
pub fn keep_alive(mut self, val: Option<u16>) -> Self {
|
||||
pub fn keep_alive(mut self, val: Option<u64>) -> Self {
|
||||
self.keep_alive = val;
|
||||
self
|
||||
}
|
||||
@ -240,7 +240,7 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
|
||||
let (tx, rx) = mpsc::unbounded::<IoStream<Socket>>();
|
||||
|
||||
let h = handler.clone();
|
||||
let ka = self.keep_alive.clone();
|
||||
let ka = self.keep_alive;
|
||||
let factory = Arc::clone(&self.factory);
|
||||
let addr = Arbiter::start(move |ctx: &mut Context<_>| {
|
||||
let mut apps: Vec<_> = (*factory)()
|
||||
@ -396,7 +396,7 @@ impl<T, A, H, U> Handler<IoStream<T>, io::Error> for HttpServer<T, A, H, U>
|
||||
-> Response<Self, IoStream<T>>
|
||||
{
|
||||
Arbiter::handle().spawn(
|
||||
HttpChannel::new(Rc::clone(&self.h.as_ref().unwrap()), msg.io, msg.peer, msg.http2));
|
||||
HttpChannel::new(Rc::clone(self.h.as_ref().unwrap()), msg.io, msg.peer, msg.http2));
|
||||
Self::empty()
|
||||
}
|
||||
}
|
||||
@ -412,21 +412,21 @@ struct Worker<H> {
|
||||
|
||||
pub(crate) struct WorkerSettings<H> {
|
||||
h: Vec<H>,
|
||||
keep_alive: Option<u16>,
|
||||
keep_alive: Option<u64>,
|
||||
}
|
||||
|
||||
impl<H> WorkerSettings<H> {
|
||||
pub fn handlers(&self) -> &Vec<H> {
|
||||
&self.h
|
||||
}
|
||||
pub fn keep_alive(&self) -> Option<u16> {
|
||||
pub fn keep_alive(&self) -> Option<u64> {
|
||||
self.keep_alive
|
||||
}
|
||||
}
|
||||
|
||||
impl<H: 'static> Worker<H> {
|
||||
|
||||
fn new(h: Vec<H>, handler: StreamHandlerType, keep_alive: Option<u16>) -> Worker<H> {
|
||||
fn new(h: Vec<H>, handler: StreamHandlerType, keep_alive: Option<u64>) -> Worker<H> {
|
||||
Worker {
|
||||
h: Rc::new(WorkerSettings{h: h, keep_alive: keep_alive}),
|
||||
handler: handler,
|
||||
@ -456,10 +456,10 @@ impl<H> Handler<IoStream<Socket>> for Worker<H>
|
||||
fn handle(&mut self, msg: IoStream<Socket>, _: &mut Context<Self>)
|
||||
-> Response<Self, IoStream<Socket>>
|
||||
{
|
||||
if let None = self.h.keep_alive {
|
||||
if msg.io.set_keepalive(Some(Duration::new(75, 0))).is_err() {
|
||||
error!("Can not set socket keep-alive option");
|
||||
}
|
||||
if self.h.keep_alive.is_none() &&
|
||||
msg.io.set_keepalive(Some(Duration::new(75, 0))).is_err()
|
||||
{
|
||||
error!("Can not set socket keep-alive option");
|
||||
}
|
||||
self.handler.handle(Rc::clone(&self.h), msg);
|
||||
Self::empty()
|
||||
|
Reference in New Issue
Block a user