mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 01:32:57 +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 actix::{Actor, ActorState, ActorContext, AsyncContext,
|
||||
Handler, Subscriber, ResponseType, SpawnHandle};
|
||||
Address, SyncAddress, Handler, Subscriber, ResponseType, SpawnHandle};
|
||||
use actix::fut::ActorFuture;
|
||||
use actix::dev::{AsyncContextApi, ActorAddressCell,
|
||||
ContextImpl, Envelope, ToEnvelope, RemoteEnvelope};
|
||||
use actix::dev::{queue, AsyncContextApi,
|
||||
ContextImpl, ContextProtocol, Envelope, ToEnvelope, RemoteEnvelope};
|
||||
|
||||
use body::{Body, Binary};
|
||||
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)]
|
||||
impl<A, S> AsyncContextApi<A> for HttpContext<A, S> where A: Actor<Context=Self> {
|
||||
fn address_cell(&mut self) -> &mut ActorAddressCell<A> {
|
||||
self.inner.address_cell()
|
||||
#[inline]
|
||||
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> {
|
||||
|
||||
#[inline]
|
||||
pub fn new(req: HttpRequest<S>, actor: A) -> HttpContext<A, S> {
|
||||
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> {
|
||||
self.inner.set_actor(actor);
|
||||
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> {
|
||||
|
||||
/// Shared application state
|
||||
#[inline]
|
||||
pub fn state(&self) -> &S {
|
||||
self.request.state()
|
||||
}
|
||||
|
||||
/// Incoming request
|
||||
#[inline]
|
||||
pub fn request(&mut self) -> &mut HttpRequest<S> {
|
||||
&mut self.request
|
||||
}
|
||||
|
||||
/// Write payload
|
||||
#[inline]
|
||||
pub fn write<B: Into<Binary>>(&mut self, data: B) {
|
||||
if !self.disconnected {
|
||||
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`.
|
||||
#[inline]
|
||||
pub fn write_eof(&mut self) {
|
||||
self.stop();
|
||||
}
|
||||
@ -137,6 +154,7 @@ impl<A, S> HttpContext<A, S> where A: Actor<Context=Self> {
|
||||
}
|
||||
|
||||
/// Check if connection still open
|
||||
#[inline]
|
||||
pub fn connected(&self) -> bool {
|
||||
!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> {
|
||||
|
||||
#[inline]
|
||||
#[doc(hidden)]
|
||||
pub fn subscriber<M>(&mut self) -> Box<Subscriber<M>>
|
||||
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()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[doc(hidden)]
|
||||
pub fn sync_subscriber<M>(&mut self) -> Box<Subscriber<M> + Send>
|
||||
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 {
|
||||
|
||||
#[inline]
|
||||
fn disconnected(&mut self) {
|
||||
self.disconnected = true;
|
||||
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>)
|
||||
};
|
||||
|
||||
match self.inner.poll(ctx) {
|
||||
Ok(Async::NotReady) => {
|
||||
// get frame
|
||||
if let Some(frame) = self.stream.pop_front() {
|
||||
Ok(Async::Ready(Some(frame)))
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
if self.inner.alive() {
|
||||
match self.inner.poll(ctx) {
|
||||
Ok(Async::NotReady) | Ok(Async::Ready(())) => (),
|
||||
Err(_) => return Err(ErrorInternalServerError("error").into()),
|
||||
}
|
||||
Ok(Async::Ready(())) => Ok(Async::Ready(None)),
|
||||
Err(_) => Err(ErrorInternalServerError("error").into()),
|
||||
}
|
||||
|
||||
// frames
|
||||
if let Some(frame) = self.stream.pop_front() {
|
||||
Ok(Async::Ready(Some(frame)))
|
||||
} else if self.inner.alive() {
|
||||
Ok(Async::NotReady)
|
||||
} else {
|
||||
Ok(Async::Ready(None))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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>
|
||||
where A: Actor<Context=HttpContext<A, S>>,
|
||||
{
|
||||
#[inline]
|
||||
fn pack<M>(msg: M, tx: Option<Sender<Result<M::Item, M::Error>>>,
|
||||
channel_on_drop: bool) -> Envelope<A>
|
||||
where A: Handler<M>,
|
||||
@ -229,6 +254,7 @@ impl<A: Actor> ActorFuture for Drain<A> {
|
||||
type Error = ();
|
||||
type Actor = A;
|
||||
|
||||
#[inline]
|
||||
fn poll(&mut self,
|
||||
_: &mut A,
|
||||
_: &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 {}
|
||||
|
||||
|
||||
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>;
|
||||
|
||||
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>
|
||||
where A: 'static,
|
||||
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.
|
||||
///
|
||||
/// 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
|
||||
fn subscribe_to_signals(&self, addr: &SyncAddress<HttpServer<T, A, H, U>>) {
|
||||
if self.no_signals {
|
||||
let msg = signal::Subscribe(addr.subscriber());
|
||||
fn subscribe_to_signals(&self) -> Option<SyncAddress<signal::ProcessSignals>> {
|
||||
if !self.no_signals {
|
||||
if let Some(ref signals) = self.signals {
|
||||
signals.send(msg);
|
||||
Some(signals.clone())
|
||||
} 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
|
||||
HttpServer::create(|ctx| {
|
||||
self.subscribe_to_signals(&ctx.address());
|
||||
self
|
||||
})
|
||||
let signals = self.subscribe_to_signals();
|
||||
let addr: SyncAddress<_> = Actor::start(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
|
||||
Ok(HttpServer::create(|ctx| {
|
||||
self.subscribe_to_signals(&ctx.address());
|
||||
self
|
||||
}))
|
||||
let signals = self.subscribe_to_signals();
|
||||
let addr: SyncAddress<_> = Actor::start(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
|
||||
Ok(HttpServer::create(|ctx| {
|
||||
self.subscribe_to_signals(&ctx.address());
|
||||
self
|
||||
}))
|
||||
let signals = self.subscribe_to_signals();
|
||||
let addr: SyncAddress<_> = Actor::start(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)));
|
||||
|
||||
// start server
|
||||
HttpServer::create(move |ctx| {
|
||||
let signals = self.subscribe_to_signals();
|
||||
let addr: SyncAddress<_> = HttpServer::create(move |ctx| {
|
||||
ctx.add_stream(stream.map(
|
||||
move |(t, _)| Conn{io: WrapperStream::new(t), peer: None, http2: false}));
|
||||
self.subscribe_to_signals(&ctx.address());
|
||||
self
|
||||
})
|
||||
});
|
||||
signals.map(|signals| signals.send(signal::Subscribe(addr.subscriber())));
|
||||
addr
|
||||
}
|
||||
}
|
||||
|
||||
/// 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>
|
||||
impl<T, A, H, U, V> Handler<signal::Signal> for HttpServer<T, A, H, U>
|
||||
where T: IoStream,
|
||||
H: HttpHandler + 'static,
|
||||
U: 'static,
|
||||
U: IntoIterator<Item=V> + 'static,
|
||||
V: IntoHttpHandler<Handler=H>,
|
||||
A: 'static,
|
||||
{
|
||||
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,
|
||||
H: HttpHandler + 'static,
|
||||
U: 'static,
|
||||
U: IntoIterator<Item=V> + 'static,
|
||||
V: IntoHttpHandler<Handler=H>,
|
||||
A: 'static,
|
||||
{
|
||||
type Result = ();
|
||||
@ -595,10 +604,11 @@ pub struct StopServer {
|
||||
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,
|
||||
H: HttpHandler + 'static,
|
||||
U: 'static,
|
||||
U: IntoIterator<Item=V> + 'static,
|
||||
V: IntoHttpHandler<Handler=H>,
|
||||
A: 'static,
|
||||
{
|
||||
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,
|
||||
H: HttpHandler + 'static,
|
||||
U: 'static,
|
||||
U: IntoIterator<Item=V> + 'static,
|
||||
V: IntoHttpHandler<Handler=H>,
|
||||
A: 'static,
|
||||
{
|
||||
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,
|
||||
H: HttpHandler + 'static,
|
||||
U: 'static,
|
||||
U: IntoIterator<Item=V> + 'static,
|
||||
V: IntoHttpHandler<Handler=H>,
|
||||
A: 'static,
|
||||
{
|
||||
type Result = actix::Response<Self, StopServer>;
|
||||
|
Loading…
Reference in New Issue
Block a user