1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-02-09 21:35:37 +01:00
actix-extras/src/context.rs

295 lines
6.9 KiB
Rust
Raw Normal View History

extern crate actix;
use futures::sync::oneshot;
2017-10-20 17:16:17 -07:00
use futures::sync::oneshot::Sender;
2018-04-13 16:02:01 -07:00
use futures::{Async, Future, Poll};
use smallvec::SmallVec;
2018-04-13 16:02:01 -07:00
use std::marker::PhantomData;
2017-10-06 21:48:14 -07:00
2018-07-04 17:04:23 +06:00
use self::actix::dev::{
AsyncContextParts, ContextFut, ContextParts, Envelope, Mailbox, ToEnvelope,
};
use self::actix::fut::ActorFuture;
use self::actix::{
2018-05-17 12:20:20 -07:00
Actor, ActorContext, ActorState, Addr, AsyncContext, Handler, Message, SpawnHandle,
};
2017-10-06 21:48:14 -07:00
2018-04-13 16:02:01 -07:00
use body::{Binary, Body};
2018-01-27 10:04:56 -08:00
use error::{Error, ErrorInternalServerError};
2017-11-29 10:31:24 -08:00
use httprequest::HttpRequest;
2017-12-24 11:58:09 -08:00
2017-12-31 17:26:32 -08:00
pub trait ActorHttpContext: 'static {
2017-12-01 15:45:15 -08:00
fn disconnected(&mut self);
fn poll(&mut self) -> Poll<Option<SmallVec<[Frame; 4]>>, Error>;
2017-12-01 15:45:15 -08:00
}
2017-10-06 21:48:14 -07:00
2017-11-30 14:42:20 -08:00
#[derive(Debug)]
2017-12-31 17:26:32 -08:00
pub enum Frame {
2018-01-10 20:08:13 -08:00
Chunk(Option<Binary>),
2017-12-24 11:58:09 -08:00
Drain(oneshot::Sender<()>),
2017-11-30 14:42:20 -08:00
}
2018-01-23 15:35:39 -08:00
impl Frame {
pub fn len(&self) -> usize {
match *self {
Frame::Chunk(Some(ref bin)) => bin.len(),
_ => 0,
}
}
}
2018-03-29 15:00:18 -07:00
/// Execution context for http actors
2018-04-13 16:02:01 -07:00
pub struct HttpContext<A, S = ()>
where
A: Actor<Context = HttpContext<A, S>>,
2017-10-06 21:48:14 -07:00
{
2018-07-04 17:04:23 +06:00
inner: ContextParts<A>,
stream: Option<SmallVec<[Frame; 4]>>,
2017-11-29 10:31:24 -08:00
request: HttpRequest<S>,
2017-10-25 16:25:26 -07:00
disconnected: bool,
2017-10-06 21:48:14 -07:00
}
2018-04-13 16:02:01 -07:00
impl<A, S> ActorContext for HttpContext<A, S>
where
A: Actor<Context = Self>,
2017-10-06 21:48:14 -07:00
{
fn stop(&mut self) {
2018-01-06 22:59:39 -08:00
self.inner.stop();
2017-10-06 21:48:14 -07:00
}
fn terminate(&mut self) {
2018-01-06 22:59:39 -08:00
self.inner.terminate()
2017-10-06 21:48:14 -07:00
}
fn state(&self) -> ActorState {
2018-01-06 22:59:39 -08:00
self.inner.state()
2017-10-06 21:48:14 -07:00
}
}
2018-04-13 16:02:01 -07:00
impl<A, S> AsyncContext<A> for HttpContext<A, S>
where
A: Actor<Context = Self>,
2017-10-06 21:48:14 -07:00
{
2018-01-23 09:42:04 -08:00
#[inline]
2017-10-09 10:44:11 -07:00
fn spawn<F>(&mut self, fut: F) -> SpawnHandle
2018-04-13 16:02:01 -07:00
where
F: ActorFuture<Item = (), Error = (), Actor = A> + 'static,
2017-10-06 21:48:14 -07:00
{
2018-01-06 22:59:39 -08:00
self.inner.spawn(fut)
2017-10-09 10:44:11 -07:00
}
2018-01-23 09:42:04 -08:00
#[inline]
2017-10-11 19:20:05 -07:00
fn wait<F>(&mut self, fut: F)
2018-04-13 16:02:01 -07:00
where
F: ActorFuture<Item = (), Error = (), Actor = A> + 'static,
2017-10-11 19:20:05 -07:00
{
2018-01-06 22:59:39 -08:00
self.inner.wait(fut)
2017-10-11 19:20:05 -07:00
}
2018-01-23 09:42:04 -08:00
#[doc(hidden)]
#[inline]
fn waiting(&self) -> bool {
2018-05-17 12:20:20 -07:00
self.inner.waiting()
|| self.inner.state() == ActorState::Stopping
2018-04-13 16:02:01 -07:00
|| self.inner.state() == ActorState::Stopped
2018-01-23 09:42:04 -08:00
}
#[inline]
2017-10-09 10:44:11 -07:00
fn cancel_future(&mut self, handle: SpawnHandle) -> bool {
2018-01-06 22:59:39 -08:00
self.inner.cancel_future(handle)
2017-10-30 14:49:20 -07:00
}
2018-01-07 17:13:49 -08:00
#[inline]
2018-05-29 10:31:37 -07:00
fn address(&self) -> Addr<A> {
self.inner.address()
2017-10-06 21:48:14 -07:00
}
}
2018-04-13 16:02:01 -07:00
impl<A, S: 'static> HttpContext<A, S>
where
A: Actor<Context = Self>,
{
2018-01-07 17:13:49 -08:00
#[inline]
/// Create a new HTTP Context from a request and an actor
2018-07-04 21:01:27 +06:00
pub fn create(req: HttpRequest<S>, actor: A) -> Body {
2018-07-04 17:04:23 +06:00
let mb = Mailbox::default();
let ctx = HttpContext {
inner: ContextParts::new(mb.sender_producer()),
stream: None,
2017-11-29 10:31:24 -08:00
request: req,
2017-10-25 16:25:26 -07:00
disconnected: false,
2018-07-04 17:04:23 +06:00
};
Body::Actor(Box::new(HttpContextFut::new(ctx, actor, mb)))
2017-10-06 21:48:14 -07:00
}
2018-06-17 03:10:44 +06:00
/// Create a new HTTP Context
2018-07-04 17:04:23 +06:00
pub fn with_factory<F>(req: HttpRequest<S>, f: F) -> Body
2018-06-17 03:10:44 +06:00
where
F: FnOnce(&mut Self) -> A + 'static,
{
2018-07-04 17:04:23 +06:00
let mb = Mailbox::default();
2018-06-17 03:10:44 +06:00
let mut ctx = HttpContext {
2018-07-04 17:04:23 +06:00
inner: ContextParts::new(mb.sender_producer()),
2018-06-17 03:10:44 +06:00
stream: None,
request: req,
disconnected: false,
};
let act = f(&mut ctx);
2018-07-04 17:04:23 +06:00
Body::Actor(Box::new(HttpContextFut::new(ctx, act, mb)))
2017-12-31 17:26:32 -08:00
}
2017-10-06 21:48:14 -07:00
}
2018-04-13 16:02:01 -07:00
impl<A, S> HttpContext<A, S>
where
A: Actor<Context = Self>,
{
2017-11-26 21:47:33 -08:00
/// Shared application state
2018-01-07 17:13:49 -08:00
#[inline]
2017-11-29 10:31:24 -08:00
pub fn state(&self) -> &S {
self.request.state()
2017-11-26 21:47:33 -08:00
}
2017-11-29 10:31:24 -08:00
/// Incoming request
2018-01-07 17:13:49 -08:00
#[inline]
2017-11-29 10:31:24 -08:00
pub fn request(&mut self) -> &mut HttpRequest<S> {
&mut self.request
}
2017-10-06 21:48:14 -07:00
/// Write payload
2018-01-07 17:13:49 -08:00
#[inline]
2017-11-10 13:42:32 -08:00
pub fn write<B: Into<Binary>>(&mut self, data: B) {
2017-12-31 17:26:32 -08:00
if !self.disconnected {
self.add_frame(Frame::Chunk(Some(data.into())));
2017-11-30 14:42:20 -08:00
} else {
2017-12-31 17:26:32 -08:00
warn!("Trying to write to disconnected response");
2017-11-30 14:42:20 -08:00
}
2017-10-06 21:48:14 -07:00
}
2017-10-25 16:25:26 -07:00
2018-01-16 00:47:25 +03:00
/// Indicate end of streaming payload. Also this method calls `Self::close`.
2018-01-07 17:13:49 -08:00
#[inline]
2017-12-01 15:45:15 -08:00
pub fn write_eof(&mut self) {
self.add_frame(Frame::Chunk(None));
2017-12-01 15:45:15 -08:00
}
2017-10-29 06:05:31 -07:00
/// Returns drain future
2017-12-31 17:26:32 -08:00
pub fn drain(&mut self) -> Drain<A> {
2017-12-24 11:58:09 -08:00
let (tx, rx) = oneshot::channel();
self.add_frame(Frame::Drain(tx));
2017-12-31 17:26:32 -08:00
Drain::new(rx)
2017-10-29 06:05:31 -07:00
}
2017-10-25 16:25:26 -07:00
/// Check if connection still open
2018-01-07 17:13:49 -08:00
#[inline]
2017-10-25 16:25:26 -07:00
pub fn connected(&self) -> bool {
!self.disconnected
}
#[inline]
fn add_frame(&mut self, frame: Frame) {
if self.stream.is_none() {
self.stream = Some(SmallVec::new());
}
2018-04-28 22:55:47 -07:00
if let Some(s) = self.stream.as_mut() {
s.push(frame)
}
}
2018-01-28 09:47:46 -08:00
/// Handle of the running future
///
/// SpawnHandle is the handle returned by `AsyncContext::spawn()` method.
pub fn handle(&self) -> SpawnHandle {
self.inner.curr_handle()
}
2017-10-06 21:48:14 -07:00
}
2018-07-04 17:04:23 +06:00
impl<A, S> AsyncContextParts<A> for HttpContext<A, S>
2018-04-13 16:02:01 -07:00
where
A: Actor<Context = Self>,
2018-07-04 17:04:23 +06:00
{
fn parts(&mut self) -> &mut ContextParts<A> {
&mut self.inner
}
}
struct HttpContextFut<A, S>
where
A: Actor<Context = HttpContext<A, S>>,
{
fut: ContextFut<A, HttpContext<A, S>>,
}
impl<A, S> HttpContextFut<A, S>
where
A: Actor<Context = HttpContext<A, S>>,
{
fn new(ctx: HttpContext<A, S>, act: A, mailbox: Mailbox<A>) -> Self {
let fut = ContextFut::new(ctx, act, mailbox);
HttpContextFut { fut }
}
}
impl<A, S> ActorHttpContext for HttpContextFut<A, S>
where
A: Actor<Context = HttpContext<A, S>>,
2018-04-13 16:02:01 -07:00
S: 'static,
{
2018-01-07 17:13:49 -08:00
#[inline]
2017-11-30 14:42:20 -08:00
fn disconnected(&mut self) {
2018-07-04 17:04:23 +06:00
self.fut.ctx().disconnected = true;
self.fut.ctx().stop();
2017-11-30 14:42:20 -08:00
}
2017-10-06 21:48:14 -07:00
fn poll(&mut self) -> Poll<Option<SmallVec<[Frame; 4]>>, Error> {
2018-07-04 17:04:23 +06:00
if self.fut.alive() {
match self.fut.poll() {
2018-01-07 17:13:49 -08:00
Ok(Async::NotReady) | Ok(Async::Ready(())) => (),
Err(_) => return Err(ErrorInternalServerError("error")),
2017-10-06 21:48:14 -07:00
}
2018-01-07 17:13:49 -08:00
}
// frames
2018-07-04 17:04:23 +06:00
if let Some(data) = self.fut.ctx().stream.take() {
Ok(Async::Ready(Some(data)))
2018-07-04 17:04:23 +06:00
} else if self.fut.alive() {
2018-01-07 17:13:49 -08:00
Ok(Async::NotReady)
} else {
Ok(Async::Ready(None))
2017-10-06 21:48:14 -07:00
}
}
}
2017-10-20 17:16:17 -07:00
impl<A, M, S> ToEnvelope<A, M> for HttpContext<A, S>
2018-04-13 16:02:01 -07:00
where
A: Actor<Context = HttpContext<A, S>> + Handler<M>,
M: Message + Send + 'static,
M::Result: Send,
2017-10-20 17:16:17 -07:00
{
fn pack(msg: M, tx: Option<Sender<M::Result>>) -> Envelope<A> {
Envelope::new(msg, tx)
2017-10-20 17:16:17 -07:00
}
}
2017-12-31 17:26:32 -08:00
/// Consume a future
2017-12-31 17:26:32 -08:00
pub struct Drain<A> {
fut: oneshot::Receiver<()>,
_a: PhantomData<A>,
}
impl<A> Drain<A> {
/// Create a drain from a future
pub fn new(fut: oneshot::Receiver<()>) -> Self {
2018-04-13 16:02:01 -07:00
Drain {
fut,
_a: PhantomData,
}
2017-12-31 17:26:32 -08:00
}
}
impl<A: Actor> ActorFuture for Drain<A> {
type Item = ();
type Error = ();
type Actor = A;
2018-01-07 17:13:49 -08:00
#[inline]
2018-04-13 16:02:01 -07:00
fn poll(
2018-04-28 22:55:47 -07:00
&mut self, _: &mut A, _: &mut <Self::Actor as Actor>::Context,
2018-04-13 16:02:01 -07:00
) -> Poll<Self::Item, Self::Error> {
2017-12-31 17:26:32 -08:00
self.fut.poll().map_err(|_| ())
}
}