mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 09:42:40 +01:00
fix context poll
This commit is contained in:
parent
896981cdf8
commit
f802fe09e6
@ -6,10 +6,10 @@ use futures::sync::oneshot::Sender;
|
|||||||
use futures::unsync::oneshot;
|
use futures::unsync::oneshot;
|
||||||
|
|
||||||
use actix::{Actor, ActorState, ActorContext, AsyncContext,
|
use actix::{Actor, ActorState, ActorContext, AsyncContext,
|
||||||
Handler, Subscriber, ResponseType, SpawnHandle};
|
Address, SyncAddress, Handler, Subscriber, ResponseType, SpawnHandle};
|
||||||
use actix::fut::ActorFuture;
|
use actix::fut::ActorFuture;
|
||||||
use actix::dev::{AsyncContextApi, ActorAddressCell,
|
use actix::dev::{queue, AsyncContextApi,
|
||||||
ContextImpl, Envelope, ToEnvelope, RemoteEnvelope};
|
ContextImpl, ContextProtocol, Envelope, ToEnvelope, RemoteEnvelope};
|
||||||
|
|
||||||
use body::{Body, Binary};
|
use body::{Body, Binary};
|
||||||
use error::{Error, Result, ErrorInternalServerError};
|
use error::{Error, Result, ErrorInternalServerError};
|
||||||
@ -76,13 +76,25 @@ impl<A, S> AsyncContext<A> for HttpContext<A, S> where A: Actor<Context=Self>
|
|||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
impl<A, S> AsyncContextApi<A> for HttpContext<A, S> where A: Actor<Context=Self> {
|
impl<A, S> AsyncContextApi<A> for HttpContext<A, S> where A: Actor<Context=Self> {
|
||||||
fn address_cell(&mut self) -> &mut ActorAddressCell<A> {
|
#[inline]
|
||||||
self.inner.address_cell()
|
fn unsync_sender(&mut self) -> queue::unsync::UnboundedSender<ContextProtocol<A>> {
|
||||||
|
self.inner.unsync_sender()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn unsync_address(&mut self) -> Address<A> {
|
||||||
|
self.inner.unsync_address()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn sync_address(&mut self) -> SyncAddress<A> {
|
||||||
|
self.inner.sync_address()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, S: 'static> HttpContext<A, S> where A: Actor<Context=Self> {
|
impl<A, S: 'static> HttpContext<A, S> where A: Actor<Context=Self> {
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn new(req: HttpRequest<S>, actor: A) -> HttpContext<A, S> {
|
pub fn new(req: HttpRequest<S>, actor: A) -> HttpContext<A, S> {
|
||||||
HttpContext::from_request(req).actor(actor)
|
HttpContext::from_request(req).actor(actor)
|
||||||
}
|
}
|
||||||
@ -96,6 +108,7 @@ impl<A, S: 'static> HttpContext<A, S> where A: Actor<Context=Self> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn actor(mut self, actor: A) -> HttpContext<A, S> {
|
pub fn actor(mut self, actor: A) -> HttpContext<A, S> {
|
||||||
self.inner.set_actor(actor);
|
self.inner.set_actor(actor);
|
||||||
self
|
self
|
||||||
@ -105,16 +118,19 @@ impl<A, S: 'static> HttpContext<A, S> where A: Actor<Context=Self> {
|
|||||||
impl<A, S> HttpContext<A, S> where A: Actor<Context=Self> {
|
impl<A, S> HttpContext<A, S> where A: Actor<Context=Self> {
|
||||||
|
|
||||||
/// Shared application state
|
/// Shared application state
|
||||||
|
#[inline]
|
||||||
pub fn state(&self) -> &S {
|
pub fn state(&self) -> &S {
|
||||||
self.request.state()
|
self.request.state()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Incoming request
|
/// Incoming request
|
||||||
|
#[inline]
|
||||||
pub fn request(&mut self) -> &mut HttpRequest<S> {
|
pub fn request(&mut self) -> &mut HttpRequest<S> {
|
||||||
&mut self.request
|
&mut self.request
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write payload
|
/// Write payload
|
||||||
|
#[inline]
|
||||||
pub fn write<B: Into<Binary>>(&mut self, data: B) {
|
pub fn write<B: Into<Binary>>(&mut self, data: B) {
|
||||||
if !self.disconnected {
|
if !self.disconnected {
|
||||||
self.stream.push_back(Frame::Payload(Some(data.into())));
|
self.stream.push_back(Frame::Payload(Some(data.into())));
|
||||||
@ -124,6 +140,7 @@ impl<A, S> HttpContext<A, S> where A: Actor<Context=Self> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Indicate end of streamimng payload. Also this method calls `Self::close`.
|
/// Indicate end of streamimng payload. Also this method calls `Self::close`.
|
||||||
|
#[inline]
|
||||||
pub fn write_eof(&mut self) {
|
pub fn write_eof(&mut self) {
|
||||||
self.stop();
|
self.stop();
|
||||||
}
|
}
|
||||||
@ -137,6 +154,7 @@ impl<A, S> HttpContext<A, S> where A: Actor<Context=Self> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Check if connection still open
|
/// Check if connection still open
|
||||||
|
#[inline]
|
||||||
pub fn connected(&self) -> bool {
|
pub fn connected(&self) -> bool {
|
||||||
!self.disconnected
|
!self.disconnected
|
||||||
}
|
}
|
||||||
@ -144,6 +162,7 @@ impl<A, S> HttpContext<A, S> where A: Actor<Context=Self> {
|
|||||||
|
|
||||||
impl<A, S> HttpContext<A, S> where A: Actor<Context=Self> {
|
impl<A, S> HttpContext<A, S> where A: Actor<Context=Self> {
|
||||||
|
|
||||||
|
#[inline]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn subscriber<M>(&mut self) -> Box<Subscriber<M>>
|
pub fn subscriber<M>(&mut self) -> Box<Subscriber<M>>
|
||||||
where A: Handler<M>, M: ResponseType + 'static
|
where A: Handler<M>, M: ResponseType + 'static
|
||||||
@ -151,6 +170,7 @@ impl<A, S> HttpContext<A, S> where A: Actor<Context=Self> {
|
|||||||
self.inner.subscriber()
|
self.inner.subscriber()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn sync_subscriber<M>(&mut self) -> Box<Subscriber<M> + Send>
|
pub fn sync_subscriber<M>(&mut self) -> Box<Subscriber<M> + Send>
|
||||||
where A: Handler<M>,
|
where A: Handler<M>,
|
||||||
@ -162,6 +182,7 @@ impl<A, S> HttpContext<A, S> where A: Actor<Context=Self> {
|
|||||||
|
|
||||||
impl<A, S> ActorHttpContext for HttpContext<A, S> where A: Actor<Context=Self>, S: 'static {
|
impl<A, S> ActorHttpContext for HttpContext<A, S> where A: Actor<Context=Self>, S: 'static {
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn disconnected(&mut self) {
|
fn disconnected(&mut self) {
|
||||||
self.disconnected = true;
|
self.disconnected = true;
|
||||||
self.stop();
|
self.stop();
|
||||||
@ -172,17 +193,20 @@ impl<A, S> ActorHttpContext for HttpContext<A, S> where A: Actor<Context=Self>,
|
|||||||
std::mem::transmute(self as &mut HttpContext<A, S>)
|
std::mem::transmute(self as &mut HttpContext<A, S>)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if self.inner.alive() {
|
||||||
match self.inner.poll(ctx) {
|
match self.inner.poll(ctx) {
|
||||||
Ok(Async::NotReady) => {
|
Ok(Async::NotReady) | Ok(Async::Ready(())) => (),
|
||||||
// get frame
|
Err(_) => return Err(ErrorInternalServerError("error").into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// frames
|
||||||
if let Some(frame) = self.stream.pop_front() {
|
if let Some(frame) = self.stream.pop_front() {
|
||||||
Ok(Async::Ready(Some(frame)))
|
Ok(Async::Ready(Some(frame)))
|
||||||
} else {
|
} else if self.inner.alive() {
|
||||||
Ok(Async::NotReady)
|
Ok(Async::NotReady)
|
||||||
}
|
} else {
|
||||||
}
|
Ok(Async::Ready(None))
|
||||||
Ok(Async::Ready(())) => Ok(Async::Ready(None)),
|
|
||||||
Err(_) => Err(ErrorInternalServerError("error").into()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -190,6 +214,7 @@ impl<A, S> ActorHttpContext for HttpContext<A, S> where A: Actor<Context=Self>,
|
|||||||
impl<A, S> ToEnvelope<A> for HttpContext<A, S>
|
impl<A, S> ToEnvelope<A> for HttpContext<A, S>
|
||||||
where A: Actor<Context=HttpContext<A, S>>,
|
where A: Actor<Context=HttpContext<A, S>>,
|
||||||
{
|
{
|
||||||
|
#[inline]
|
||||||
fn pack<M>(msg: M, tx: Option<Sender<Result<M::Item, M::Error>>>,
|
fn pack<M>(msg: M, tx: Option<Sender<Result<M::Item, M::Error>>>,
|
||||||
channel_on_drop: bool) -> Envelope<A>
|
channel_on_drop: bool) -> Envelope<A>
|
||||||
where A: Handler<M>,
|
where A: Handler<M>,
|
||||||
@ -229,6 +254,7 @@ impl<A: Actor> ActorFuture for Drain<A> {
|
|||||||
type Error = ();
|
type Error = ();
|
||||||
type Actor = A;
|
type Actor = A;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn poll(&mut self,
|
fn poll(&mut self,
|
||||||
_: &mut A,
|
_: &mut A,
|
||||||
_: &mut <Self::Actor as Actor>::Context) -> Poll<Self::Item, Self::Error>
|
_: &mut <Self::Actor as Actor>::Context) -> Poll<Self::Item, Self::Error>
|
||||||
|
@ -113,7 +113,13 @@ unsafe impl<T, A, H, U> Sync for HttpServer<T, A, H, U> where H: HttpHandler + '
|
|||||||
unsafe impl<T, A, H, U> Send for HttpServer<T, A, H, U> where H: HttpHandler + 'static {}
|
unsafe impl<T, A, H, U> Send for HttpServer<T, A, H, U> where H: HttpHandler + 'static {}
|
||||||
|
|
||||||
|
|
||||||
impl<T: 'static, A: 'static, H: HttpHandler + 'static, U: 'static> Actor for HttpServer<T, A, H, U> {
|
impl<T, A, H, U, V> Actor for HttpServer<T, A, H, U>
|
||||||
|
where A: 'static,
|
||||||
|
T: IoStream,
|
||||||
|
H: HttpHandler,
|
||||||
|
U: IntoIterator<Item=V> + 'static,
|
||||||
|
V: IntoHttpHandler<Handler=H>,
|
||||||
|
{
|
||||||
type Context = Context<Self>;
|
type Context = Context<Self>;
|
||||||
|
|
||||||
fn started(&mut self, ctx: &mut Self::Context) {
|
fn started(&mut self, ctx: &mut Self::Context) {
|
||||||
@ -121,13 +127,6 @@ impl<T: 'static, A: 'static, H: HttpHandler + 'static, U: 'static> Actor for Htt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static, A: 'static, H: HttpHandler + 'static, U: 'static> HttpServer<T, A, H, U> {
|
|
||||||
fn update_time(&self, ctx: &mut Context<Self>) {
|
|
||||||
helpers::update_date();
|
|
||||||
ctx.run_later(Duration::new(1, 0), |slf, ctx| slf.update_time(ctx));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, A, H, U, V> HttpServer<T, A, H, U>
|
impl<T, A, H, U, V> HttpServer<T, A, H, U>
|
||||||
where A: 'static,
|
where A: 'static,
|
||||||
T: IoStream,
|
T: IoStream,
|
||||||
@ -157,6 +156,11 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_time(&self, ctx: &mut Context<Self>) {
|
||||||
|
helpers::update_date();
|
||||||
|
ctx.run_later(Duration::new(1, 0), |slf, ctx| slf.update_time(ctx));
|
||||||
|
}
|
||||||
|
|
||||||
/// Set number of workers to start.
|
/// Set number of workers to start.
|
||||||
///
|
///
|
||||||
/// By default http server uses number of available logical cpu as threads count.
|
/// By default http server uses number of available logical cpu as threads count.
|
||||||
@ -294,14 +298,15 @@ impl<T, A, H, U, V> HttpServer<T, A, H, U>
|
|||||||
}
|
}
|
||||||
|
|
||||||
// subscribe to os signals
|
// subscribe to os signals
|
||||||
fn subscribe_to_signals(&self, addr: &SyncAddress<HttpServer<T, A, H, U>>) {
|
fn subscribe_to_signals(&self) -> Option<SyncAddress<signal::ProcessSignals>> {
|
||||||
if self.no_signals {
|
if !self.no_signals {
|
||||||
let msg = signal::Subscribe(addr.subscriber());
|
|
||||||
if let Some(ref signals) = self.signals {
|
if let Some(ref signals) = self.signals {
|
||||||
signals.send(msg);
|
Some(signals.clone())
|
||||||
} else {
|
} else {
|
||||||
Arbiter::system_registry().get::<signal::ProcessSignals>().send(msg);
|
Some(Arbiter::system_registry().get::<signal::ProcessSignals>())
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -355,10 +360,10 @@ impl<H: HttpHandler, U, V> HttpServer<TcpStream, net::SocketAddr, H, U>
|
|||||||
}
|
}
|
||||||
|
|
||||||
// start http server actor
|
// start http server actor
|
||||||
HttpServer::create(|ctx| {
|
let signals = self.subscribe_to_signals();
|
||||||
self.subscribe_to_signals(&ctx.address());
|
let addr: SyncAddress<_> = Actor::start(self);
|
||||||
self
|
signals.map(|signals| signals.send(signal::Subscribe(addr.subscriber())));
|
||||||
})
|
addr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -427,10 +432,10 @@ impl<H: HttpHandler, U, V> HttpServer<TlsStream<TcpStream>, net::SocketAddr, H,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// start http server actor
|
// start http server actor
|
||||||
Ok(HttpServer::create(|ctx| {
|
let signals = self.subscribe_to_signals();
|
||||||
self.subscribe_to_signals(&ctx.address());
|
let addr: SyncAddress<_> = Actor::start(self);
|
||||||
self
|
signals.map(|signals| signals.send(signal::Subscribe(addr.subscriber())));
|
||||||
}))
|
Ok(addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -470,10 +475,10 @@ impl<H: HttpHandler, U, V> HttpServer<SslStream<TcpStream>, net::SocketAddr, H,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// start http server actor
|
// start http server actor
|
||||||
Ok(HttpServer::create(|ctx| {
|
let signals = self.subscribe_to_signals();
|
||||||
self.subscribe_to_signals(&ctx.address());
|
let addr: SyncAddress<_> = Actor::start(self);
|
||||||
self
|
signals.map(|signals| signals.send(signal::Subscribe(addr.subscriber())));
|
||||||
}))
|
Ok(addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -514,22 +519,25 @@ impl<T, A, H, U, V> HttpServer<WrapperStream<T>, A, H, U>
|
|||||||
self.h = Some(Rc::new(WorkerSettings::new(apps, self.keep_alive)));
|
self.h = Some(Rc::new(WorkerSettings::new(apps, self.keep_alive)));
|
||||||
|
|
||||||
// start server
|
// start server
|
||||||
HttpServer::create(move |ctx| {
|
let signals = self.subscribe_to_signals();
|
||||||
|
let addr: SyncAddress<_> = HttpServer::create(move |ctx| {
|
||||||
ctx.add_stream(stream.map(
|
ctx.add_stream(stream.map(
|
||||||
move |(t, _)| Conn{io: WrapperStream::new(t), peer: None, http2: false}));
|
move |(t, _)| Conn{io: WrapperStream::new(t), peer: None, http2: false}));
|
||||||
self.subscribe_to_signals(&ctx.address());
|
|
||||||
self
|
self
|
||||||
})
|
});
|
||||||
|
signals.map(|signals| signals.send(signal::Subscribe(addr.subscriber())));
|
||||||
|
addr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Signals support
|
/// Signals support
|
||||||
/// Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and send `SystemExit(0)`
|
/// Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and send `SystemExit(0)`
|
||||||
/// message to `System` actor.
|
/// message to `System` actor.
|
||||||
impl<T, A, H, U> Handler<signal::Signal> for HttpServer<T, A, H, U>
|
impl<T, A, H, U, V> Handler<signal::Signal> for HttpServer<T, A, H, U>
|
||||||
where T: IoStream,
|
where T: IoStream,
|
||||||
H: HttpHandler + 'static,
|
H: HttpHandler + 'static,
|
||||||
U: 'static,
|
U: IntoIterator<Item=V> + 'static,
|
||||||
|
V: IntoHttpHandler<Handler=H>,
|
||||||
A: 'static,
|
A: 'static,
|
||||||
{
|
{
|
||||||
type Result = ();
|
type Result = ();
|
||||||
@ -556,10 +564,11 @@ impl<T, A, H, U> Handler<signal::Signal> for HttpServer<T, A, H, U>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, A, H, U> Handler<io::Result<Conn<T>>> for HttpServer<T, A, H, U>
|
impl<T, A, H, U, V> Handler<io::Result<Conn<T>>> for HttpServer<T, A, H, U>
|
||||||
where T: IoStream,
|
where T: IoStream,
|
||||||
H: HttpHandler + 'static,
|
H: HttpHandler + 'static,
|
||||||
U: 'static,
|
U: IntoIterator<Item=V> + 'static,
|
||||||
|
V: IntoHttpHandler<Handler=H>,
|
||||||
A: 'static,
|
A: 'static,
|
||||||
{
|
{
|
||||||
type Result = ();
|
type Result = ();
|
||||||
@ -595,10 +604,11 @@ pub struct StopServer {
|
|||||||
pub graceful: bool
|
pub graceful: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, A, H, U> Handler<PauseServer> for HttpServer<T, A, H, U>
|
impl<T, A, H, U, V> Handler<PauseServer> for HttpServer<T, A, H, U>
|
||||||
where T: IoStream,
|
where T: IoStream,
|
||||||
H: HttpHandler + 'static,
|
H: HttpHandler + 'static,
|
||||||
U: 'static,
|
U: IntoIterator<Item=V> + 'static,
|
||||||
|
V: IntoHttpHandler<Handler=H>,
|
||||||
A: 'static,
|
A: 'static,
|
||||||
{
|
{
|
||||||
type Result = ();
|
type Result = ();
|
||||||
@ -612,10 +622,11 @@ impl<T, A, H, U> Handler<PauseServer> for HttpServer<T, A, H, U>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, A, H, U> Handler<ResumeServer> for HttpServer<T, A, H, U>
|
impl<T, A, H, U, V> Handler<ResumeServer> for HttpServer<T, A, H, U>
|
||||||
where T: IoStream,
|
where T: IoStream,
|
||||||
H: HttpHandler + 'static,
|
H: HttpHandler + 'static,
|
||||||
U: 'static,
|
U: IntoIterator<Item=V> + 'static,
|
||||||
|
V: IntoHttpHandler<Handler=H>,
|
||||||
A: 'static,
|
A: 'static,
|
||||||
{
|
{
|
||||||
type Result = ();
|
type Result = ();
|
||||||
@ -628,10 +639,11 @@ impl<T, A, H, U> Handler<ResumeServer> for HttpServer<T, A, H, U>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, A, H, U> Handler<StopServer> for HttpServer<T, A, H, U>
|
impl<T, A, H, U, V> Handler<StopServer> for HttpServer<T, A, H, U>
|
||||||
where T: IoStream,
|
where T: IoStream,
|
||||||
H: HttpHandler + 'static,
|
H: HttpHandler + 'static,
|
||||||
U: 'static,
|
U: IntoIterator<Item=V> + 'static,
|
||||||
|
V: IntoHttpHandler<Handler=H>,
|
||||||
A: 'static,
|
A: 'static,
|
||||||
{
|
{
|
||||||
type Result = actix::Response<Self, StopServer>;
|
type Result = actix::Response<Self, StopServer>;
|
||||||
|
Loading…
Reference in New Issue
Block a user