mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 18:09:22 +02:00
upgrade to actix 0.4
This commit is contained in:
@ -6,9 +6,9 @@ use futures::sync::oneshot::Sender;
|
||||
use futures::unsync::oneshot;
|
||||
|
||||
use actix::{Actor, ActorState, ActorContext, AsyncContext,
|
||||
Handler, Subscriber, ResponseType};
|
||||
Handler, Subscriber, ResponseType, SpawnHandle};
|
||||
use actix::fut::ActorFuture;
|
||||
use actix::dev::{AsyncContextApi, ActorAddressCell, ActorItemsCell, ActorWaitCell, SpawnHandle,
|
||||
use actix::dev::{AsyncContextApi, ActorAddressCell, ActorItemsCell, ActorWaitCell,
|
||||
Envelope, ToEnvelope, RemoteEnvelope};
|
||||
|
||||
use body::{Body, Binary};
|
||||
@ -290,13 +290,14 @@ impl<A, S> ActorHttpContext for HttpContext<A, S> where A: Actor<Context=Self>,
|
||||
impl<A, S> ToEnvelope<A> for HttpContext<A, S>
|
||||
where A: Actor<Context=HttpContext<A, S>>,
|
||||
{
|
||||
fn pack<M>(msg: M, tx: Option<Sender<Result<M::Item, M::Error>>>) -> Envelope<A>
|
||||
fn pack<M>(msg: M, tx: Option<Sender<Result<M::Item, M::Error>>>,
|
||||
channel_on_drop: bool) -> Envelope<A>
|
||||
where A: Handler<M>,
|
||||
M: ResponseType + Send + 'static,
|
||||
M::Item: Send,
|
||||
M::Error: Send
|
||||
{
|
||||
RemoteEnvelope::new(msg, tx).into()
|
||||
RemoteEnvelope::new(msg, tx, channel_on_drop).into()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,12 +5,12 @@ use std::time::Duration;
|
||||
use std::marker::PhantomData;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use actix::dev::*;
|
||||
use actix::System;
|
||||
use actix::prelude::*;
|
||||
use futures::{Future, Sink, Stream};
|
||||
use futures::sync::mpsc;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use tokio_core::net::TcpStream;
|
||||
use actix::actors::signal;
|
||||
use mio;
|
||||
use num_cpus;
|
||||
use net2::TcpBuilder;
|
||||
@ -27,9 +27,6 @@ use openssl::pkcs12::ParsedPkcs12;
|
||||
#[cfg(feature="alpn")]
|
||||
use tokio_openssl::SslStream;
|
||||
|
||||
#[cfg(feature="signal")]
|
||||
use actix::actors::signal;
|
||||
|
||||
use helpers;
|
||||
use channel::{HttpChannel, HttpHandler, IntoHttpHandler, IoStream, WrapperStream};
|
||||
use worker::{Conn, Worker, WorkerSettings, StreamHandlerType, StopWorker};
|
||||
@ -202,7 +199,6 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature="signal")]
|
||||
/// Send `SystemExit` message to actix system
|
||||
///
|
||||
/// `SystemExit` message stops currently running system arbiter and all
|
||||
@ -271,7 +267,7 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
|
||||
let apps: Vec<_> = (*factory)()
|
||||
.into_iter()
|
||||
.map(|h| h.into_handler(s.clone())).collect();
|
||||
ctx.add_stream(rx);
|
||||
ctx.add_message_stream(rx);
|
||||
Worker::new(apps, h, ka)
|
||||
});
|
||||
workers.push(tx);
|
||||
@ -494,8 +490,7 @@ impl<T, A, H, U, V> HttpServer<WrapperStream<T>, A, H, U>
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature="signal")]
|
||||
/// Unix Signals support
|
||||
/// Signals support
|
||||
/// Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and send `SystemExit(0)`
|
||||
/// message to `System` actor.
|
||||
impl<T, A, H, U> Handler<signal::Signal> for HttpServer<T, A, H, U>
|
||||
@ -504,9 +499,9 @@ impl<T, A, H, U> Handler<signal::Signal> for HttpServer<T, A, H, U>
|
||||
U: 'static,
|
||||
A: 'static,
|
||||
{
|
||||
fn handle(&mut self, msg: signal::Signal, ctx: &mut Context<Self>)
|
||||
-> Response<Self, signal::Signal>
|
||||
{
|
||||
type Result = ();
|
||||
|
||||
fn handle(&mut self, msg: signal::Signal, ctx: &mut Context<Self>) {
|
||||
match msg.0 {
|
||||
signal::SignalType::Int => {
|
||||
info!("SIGINT received, exiting");
|
||||
@ -524,32 +519,33 @@ impl<T, A, H, U> Handler<signal::Signal> for HttpServer<T, A, H, U>
|
||||
Handler::<StopServer>::handle(self, StopServer{graceful: false}, ctx);
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
Self::empty()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, A, H, U> StreamHandler<Conn<T>, io::Error> for HttpServer<T, A, H, U>
|
||||
impl<T, A, H, U> StreamHandler<io::Result<Conn<T>>> for HttpServer<T, A, H, U>
|
||||
where T: IoStream,
|
||||
H: HttpHandler + 'static,
|
||||
U: 'static,
|
||||
A: 'static {}
|
||||
|
||||
impl<T, A, H, U> Handler<Conn<T>, io::Error> for HttpServer<T, A, H, U>
|
||||
impl<T, A, H, U> Handler<io::Result<Conn<T>>> for HttpServer<T, A, H, U>
|
||||
where T: IoStream,
|
||||
H: HttpHandler + 'static,
|
||||
U: 'static,
|
||||
A: 'static,
|
||||
{
|
||||
fn error(&mut self, err: io::Error, _: &mut Context<Self>) {
|
||||
debug!("Error handling request: {}", err)
|
||||
}
|
||||
type Result = ();
|
||||
|
||||
fn handle(&mut self, msg: Conn<T>, _: &mut Context<Self>) -> Response<Self, Conn<T>>
|
||||
{
|
||||
Arbiter::handle().spawn(
|
||||
HttpChannel::new(Rc::clone(self.h.as_ref().unwrap()), msg.io, msg.peer, msg.http2));
|
||||
Self::empty()
|
||||
fn handle(&mut self, msg: io::Result<Conn<T>>, _: &mut Context<Self>) -> Self::Result {
|
||||
match msg {
|
||||
Ok(msg) =>
|
||||
Arbiter::handle().spawn(
|
||||
HttpChannel::new(
|
||||
Rc::clone(self.h.as_ref().unwrap()), msg.io, msg.peer, msg.http2)),
|
||||
Err(err) =>
|
||||
debug!("Error handling request: {}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -578,13 +574,14 @@ impl<T, A, H, U> Handler<PauseServer> for HttpServer<T, A, H, U>
|
||||
U: 'static,
|
||||
A: 'static,
|
||||
{
|
||||
fn handle(&mut self, _: PauseServer, _: &mut Context<Self>) -> Response<Self, PauseServer>
|
||||
type Result = ();
|
||||
|
||||
fn handle(&mut self, _: PauseServer, _: &mut Context<Self>)
|
||||
{
|
||||
for item in &self.accept {
|
||||
let _ = item.1.send(Command::Pause);
|
||||
let _ = item.0.set_readiness(mio::Ready::readable());
|
||||
}
|
||||
Self::empty()
|
||||
}
|
||||
}
|
||||
|
||||
@ -594,13 +591,13 @@ impl<T, A, H, U> Handler<ResumeServer> for HttpServer<T, A, H, U>
|
||||
U: 'static,
|
||||
A: 'static,
|
||||
{
|
||||
fn handle(&mut self, _: ResumeServer, _: &mut Context<Self>) -> Response<Self, ResumeServer>
|
||||
{
|
||||
type Result = ();
|
||||
|
||||
fn handle(&mut self, _: ResumeServer, _: &mut Context<Self>) {
|
||||
for item in &self.accept {
|
||||
let _ = item.1.send(Command::Resume);
|
||||
let _ = item.0.set_readiness(mio::Ready::readable());
|
||||
}
|
||||
Self::empty()
|
||||
}
|
||||
}
|
||||
|
||||
@ -610,8 +607,9 @@ impl<T, A, H, U> Handler<StopServer> for HttpServer<T, A, H, U>
|
||||
U: 'static,
|
||||
A: 'static,
|
||||
{
|
||||
fn handle(&mut self, msg: StopServer, ctx: &mut Context<Self>) -> Response<Self, StopServer>
|
||||
{
|
||||
type Result = actix::Response<Self, StopServer>;
|
||||
|
||||
fn handle(&mut self, msg: StopServer, ctx: &mut Context<Self>) -> Self::Result {
|
||||
// stop accept threads
|
||||
for item in &self.accept {
|
||||
let _ = item.1.send(Command::Stop);
|
||||
@ -636,10 +634,10 @@ impl<T, A, H, U> Handler<StopServer> for HttpServer<T, A, H, U>
|
||||
|
||||
// we need to stop system if server was spawned
|
||||
if slf.exit {
|
||||
Arbiter::system().send(msgs::SystemExit(0))
|
||||
Arbiter::system().send(actix::msgs::SystemExit(0))
|
||||
}
|
||||
}
|
||||
fut::ok(())
|
||||
actix::fut::ok(())
|
||||
}).spawn(ctx);
|
||||
}
|
||||
|
||||
@ -649,7 +647,7 @@ impl<T, A, H, U> Handler<StopServer> for HttpServer<T, A, H, U>
|
||||
} else {
|
||||
// we need to stop system if server was spawned
|
||||
if self.exit {
|
||||
Arbiter::system().send(msgs::SystemExit(0))
|
||||
Arbiter::system().send(actix::msgs::SystemExit(0))
|
||||
}
|
||||
Self::empty()
|
||||
}
|
||||
|
@ -157,8 +157,9 @@ impl<H> StreamHandler<Conn<net::TcpStream>> for Worker<H>
|
||||
impl<H> Handler<Conn<net::TcpStream>> for Worker<H>
|
||||
where H: HttpHandler + 'static,
|
||||
{
|
||||
type Result = ();
|
||||
|
||||
fn handle(&mut self, msg: Conn<net::TcpStream>, _: &mut Context<Self>)
|
||||
-> Response<Self, Conn<net::TcpStream>>
|
||||
{
|
||||
if !self.settings.keep_alive_enabled() &&
|
||||
msg.io.set_keepalive(Some(time::Duration::new(75, 0))).is_err()
|
||||
@ -166,7 +167,6 @@ impl<H> Handler<Conn<net::TcpStream>> for Worker<H>
|
||||
error!("Can not set socket keep-alive option");
|
||||
}
|
||||
self.handler.handle(Rc::clone(&self.settings), &self.hnd, msg);
|
||||
Self::empty()
|
||||
}
|
||||
}
|
||||
|
||||
@ -174,8 +174,9 @@ impl<H> Handler<Conn<net::TcpStream>> for Worker<H>
|
||||
impl<H> Handler<StopWorker> for Worker<H>
|
||||
where H: HttpHandler + 'static,
|
||||
{
|
||||
fn handle(&mut self, msg: StopWorker, ctx: &mut Context<Self>) -> Response<Self, StopWorker>
|
||||
{
|
||||
type Result = Response<Self, StopWorker>;
|
||||
|
||||
fn handle(&mut self, msg: StopWorker, ctx: &mut Context<Self>) -> Self::Result {
|
||||
let num = self.settings.channels.get();
|
||||
if num == 0 {
|
||||
info!("Shutting down http worker, 0 connections");
|
||||
|
@ -26,16 +26,15 @@
|
||||
//! # impl StreamHandler<ws::Message> for Ws {}
|
||||
//! #
|
||||
//! impl Handler<ws::Message> for Ws {
|
||||
//! fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>)
|
||||
//! -> Response<Self, ws::Message>
|
||||
//! {
|
||||
//! type Result = ();
|
||||
//!
|
||||
//! fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>) {
|
||||
//! match msg {
|
||||
//! ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, &msg),
|
||||
//! ws::Message::Text(text) => ws::WsWriter::text(ctx, &text),
|
||||
//! ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin),
|
||||
//! _ => (),
|
||||
//! }
|
||||
//! Self::empty()
|
||||
//! }
|
||||
//! }
|
||||
//! #
|
||||
@ -94,7 +93,7 @@ pub fn start<A, S>(mut req: HttpRequest<S>, actor: A) -> Result<HttpResponse, Er
|
||||
let stream = WsStream::new(req.payload_mut().readany());
|
||||
|
||||
let mut ctx = HttpContext::new(req, actor);
|
||||
ctx.add_stream(stream);
|
||||
ctx.add_message_stream(stream);
|
||||
|
||||
Ok(resp.body(ctx)?)
|
||||
}
|
||||
|
Reference in New Issue
Block a user