mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-27 19:12:56 +01:00
Migrate actix-net to std::future (#64)
* Migrate actix-codec, actix-rt, and actix-threadpool to std::future * update to latest tokio alpha and futures-rs * Migrate actix-service to std::future, This is a squash of ~8 commits, since it included a lot of experimentation. To see the commits, look into the semtexzv/std-future-service-tmp branch. * update futures-rs and tokio * Migrate actix-threadpool to std::future (#59) * Migrate actix-threadpool to std::future * Cosmetic refactor - turn log::error! into log::warn! as it doesn't throw any error - add Clone and Copy impls for Cancelled making it cheap to operate with - apply rustfmt * Bump up crate version to 0.2.0 and pre-fill its changelog * Disable patching 'actix-threadpool' crate in global workspace as unnecessary * Revert patching and fix 'actix-rt' * Migrate actix-rt to std::future (#47) * remove Pin from Service::poll_ready(); simplify combinators api; make code compile * disable tests * update travis config * refactor naming * drop IntoFuture trait * Migrate actix-server to std::future (#50) Still not finished, this is more WIP, this is an aggregation of several commits, which can be found in semtexzv/std-future-server-tmp branch * update actix-server * rename Factor to ServiceFactory * start server worker in start mehtod * update actix-utils * remove IntoTransform trait * Migrate actix-server::ssl::nativetls to std futures (#61) * Refactor 'nativetls' module * Migrate 'actix-server-config' to std futures - remove "uds" feature - disable features by default * Switch NativeTlsAcceptor to use 'tokio-tls' crate * Bikeshed features names and remove unnecessary dependencies for 'actix-server-config' crate * update openssl impl * migrate actix-connect to std::future * migrate actix-ioframe to std::future * update version to alpha.1 * fix boxed service * migrate server rustls support * migratte openssl and rustls connecttors * store the thread's handle with arbiter (#62) * update ssl connect tests * restore service tests * update readme
This commit is contained in:
parent
9fa2a36b4e
commit
13049b80ca
2
.gitignore
vendored
2
.gitignore
vendored
@ -12,3 +12,5 @@ guide/build/
|
|||||||
|
|
||||||
# These are backup files generated by rustfmt
|
# These are backup files generated by rustfmt
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
|
.idea
|
||||||
|
11
.travis.yml
11
.travis.yml
@ -10,10 +10,9 @@ matrix:
|
|||||||
include:
|
include:
|
||||||
- rust: stable
|
- rust: stable
|
||||||
- rust: beta
|
- rust: beta
|
||||||
- rust: 1.37.0
|
- rust: nightly-2019-11-07
|
||||||
- rust: nightly-2019-06-15
|
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- rust: nightly-2019-06-15
|
- rust: nightly-2019-11-07
|
||||||
|
|
||||||
env:
|
env:
|
||||||
global:
|
global:
|
||||||
@ -26,7 +25,7 @@ before_install:
|
|||||||
- sudo apt-get install -y openssl libssl-dev libelf-dev libdw-dev cmake gcc binutils-dev libiberty-dev
|
- sudo apt-get install -y openssl libssl-dev libelf-dev libdw-dev cmake gcc binutils-dev libiberty-dev
|
||||||
|
|
||||||
before_cache: |
|
before_cache: |
|
||||||
if [[ "$TRAVIS_RUST_VERSION" == "nightly-2019-06-15" ]]; then
|
if [[ "$TRAVIS_RUST_VERSION" == "nightly-2019-11-07" ]]; then
|
||||||
RUSTFLAGS="--cfg procmacro2_semver_exempt" cargo install --version 0.6.11 cargo-tarpaulin
|
RUSTFLAGS="--cfg procmacro2_semver_exempt" cargo install --version 0.6.11 cargo-tarpaulin
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -36,14 +35,14 @@ before_script:
|
|||||||
|
|
||||||
script:
|
script:
|
||||||
- |
|
- |
|
||||||
if [[ "$TRAVIS_RUST_VERSION" != "nightly-2019-06-15" ]]; then
|
if [[ "$TRAVIS_RUST_VERSION" != "nightly-2019-11-07" ]]; then
|
||||||
cargo clean
|
cargo clean
|
||||||
cargo test --all --all-features -- --nocapture
|
cargo test --all --all-features -- --nocapture
|
||||||
fi
|
fi
|
||||||
|
|
||||||
after_success:
|
after_success:
|
||||||
- |
|
- |
|
||||||
if [[ "$TRAVIS_RUST_VERSION" == "nightly-2019-06-15" ]]; then
|
if [[ "$TRAVIS_RUST_VERSION" == "nightly-2019-11-07" ]]; then
|
||||||
taskset -c 0 cargo tarpaulin --all --all-features --out Xml
|
taskset -c 0 cargo tarpaulin --all --all-features --out Xml
|
||||||
echo "Uploaded code coverage"
|
echo "Uploaded code coverage"
|
||||||
bash <(curl -s https://codecov.io/bash)
|
bash <(curl -s https://codecov.io/bash)
|
||||||
|
@ -4,10 +4,8 @@ Actix net - framework for composable network services
|
|||||||
|
|
||||||
## Documentation & community resources
|
## Documentation & community resources
|
||||||
|
|
||||||
* [API Documentation (Development)](https://actix.rs/actix-net/actix_net/)
|
|
||||||
* [Chat on gitter](https://gitter.im/actix/actix)
|
* [Chat on gitter](https://gitter.im/actix/actix)
|
||||||
* Cargo package: [actix-net](https://crates.io/crates/actix-net)
|
* Minimum supported Rust version: 1.39 or later
|
||||||
* Minimum supported Rust version: 1.37 or later
|
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-codec"
|
name = "actix-codec"
|
||||||
version = "0.1.2"
|
version = "0.2.0-alpha.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Utilities for encoding and decoding frames"
|
description = "Utilities for encoding and decoding frames"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -19,7 +19,8 @@ path = "src/lib.rs"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bytes = "0.4.12"
|
bytes = "0.4.12"
|
||||||
futures = "0.1.24"
|
pin-utils = "0.1.0-alpha.4"
|
||||||
tokio-io = "0.1.12"
|
futures = "0.3.1"
|
||||||
tokio-codec = "0.1.1"
|
tokio-io = "0.2.0-alpha.6"
|
||||||
|
tokio-codec = "0.2.0-alpha.6"
|
||||||
log = "0.4"
|
log = "0.4"
|
@ -2,9 +2,11 @@
|
|||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io::{self, Read, Write};
|
use std::io::{self, Read, Write};
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures::{Poll, Sink, StartSend, Stream};
|
use futures::{Sink, Stream};
|
||||||
use tokio_codec::{Decoder, Encoder};
|
use tokio_codec::{Decoder, Encoder};
|
||||||
use tokio_io::{AsyncRead, AsyncWrite};
|
use tokio_io::{AsyncRead, AsyncWrite};
|
||||||
|
|
||||||
@ -26,8 +28,8 @@ pub struct Fuse<T, U>(pub T, pub U);
|
|||||||
|
|
||||||
impl<T, U> Framed<T, U>
|
impl<T, U> Framed<T, U>
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
U: Decoder + Encoder,
|
U: Decoder + Encoder + Unpin,
|
||||||
{
|
{
|
||||||
/// Provides a `Stream` and `Sink` interface for reading and writing to this
|
/// Provides a `Stream` and `Sink` interface for reading and writing to this
|
||||||
/// `Io` object, using `Decode` and `Encode` to read and write the raw data.
|
/// `Io` object, using `Decode` and `Encode` to read and write the raw data.
|
||||||
@ -221,41 +223,70 @@ impl<T, U> Framed<T, U> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, U> Stream for Framed<T, U>
|
impl<T, U> Framed<T, U> {
|
||||||
where
|
pub fn next_item(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<U::Item, U::Error>>>
|
||||||
T: AsyncRead,
|
where
|
||||||
U: Decoder,
|
T: AsyncRead + Unpin,
|
||||||
{
|
U: Decoder + Unpin,
|
||||||
type Item = U::Item;
|
{
|
||||||
type Error = U::Error;
|
Pin::new(&mut self.inner).poll_next(cx)
|
||||||
|
}
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
pub fn flush(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), U::Error>>
|
||||||
self.inner.poll()
|
where
|
||||||
|
T: AsyncWrite + Unpin,
|
||||||
|
U: Encoder + Unpin,
|
||||||
|
{
|
||||||
|
Pin::new(self.inner.get_mut()).poll_flush(cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, U> Sink for Framed<T, U>
|
impl<T, U> Stream for Framed<T, U>
|
||||||
where
|
where
|
||||||
T: AsyncWrite,
|
T: AsyncRead + Unpin,
|
||||||
U: Encoder,
|
U: Decoder + Unpin,
|
||||||
|
{
|
||||||
|
type Item = Result<U::Item, U::Error>;
|
||||||
|
|
||||||
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||||
|
Pin::new(&mut self.as_mut().inner).poll_next(cx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, U> Sink<U::Item> for Framed<T, U>
|
||||||
|
where
|
||||||
|
T: AsyncWrite + Unpin,
|
||||||
|
U: Encoder + Unpin,
|
||||||
U::Error: From<io::Error>,
|
U::Error: From<io::Error>,
|
||||||
{
|
{
|
||||||
type SinkItem = U::Item;
|
type Error = U::Error;
|
||||||
type SinkError = U::Error;
|
|
||||||
|
fn poll_ready(
|
||||||
|
mut self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
) -> Poll<Result<(), Self::Error>> {
|
||||||
|
Pin::new(&mut self.as_mut().inner.get_mut()).poll_ready(cx)
|
||||||
|
}
|
||||||
|
|
||||||
fn start_send(
|
fn start_send(
|
||||||
&mut self,
|
mut self: Pin<&mut Self>,
|
||||||
item: Self::SinkItem,
|
item: <U as Encoder>::Item,
|
||||||
) -> StartSend<Self::SinkItem, Self::SinkError> {
|
) -> Result<(), Self::Error> {
|
||||||
self.inner.get_mut().start_send(item)
|
Pin::new(&mut self.as_mut().inner.get_mut()).start_send(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
|
fn poll_flush(
|
||||||
self.inner.get_mut().poll_complete()
|
mut self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
) -> Poll<Result<(), Self::Error>> {
|
||||||
|
Pin::new(&mut self.as_mut().inner.get_mut()).poll_flush(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn close(&mut self) -> Poll<(), Self::SinkError> {
|
fn poll_close(
|
||||||
self.inner.get_mut().close()
|
mut self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
) -> Poll<Result<(), Self::Error>> {
|
||||||
|
Pin::new(&mut self.as_mut().inner.get_mut()).poll_close(cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,6 +315,14 @@ impl<T: AsyncRead, U> AsyncRead for Fuse<T, U> {
|
|||||||
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
|
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
|
||||||
self.0.prepare_uninitialized_buffer(buf)
|
self.0.prepare_uninitialized_buffer(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn poll_read(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
buf: &mut [u8],
|
||||||
|
) -> Poll<io::Result<usize>> {
|
||||||
|
unsafe { self.map_unchecked_mut(|s| &mut s.0).poll_read(cx, buf) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Write, U> Write for Fuse<T, U> {
|
impl<T: Write, U> Write for Fuse<T, U> {
|
||||||
@ -297,8 +336,20 @@ impl<T: Write, U> Write for Fuse<T, U> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncWrite, U> AsyncWrite for Fuse<T, U> {
|
impl<T: AsyncWrite, U> AsyncWrite for Fuse<T, U> {
|
||||||
fn shutdown(&mut self) -> Poll<(), io::Error> {
|
fn poll_write(
|
||||||
self.0.shutdown()
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
buf: &[u8],
|
||||||
|
) -> Poll<io::Result<usize>> {
|
||||||
|
unsafe { self.map_unchecked_mut(|s| &mut s.0).poll_write(cx, buf) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||||
|
unsafe { self.map_unchecked_mut(|s| &mut s.0).poll_flush(cx) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||||
|
unsafe { self.map_unchecked_mut(|s| &mut s.0).poll_shutdown(cx) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures::{try_ready, Async, Poll, Sink, StartSend, Stream};
|
use futures::{Sink, Stream};
|
||||||
use log::trace;
|
use log::trace;
|
||||||
use tokio_codec::Decoder;
|
use tokio_codec::Decoder;
|
||||||
use tokio_io::AsyncRead;
|
use tokio_io::AsyncRead;
|
||||||
@ -83,34 +85,45 @@ where
|
|||||||
T: AsyncRead,
|
T: AsyncRead,
|
||||||
D: Decoder,
|
D: Decoder,
|
||||||
{
|
{
|
||||||
type Item = D::Item;
|
type Item = Result<D::Item, D::Error>;
|
||||||
type Error = D::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||||
self.inner.poll()
|
unsafe { self.map_unchecked_mut(|s| &mut s.inner).poll_next(cx) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, D> Sink for FramedRead<T, D>
|
impl<I, T, D> Sink<I> for FramedRead<T, D>
|
||||||
where
|
where
|
||||||
T: Sink,
|
T: Sink<I>,
|
||||||
{
|
{
|
||||||
type SinkItem = T::SinkItem;
|
type Error = T::Error;
|
||||||
type SinkError = T::SinkError;
|
|
||||||
|
|
||||||
fn start_send(
|
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
&mut self,
|
unsafe {
|
||||||
item: Self::SinkItem,
|
self.map_unchecked_mut(|s| &mut s.inner.inner.0)
|
||||||
) -> StartSend<Self::SinkItem, Self::SinkError> {
|
.poll_ready(cx)
|
||||||
self.inner.inner.0.start_send(item)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
|
fn start_send(self: Pin<&mut Self>, item: I) -> Result<(), Self::Error> {
|
||||||
self.inner.inner.0.poll_complete()
|
unsafe {
|
||||||
|
self.map_unchecked_mut(|s| &mut s.inner.inner.0)
|
||||||
|
.start_send(item)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn close(&mut self) -> Poll<(), Self::SinkError> {
|
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.inner.inner.0.close()
|
unsafe {
|
||||||
|
self.map_unchecked_mut(|s| &mut s.inner.inner.0)
|
||||||
|
.poll_flush(cx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
|
unsafe {
|
||||||
|
self.map_unchecked_mut(|s| &mut s.inner.inner.0)
|
||||||
|
.poll_close(cx)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,46 +187,61 @@ impl<T> FramedRead2<T> {
|
|||||||
|
|
||||||
impl<T> Stream for FramedRead2<T>
|
impl<T> Stream for FramedRead2<T>
|
||||||
where
|
where
|
||||||
T: AsyncRead + Decoder,
|
T: tokio_io::AsyncRead + Decoder,
|
||||||
{
|
{
|
||||||
type Item = T::Item;
|
type Item = Result<T::Item, T::Error>;
|
||||||
type Error = T::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||||
|
let mut this = unsafe { self.get_unchecked_mut() };
|
||||||
loop {
|
loop {
|
||||||
// Repeatedly call `decode` or `decode_eof` as long as it is
|
// Repeatedly call `decode` or `decode_eof` as long as it is
|
||||||
// "readable". Readable is defined as not having returned `None`. If
|
// "readable". Readable is defined as not having returned `None`. If
|
||||||
// the upstream has returned EOF, and the decoder is no longer
|
// the upstream has returned EOF, and the decoder is no longer
|
||||||
// readable, it can be assumed that the decoder will never become
|
// readable, it can be assumed that the decoder will never become
|
||||||
// readable again, at which point the stream is terminated.
|
// readable again, at which point the stream is terminated.
|
||||||
if self.is_readable {
|
|
||||||
if self.eof {
|
if this.is_readable {
|
||||||
let frame = self.inner.decode_eof(&mut self.buffer)?;
|
if this.eof {
|
||||||
return Ok(Async::Ready(frame));
|
match this.inner.decode_eof(&mut this.buffer) {
|
||||||
|
Ok(Some(frame)) => return Poll::Ready(Some(Ok(frame))),
|
||||||
|
Ok(None) => return Poll::Ready(None),
|
||||||
|
Err(e) => return Poll::Ready(Some(Err(e))),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trace!("attempting to decode a frame");
|
trace!("attempting to decode a frame");
|
||||||
|
|
||||||
if let Some(frame) = self.inner.decode(&mut self.buffer)? {
|
match this.inner.decode(&mut this.buffer) {
|
||||||
|
Ok(Some(frame)) => {
|
||||||
trace!("frame decoded from buffer");
|
trace!("frame decoded from buffer");
|
||||||
return Ok(Async::Ready(Some(frame)));
|
return Poll::Ready(Some(Ok(frame)));
|
||||||
|
}
|
||||||
|
Err(e) => return Poll::Ready(Some(Err(e))),
|
||||||
|
_ => {
|
||||||
|
// Need more data
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.is_readable = false;
|
this.is_readable = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert!(!self.eof);
|
assert!(!this.eof);
|
||||||
|
|
||||||
// Otherwise, try to read more data and try again. Make sure we've
|
// Otherwise, try to read more data and try again. Make sure we've
|
||||||
// got room for at least one byte to read to ensure that we don't
|
// got room for at least one byte to read to ensure that we don't
|
||||||
// get a spurious 0 that looks like EOF
|
// get a spurious 0 that looks like EOF
|
||||||
self.buffer.reserve(1);
|
this.buffer.reserve(1);
|
||||||
if 0 == try_ready!(self.inner.read_buf(&mut self.buffer)) {
|
unsafe {
|
||||||
trace!("read 0 bytes, mark stream as eof");
|
match Pin::new_unchecked(&mut this.inner).poll_read(cx, &mut this.buffer) {
|
||||||
self.eof = true;
|
Poll::Pending => return Poll::Pending,
|
||||||
|
Poll::Ready(Err(e)) => return Poll::Ready(Some(Err(e.into()))),
|
||||||
|
Poll::Ready(Ok(0)) => {
|
||||||
|
this.eof = true;
|
||||||
}
|
}
|
||||||
|
Poll::Ready(Ok(_cnt)) => {}
|
||||||
self.is_readable = true;
|
}
|
||||||
|
}
|
||||||
|
this.is_readable = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io::{self, Read};
|
use std::io::{self, Read};
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures::{try_ready, Async, AsyncSink, Poll, Sink, StartSend, Stream};
|
use futures::{ready, Sink, Stream};
|
||||||
use log::trace;
|
use log::trace;
|
||||||
use tokio_codec::{Decoder, Encoder};
|
use tokio_codec::{Decoder, Encoder};
|
||||||
use tokio_io::{AsyncRead, AsyncWrite};
|
use tokio_io::{AsyncRead, AsyncWrite};
|
||||||
@ -95,24 +97,27 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, E> Sink for FramedWrite<T, E>
|
impl<T, E> Sink<E::Item> for FramedWrite<T, E>
|
||||||
where
|
where
|
||||||
T: AsyncWrite,
|
T: AsyncWrite,
|
||||||
E: Encoder,
|
E: Encoder,
|
||||||
{
|
{
|
||||||
type SinkItem = E::Item;
|
type Error = E::Error;
|
||||||
type SinkError = E::Error;
|
|
||||||
|
|
||||||
fn start_send(&mut self, item: E::Item) -> StartSend<E::Item, E::Error> {
|
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.inner.start_send(item)
|
unsafe { self.map_unchecked_mut(|s| &mut s.inner).poll_ready(cx) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
|
fn start_send(self: Pin<&mut Self>, item: <E as Encoder>::Item) -> Result<(), Self::Error> {
|
||||||
self.inner.poll_complete()
|
unsafe { self.map_unchecked_mut(|s| &mut s.inner).start_send(item) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn close(&mut self) -> Poll<(), Self::SinkError> {
|
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(self.inner.close()?)
|
unsafe { self.map_unchecked_mut(|s| &mut s.inner).poll_flush(cx) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
|
unsafe { self.map_unchecked_mut(|s| &mut s.inner).poll_close(cx) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,10 +126,12 @@ where
|
|||||||
T: Stream,
|
T: Stream,
|
||||||
{
|
{
|
||||||
type Item = T::Item;
|
type Item = T::Item;
|
||||||
type Error = T::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||||
self.inner.inner.0.poll()
|
unsafe {
|
||||||
|
self.map_unchecked_mut(|s| &mut s.inner.inner.0)
|
||||||
|
.poll_next(cx)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,60 +227,75 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Sink for FramedWrite2<T>
|
impl<T> Sink<T::Item> for FramedWrite2<T>
|
||||||
where
|
where
|
||||||
T: AsyncWrite + Encoder,
|
T: AsyncWrite + Encoder,
|
||||||
{
|
{
|
||||||
type SinkItem = T::Item;
|
type Error = T::Error;
|
||||||
type SinkError = T::Error;
|
|
||||||
|
|
||||||
fn start_send(&mut self, item: T::Item) -> StartSend<T::Item, T::Error> {
|
fn poll_ready(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
// Check the buffer capacity
|
|
||||||
let len = self.buffer.len();
|
let len = self.buffer.len();
|
||||||
if len >= self.high_watermark {
|
if len >= self.high_watermark {
|
||||||
return Ok(AsyncSink::NotReady(item));
|
return Poll::Pending;
|
||||||
|
} else {
|
||||||
|
return Poll::Ready(Ok(()));
|
||||||
}
|
}
|
||||||
if len < self.low_watermark {
|
|
||||||
self.buffer.reserve(self.high_watermark - len)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.inner.encode(item, &mut self.buffer)?;
|
fn start_send(self: Pin<&mut Self>, item: <T as Encoder>::Item) -> Result<(), Self::Error> {
|
||||||
|
let this = unsafe { self.get_unchecked_mut() };
|
||||||
|
|
||||||
Ok(AsyncSink::Ready)
|
// Check the buffer capacity
|
||||||
|
let len = this.buffer.len();
|
||||||
|
if len < this.low_watermark {
|
||||||
|
this.buffer.reserve(this.high_watermark - len)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
|
this.inner.encode(item, &mut this.buffer)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
|
let this = unsafe { self.get_unchecked_mut() };
|
||||||
trace!("flushing framed transport");
|
trace!("flushing framed transport");
|
||||||
|
|
||||||
while !self.buffer.is_empty() {
|
while !this.buffer.is_empty() {
|
||||||
trace!("writing; remaining={}", self.buffer.len());
|
trace!("writing; remaining={}", this.buffer.len());
|
||||||
|
|
||||||
let n = try_ready!(self.inner.poll_write(&self.buffer));
|
let n = ready!(
|
||||||
|
unsafe { Pin::new_unchecked(&mut this.inner) }.poll_write(cx, &this.buffer)
|
||||||
|
)?;
|
||||||
|
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
return Err(io::Error::new(
|
return Poll::Ready(Err(io::Error::new(
|
||||||
io::ErrorKind::WriteZero,
|
io::ErrorKind::WriteZero,
|
||||||
"failed to \
|
"failed to \
|
||||||
write frame to transport",
|
write frame to transport",
|
||||||
)
|
)
|
||||||
.into());
|
.into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add a way to `bytes` to do this w/o returning the drained
|
// TODO: Add a way to `bytes` to do this w/o returning the drained
|
||||||
// data.
|
// data.
|
||||||
let _ = self.buffer.split_to(n);
|
let _ = this.buffer.split_to(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try flushing the underlying IO
|
// Try flushing the underlying IO
|
||||||
try_ready!(self.inner.poll_flush());
|
ready!(unsafe { Pin::new_unchecked(&mut this.inner) }.poll_flush(cx))?;
|
||||||
|
|
||||||
trace!("framed transport flushed");
|
trace!("framed transport flushed");
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn close(&mut self) -> Poll<(), Self::SinkError> {
|
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
try_ready!(self.poll_complete());
|
let mut this = unsafe { self.get_unchecked_mut() };
|
||||||
Ok(self.inner.shutdown()?)
|
ready!(
|
||||||
|
unsafe { Pin::new_unchecked(&mut this).map_unchecked_mut(|s| *s) }.poll_flush(cx)
|
||||||
|
)?;
|
||||||
|
ready!(unsafe { Pin::new_unchecked(&mut this.inner) }.poll_shutdown(cx))?;
|
||||||
|
|
||||||
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -300,4 +322,12 @@ impl<T: AsyncRead> AsyncRead for FramedWrite2<T> {
|
|||||||
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
|
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
|
||||||
self.inner.prepare_uninitialized_buffer(buf)
|
self.inner.prepare_uninitialized_buffer(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn poll_read(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
buf: &mut [u8],
|
||||||
|
) -> Poll<io::Result<usize>> {
|
||||||
|
unsafe { self.map_unchecked_mut(|s| &mut s.inner).poll_read(cx, buf) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,4 +21,5 @@ pub use self::framed_read::FramedRead;
|
|||||||
pub use self::framed_write::FramedWrite;
|
pub use self::framed_write::FramedWrite;
|
||||||
|
|
||||||
pub use tokio_codec::{Decoder, Encoder};
|
pub use tokio_codec::{Decoder, Encoder};
|
||||||
|
// TODO: Migrate to futures asyncRead
|
||||||
pub use tokio_io::{AsyncRead, AsyncWrite};
|
pub use tokio_io::{AsyncRead, AsyncWrite};
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-connect"
|
name = "actix-connect"
|
||||||
version = "0.3.0"
|
version = "1.0.0-alpha.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix Connector - tcp connector service"
|
description = "Actix connect - tcp connector service"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
homepage = "https://actix.rs"
|
homepage = "https://actix.rs"
|
||||||
repository = "https://github.com/actix/actix-net.git"
|
repository = "https://github.com/actix/actix-net.git"
|
||||||
@ -14,7 +14,7 @@ edition = "2018"
|
|||||||
workspace = ".."
|
workspace = ".."
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
features = ["ssl", "uri"]
|
features = ["openssl", "rustls", "uri"]
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "actix_connect"
|
name = "actix_connect"
|
||||||
@ -24,38 +24,41 @@ path = "src/lib.rs"
|
|||||||
default = ["uri"]
|
default = ["uri"]
|
||||||
|
|
||||||
# openssl
|
# openssl
|
||||||
ssl = ["openssl", "tokio-openssl"]
|
openssl = ["open-ssl", "tokio-openssl"]
|
||||||
|
|
||||||
#rustls
|
#rustls
|
||||||
rust-tls = ["rustls", "tokio-rustls", "webpki"]
|
rustls = ["rust-tls", "tokio-rustls", "webpki"]
|
||||||
|
|
||||||
# support http::Uri as connect address
|
# support http::Uri as connect address
|
||||||
uri = ["http"]
|
uri = ["http"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-service = "0.4.0"
|
actix-service = "1.0.0-alpha.1"
|
||||||
actix-codec = "0.1.2"
|
actix-codec = "0.2.0-alpha.1"
|
||||||
actix-utils = "0.4.0"
|
actix-utils = "0.5.0-alpha.1"
|
||||||
actix-rt = "0.2.5"
|
actix-rt = "1.0.0-alpha.1"
|
||||||
derive_more = "0.15"
|
derive_more = "0.15"
|
||||||
either = "1.5.2"
|
either = "1.5.2"
|
||||||
futures = "0.1.25"
|
futures = "0.3.1"
|
||||||
|
pin-project = "0.4.5"
|
||||||
http = { version = "0.1.17", optional = true }
|
http = { version = "0.1.17", optional = true }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
tokio-tcp = "0.1.3"
|
tokio-net = "=0.2.0-alpha.6"
|
||||||
tokio-current-thread = "0.1.5"
|
tokio-executor = "=0.2.0-alpha.6"
|
||||||
trust-dns-resolver = { version="0.11.0", default-features = false }
|
trust-dns-resolver = { version="0.18.0-alpha.1", default-features = false }
|
||||||
|
|
||||||
# openssl
|
# openssl
|
||||||
openssl = { version="0.10", optional = true }
|
open-ssl = { version="0.10", package = "openssl", optional = true }
|
||||||
tokio-openssl = { version="0.3", optional = true }
|
tokio-openssl = { version = "0.4.0-alpha.6", optional = true }
|
||||||
|
|
||||||
#rustls
|
#rustls
|
||||||
rustls = { version = "0.16.0", optional = true }
|
rust-tls = { version = "0.16.0", package = "rustls", optional = true }
|
||||||
tokio-rustls = { version = "0.10.0", optional = true }
|
# tokio-rustls = { version = "0.10.0", optional = true }
|
||||||
|
tokio-rustls = { git = "https://github.com/quininer/tokio-rustls.git", branch = "tokio-0.2", optional = true }
|
||||||
webpki = { version = "0.21", optional = true }
|
webpki = { version = "0.21", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
actix-testing = { version="0.2.0" }
|
actix-testing = { version="0.3.0-alpha.1" }
|
||||||
actix-server-config = "0.2.0"
|
actix-server-config = "0.3.0-alpha.1"
|
||||||
|
tokio = "0.2.0-alpha.6"
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
use std::future::Future;
|
||||||
|
use std::io;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_service::{NewService, Service};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::future::{err, ok, Either, FutureResult};
|
use futures::future::{err, ok, BoxFuture, Either, FutureExt, Ready};
|
||||||
use futures::{Async, Future, Poll};
|
use pin_project::pin_project;
|
||||||
use tokio_tcp::{ConnectFuture, TcpStream};
|
use tokio_net::tcp::TcpStream;
|
||||||
|
|
||||||
use super::connect::{Address, Connect, Connection};
|
use super::connect::{Address, Connect, Connection};
|
||||||
use super::error::ConnectError;
|
use super::error::ConnectError;
|
||||||
@ -37,14 +41,14 @@ impl<T> Clone for TcpConnectorFactory<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> NewService for TcpConnectorFactory<T> {
|
impl<T: Address> ServiceFactory for TcpConnectorFactory<T> {
|
||||||
type Request = Connect<T>;
|
type Request = Connect<T>;
|
||||||
type Response = Connection<T, TcpStream>;
|
type Response = Connection<T, TcpStream>;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Config = ();
|
type Config = ();
|
||||||
type Service = TcpConnector<T>;
|
type Service = TcpConnector<T>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, _: &()) -> Self::Future {
|
fn new_service(&self, _: &()) -> Self::Future {
|
||||||
ok(self.service())
|
ok(self.service())
|
||||||
@ -71,10 +75,10 @@ impl<T: Address> Service for TcpConnector<T> {
|
|||||||
type Request = Connect<T>;
|
type Request = Connect<T>;
|
||||||
type Response = Connection<T, TcpStream>;
|
type Response = Connection<T, TcpStream>;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Future = Either<TcpConnectorResponse<T>, FutureResult<Self::Response, Self::Error>>;
|
type Future = Either<TcpConnectorResponse<T>, Ready<Result<Self::Response, Self::Error>>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Connect<T>) -> Self::Future {
|
fn call(&mut self, req: Connect<T>) -> Self::Future {
|
||||||
@ -82,21 +86,22 @@ impl<T: Address> Service for TcpConnector<T> {
|
|||||||
let Connect { req, addr, .. } = req;
|
let Connect { req, addr, .. } = req;
|
||||||
|
|
||||||
if let Some(addr) = addr {
|
if let Some(addr) = addr {
|
||||||
Either::A(TcpConnectorResponse::new(req, port, addr))
|
Either::Left(TcpConnectorResponse::new(req, port, addr))
|
||||||
} else {
|
} else {
|
||||||
error!("TCP connector: got unresolved address");
|
error!("TCP connector: got unresolved address");
|
||||||
Either::B(err(ConnectError::Unresolverd))
|
Either::Right(err(ConnectError::Unresolverd))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pin_project]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
/// Tcp stream connector response future
|
/// Tcp stream connector response future
|
||||||
pub struct TcpConnectorResponse<T> {
|
pub struct TcpConnectorResponse<T> {
|
||||||
req: Option<T>,
|
req: Option<T>,
|
||||||
port: u16,
|
port: u16,
|
||||||
addrs: Option<VecDeque<SocketAddr>>,
|
addrs: Option<VecDeque<SocketAddr>>,
|
||||||
stream: Option<ConnectFuture>,
|
stream: Option<BoxFuture<'static, Result<TcpStream, io::Error>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> TcpConnectorResponse<T> {
|
impl<T: Address> TcpConnectorResponse<T> {
|
||||||
@ -116,7 +121,7 @@ impl<T: Address> TcpConnectorResponse<T> {
|
|||||||
req: Some(req),
|
req: Some(req),
|
||||||
port,
|
port,
|
||||||
addrs: None,
|
addrs: None,
|
||||||
stream: Some(TcpStream::connect(&addr)),
|
stream: Some(TcpStream::connect(addr).boxed()),
|
||||||
},
|
},
|
||||||
either::Either::Right(addrs) => TcpConnectorResponse {
|
either::Either::Right(addrs) => TcpConnectorResponse {
|
||||||
req: Some(req),
|
req: Some(req),
|
||||||
@ -129,40 +134,40 @@ impl<T: Address> TcpConnectorResponse<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> Future for TcpConnectorResponse<T> {
|
impl<T: Address> Future for TcpConnectorResponse<T> {
|
||||||
type Item = Connection<T, TcpStream>;
|
type Output = Result<Connection<T, TcpStream>, ConnectError>;
|
||||||
type Error = ConnectError;
|
|
||||||
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||||
|
let this = self.project();
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
// connect
|
// connect
|
||||||
loop {
|
loop {
|
||||||
if let Some(new) = self.stream.as_mut() {
|
if let Some(new) = this.stream.as_mut() {
|
||||||
match new.poll() {
|
match new.as_mut().poll(cx) {
|
||||||
Ok(Async::Ready(sock)) => {
|
Poll::Ready(Ok(sock)) => {
|
||||||
let req = self.req.take().unwrap();
|
let req = this.req.take().unwrap();
|
||||||
trace!(
|
trace!(
|
||||||
"TCP connector - successfully connected to connecting to {:?} - {:?}",
|
"TCP connector - successfully connected to connecting to {:?} - {:?}",
|
||||||
req.host(), sock.peer_addr()
|
req.host(), sock.peer_addr()
|
||||||
);
|
);
|
||||||
return Ok(Async::Ready(Connection::new(sock, req)));
|
return Poll::Ready(Ok(Connection::new(sock, req)));
|
||||||
}
|
}
|
||||||
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
Poll::Pending => return Poll::Pending,
|
||||||
Err(err) => {
|
Poll::Ready(Err(err)) => {
|
||||||
trace!(
|
trace!(
|
||||||
"TCP connector - failed to connect to connecting to {:?} port: {}",
|
"TCP connector - failed to connect to connecting to {:?} port: {}",
|
||||||
self.req.as_ref().unwrap().host(),
|
this.req.as_ref().unwrap().host(),
|
||||||
self.port,
|
this.port,
|
||||||
);
|
);
|
||||||
if self.addrs.is_none() || self.addrs.as_ref().unwrap().is_empty() {
|
if this.addrs.is_none() || this.addrs.as_ref().unwrap().is_empty() {
|
||||||
return Err(err.into());
|
return Poll::Ready(Err(err.into()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to connect
|
// try to connect
|
||||||
self.stream = Some(TcpStream::connect(
|
let addr = this.addrs.as_mut().unwrap().pop_front().unwrap();
|
||||||
&self.addrs.as_mut().unwrap().pop_front().unwrap(),
|
*this.stream = Some(TcpStream::connect(addr).boxed());
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,12 +31,12 @@ pub use self::resolver::{Resolver, ResolverFactory};
|
|||||||
pub use self::service::{ConnectService, ConnectServiceFactory, TcpConnectService};
|
pub use self::service::{ConnectService, ConnectServiceFactory, TcpConnectService};
|
||||||
|
|
||||||
use actix_rt::Arbiter;
|
use actix_rt::Arbiter;
|
||||||
use actix_service::{NewService, Service, ServiceExt};
|
use actix_service::{pipeline, pipeline_factory, Service, ServiceFactory};
|
||||||
use tokio_tcp::TcpStream;
|
use tokio_net::tcp::TcpStream;
|
||||||
|
|
||||||
pub fn start_resolver(cfg: ResolverConfig, opts: ResolverOpts) -> AsyncResolver {
|
pub fn start_resolver(cfg: ResolverConfig, opts: ResolverOpts) -> AsyncResolver {
|
||||||
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
||||||
tokio_current_thread::spawn(bg);
|
tokio_executor::current_thread::spawn(bg);
|
||||||
resolver
|
resolver
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ pub(crate) fn get_default_resolver() -> AsyncResolver {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
let (resolver, bg) = AsyncResolver::new(cfg, opts);
|
||||||
tokio_current_thread::spawn(bg);
|
tokio_executor::current_thread::spawn(bg);
|
||||||
|
|
||||||
Arbiter::set_item(DefaultResolver(resolver.clone()));
|
Arbiter::set_item(DefaultResolver(resolver.clone()));
|
||||||
resolver
|
resolver
|
||||||
@ -70,37 +70,37 @@ pub fn start_default_resolver() -> AsyncResolver {
|
|||||||
pub fn new_connector<T: Address>(
|
pub fn new_connector<T: Address>(
|
||||||
resolver: AsyncResolver,
|
resolver: AsyncResolver,
|
||||||
) -> impl Service<Request = Connect<T>, Response = Connection<T, TcpStream>, Error = ConnectError>
|
) -> impl Service<Request = Connect<T>, Response = Connection<T, TcpStream>, Error = ConnectError>
|
||||||
+ Clone {
|
{
|
||||||
Resolver::new(resolver).and_then(TcpConnector::new())
|
pipeline(Resolver::new(resolver)).and_then(TcpConnector::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create tcp connector service
|
/// Create tcp connector service
|
||||||
pub fn new_connector_factory<T: Address>(
|
pub fn new_connector_factory<T: Address>(
|
||||||
resolver: AsyncResolver,
|
resolver: AsyncResolver,
|
||||||
) -> impl NewService<
|
) -> impl ServiceFactory<
|
||||||
Config = (),
|
Config = (),
|
||||||
Request = Connect<T>,
|
Request = Connect<T>,
|
||||||
Response = Connection<T, TcpStream>,
|
Response = Connection<T, TcpStream>,
|
||||||
Error = ConnectError,
|
Error = ConnectError,
|
||||||
InitError = (),
|
InitError = (),
|
||||||
> + Clone {
|
> {
|
||||||
ResolverFactory::new(resolver).and_then(TcpConnectorFactory::new())
|
pipeline_factory(ResolverFactory::new(resolver)).and_then(TcpConnectorFactory::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create connector service with default parameters
|
/// Create connector service with default parameters
|
||||||
pub fn default_connector<T: Address>(
|
pub fn default_connector<T: Address>(
|
||||||
) -> impl Service<Request = Connect<T>, Response = Connection<T, TcpStream>, Error = ConnectError>
|
) -> impl Service<Request = Connect<T>, Response = Connection<T, TcpStream>, Error = ConnectError>
|
||||||
+ Clone {
|
{
|
||||||
Resolver::default().and_then(TcpConnector::new())
|
pipeline(Resolver::default()).and_then(TcpConnector::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create connector service factory with default parameters
|
/// Create connector service factory with default parameters
|
||||||
pub fn default_connector_factory<T: Address>() -> impl NewService<
|
pub fn default_connector_factory<T: Address>() -> impl ServiceFactory<
|
||||||
Config = (),
|
Config = (),
|
||||||
Request = Connect<T>,
|
Request = Connect<T>,
|
||||||
Response = Connection<T, TcpStream>,
|
Response = Connection<T, TcpStream>,
|
||||||
Error = ConnectError,
|
Error = ConnectError,
|
||||||
InitError = (),
|
InitError = (),
|
||||||
> + Clone {
|
> {
|
||||||
ResolverFactory::default().and_then(TcpConnectorFactory::new())
|
pipeline_factory(ResolverFactory::default()).and_then(TcpConnectorFactory::new())
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_service::{NewService, Service};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::future::{ok, Either, FutureResult};
|
use futures::future::{ok, Either, Ready};
|
||||||
use futures::{Async, Future, Poll};
|
use pin_project::pin_project;
|
||||||
use trust_dns_resolver::lookup_ip::LookupIpFuture;
|
use trust_dns_resolver::lookup_ip::LookupIpFuture;
|
||||||
use trust_dns_resolver::{AsyncResolver, Background};
|
use trust_dns_resolver::{AsyncResolver, Background};
|
||||||
|
|
||||||
@ -52,14 +55,14 @@ impl<T> Clone for ResolverFactory<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> NewService for ResolverFactory<T> {
|
impl<T: Address> ServiceFactory for ResolverFactory<T> {
|
||||||
type Request = Connect<T>;
|
type Request = Connect<T>;
|
||||||
type Response = Connect<T>;
|
type Response = Connect<T>;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Config = ();
|
type Config = ();
|
||||||
type Service = Resolver<T>;
|
type Service = Resolver<T>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, _: &()) -> Self::Future {
|
fn new_service(&self, _: &()) -> Self::Future {
|
||||||
ok(self.service())
|
ok(self.service())
|
||||||
@ -104,32 +107,34 @@ impl<T: Address> Service for Resolver<T> {
|
|||||||
type Request = Connect<T>;
|
type Request = Connect<T>;
|
||||||
type Response = Connect<T>;
|
type Response = Connect<T>;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Future = Either<ResolverFuture<T>, FutureResult<Connect<T>, Self::Error>>;
|
type Future = Either<ResolverFuture<T>, Ready<Result<Connect<T>, Self::Error>>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, mut req: Connect<T>) -> Self::Future {
|
fn call(&mut self, mut req: Connect<T>) -> Self::Future {
|
||||||
if req.addr.is_some() {
|
if req.addr.is_some() {
|
||||||
Either::B(ok(req))
|
Either::Right(ok(req))
|
||||||
} else if let Ok(ip) = req.host().parse() {
|
} else if let Ok(ip) = req.host().parse() {
|
||||||
req.addr = Some(either::Either::Left(SocketAddr::new(ip, req.port())));
|
req.addr = Some(either::Either::Left(SocketAddr::new(ip, req.port())));
|
||||||
Either::B(ok(req))
|
Either::Right(ok(req))
|
||||||
} else {
|
} else {
|
||||||
trace!("DNS resolver: resolving host {:?}", req.host());
|
trace!("DNS resolver: resolving host {:?}", req.host());
|
||||||
if self.resolver.is_none() {
|
if self.resolver.is_none() {
|
||||||
self.resolver = Some(get_default_resolver());
|
self.resolver = Some(get_default_resolver());
|
||||||
}
|
}
|
||||||
Either::A(ResolverFuture::new(req, self.resolver.as_ref().unwrap()))
|
Either::Left(ResolverFuture::new(req, self.resolver.as_ref().unwrap()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pin_project]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
/// Resolver future
|
/// Resolver future
|
||||||
pub struct ResolverFuture<T: Address> {
|
pub struct ResolverFuture<T: Address> {
|
||||||
req: Option<Connect<T>>,
|
req: Option<Connect<T>>,
|
||||||
|
#[pin]
|
||||||
lookup: Background<LookupIpFuture>,
|
lookup: Background<LookupIpFuture>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,22 +154,15 @@ impl<T: Address> ResolverFuture<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> Future for ResolverFuture<T> {
|
impl<T: Address> Future for ResolverFuture<T> {
|
||||||
type Item = Connect<T>;
|
type Output = Result<Connect<T>, ConnectError>;
|
||||||
type Error = ConnectError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||||
match self.lookup.poll().map_err(|e| {
|
let this = self.project();
|
||||||
trace!(
|
|
||||||
"DNS resolver: failed to resolve host {:?} err: {}",
|
|
||||||
self.req.as_ref().unwrap().host(),
|
|
||||||
e
|
|
||||||
);
|
|
||||||
e
|
|
||||||
})? {
|
|
||||||
Async::NotReady => Ok(Async::NotReady),
|
|
||||||
Async::Ready(ips) => {
|
|
||||||
let req = self.req.take().unwrap();
|
|
||||||
|
|
||||||
|
match this.lookup.poll(cx) {
|
||||||
|
Poll::Pending => Poll::Pending,
|
||||||
|
Poll::Ready(Ok(ips)) => {
|
||||||
|
let req = this.req.take().unwrap();
|
||||||
let port = req.port();
|
let port = req.port();
|
||||||
let req = req.set_addrs(ips.iter().map(|ip| SocketAddr::new(ip, port)));
|
let req = req.set_addrs(ips.iter().map(|ip| SocketAddr::new(ip, port)));
|
||||||
|
|
||||||
@ -175,11 +173,19 @@ impl<T: Address> Future for ResolverFuture<T> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if req.addr.is_none() {
|
if req.addr.is_none() {
|
||||||
Err(ConnectError::NoRecords)
|
Poll::Ready(Err(ConnectError::NoRecords))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::Ready(req))
|
Poll::Ready(Ok(req))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Poll::Ready(Err(e)) => {
|
||||||
|
trace!(
|
||||||
|
"DNS resolver: failed to resolve host {:?} err: {}",
|
||||||
|
this.req.as_ref().unwrap().host(),
|
||||||
|
e
|
||||||
|
);
|
||||||
|
Poll::Ready(Err(e.into()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
use actix_service::{NewService, Service};
|
use std::future::Future;
|
||||||
use futures::future::{ok, FutureResult};
|
use std::pin::Pin;
|
||||||
use futures::{try_ready, Async, Future, Poll};
|
use std::task::{Context, Poll};
|
||||||
use tokio_tcp::TcpStream;
|
|
||||||
|
use actix_service::{Service, ServiceFactory};
|
||||||
|
use either::Either;
|
||||||
|
use futures::future::{ok, Ready};
|
||||||
|
use tokio_net::tcp::TcpStream;
|
||||||
use trust_dns_resolver::AsyncResolver;
|
use trust_dns_resolver::AsyncResolver;
|
||||||
|
|
||||||
use crate::connect::{Address, Connect, Connection};
|
use crate::connect::{Address, Connect, Connection};
|
||||||
@ -14,7 +18,7 @@ pub struct ConnectServiceFactory<T> {
|
|||||||
resolver: ResolverFactory<T>,
|
resolver: ResolverFactory<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> ConnectServiceFactory<T> {
|
impl<T: Unpin> ConnectServiceFactory<T> {
|
||||||
/// Construct new ConnectService factory
|
/// Construct new ConnectService factory
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
ConnectServiceFactory {
|
ConnectServiceFactory {
|
||||||
@ -66,14 +70,14 @@ impl<T> Clone for ConnectServiceFactory<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> NewService for ConnectServiceFactory<T> {
|
impl<T: Address + Unpin> ServiceFactory for ConnectServiceFactory<T> {
|
||||||
type Request = Connect<T>;
|
type Request = Connect<T>;
|
||||||
type Response = Connection<T, TcpStream>;
|
type Response = Connection<T, TcpStream>;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Config = ();
|
type Config = ();
|
||||||
type Service = ConnectService<T>;
|
type Service = ConnectService<T>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, _: &()) -> Self::Future {
|
fn new_service(&self, _: &()) -> Self::Future {
|
||||||
ok(self.service())
|
ok(self.service())
|
||||||
@ -86,47 +90,66 @@ pub struct ConnectService<T> {
|
|||||||
resolver: Resolver<T>,
|
resolver: Resolver<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> Service for ConnectService<T> {
|
impl<T: Address + Unpin> Service for ConnectService<T> {
|
||||||
type Request = Connect<T>;
|
type Request = Connect<T>;
|
||||||
type Response = Connection<T, TcpStream>;
|
type Response = Connection<T, TcpStream>;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Future = ConnectServiceResponse<T>;
|
type Future = ConnectServiceResponse<T>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Connect<T>) -> Self::Future {
|
fn call(&mut self, req: Connect<T>) -> Self::Future {
|
||||||
ConnectServiceResponse {
|
ConnectServiceResponse {
|
||||||
fut1: Some(self.resolver.call(req)),
|
state: ConnectState::Resolve(self.resolver.call(req)),
|
||||||
fut2: None,
|
|
||||||
tcp: self.tcp.clone(),
|
tcp: self.tcp.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ConnectServiceResponse<T: Address> {
|
enum ConnectState<T: Address + Unpin> {
|
||||||
fut1: Option<<Resolver<T> as Service>::Future>,
|
Resolve(<Resolver<T> as Service>::Future),
|
||||||
fut2: Option<<TcpConnector<T> as Service>::Future>,
|
Connect(<TcpConnector<T> as Service>::Future),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address + Unpin> ConnectState<T> {
|
||||||
|
fn poll(
|
||||||
|
&mut self,
|
||||||
|
cx: &mut Context,
|
||||||
|
) -> Either<Poll<Result<Connection<T, TcpStream>, ConnectError>>, Connect<T>> {
|
||||||
|
match self {
|
||||||
|
ConnectState::Resolve(ref mut fut) => match Pin::new(fut).poll(cx) {
|
||||||
|
Poll::Pending => Either::Left(Poll::Pending),
|
||||||
|
Poll::Ready(Ok(res)) => Either::Right(res),
|
||||||
|
Poll::Ready(Err(err)) => Either::Left(Poll::Ready(Err(err))),
|
||||||
|
},
|
||||||
|
ConnectState::Connect(ref mut fut) => Either::Left(Pin::new(fut).poll(cx)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ConnectServiceResponse<T: Address + Unpin> {
|
||||||
|
state: ConnectState<T>,
|
||||||
tcp: TcpConnector<T>,
|
tcp: TcpConnector<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> Future for ConnectServiceResponse<T> {
|
impl<T: Address + Unpin> Future for ConnectServiceResponse<T> {
|
||||||
type Item = Connection<T, TcpStream>;
|
type Output = Result<Connection<T, TcpStream>, ConnectError>;
|
||||||
type Error = ConnectError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||||
if let Some(ref mut fut) = self.fut1 {
|
let res = match self.state.poll(cx) {
|
||||||
let res = try_ready!(fut.poll());
|
Either::Right(res) => {
|
||||||
let _ = self.fut1.take();
|
self.state = ConnectState::Connect(self.tcp.call(res));
|
||||||
self.fut2 = Some(self.tcp.call(res));
|
self.state.poll(cx)
|
||||||
}
|
}
|
||||||
|
Either::Left(res) => return res,
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(ref mut fut) = self.fut2 {
|
match res {
|
||||||
return fut.poll();
|
Either::Left(res) => res,
|
||||||
|
Either::Right(_) => panic!(),
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Async::NotReady)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,48 +159,73 @@ pub struct TcpConnectService<T> {
|
|||||||
resolver: Resolver<T>,
|
resolver: Resolver<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> Service for TcpConnectService<T> {
|
impl<T: Address + Unpin + 'static> Service for TcpConnectService<T> {
|
||||||
type Request = Connect<T>;
|
type Request = Connect<T>;
|
||||||
type Response = TcpStream;
|
type Response = TcpStream;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Future = TcpConnectServiceResponse<T>;
|
type Future = TcpConnectServiceResponse<T>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Connect<T>) -> Self::Future {
|
fn call(&mut self, req: Connect<T>) -> Self::Future {
|
||||||
TcpConnectServiceResponse {
|
TcpConnectServiceResponse {
|
||||||
fut1: Some(self.resolver.call(req)),
|
state: TcpConnectState::Resolve(self.resolver.call(req)),
|
||||||
fut2: None,
|
|
||||||
tcp: self.tcp.clone(),
|
tcp: self.tcp.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TcpConnectServiceResponse<T: Address> {
|
enum TcpConnectState<T: Address + Unpin> {
|
||||||
fut1: Option<<Resolver<T> as Service>::Future>,
|
Resolve(<Resolver<T> as Service>::Future),
|
||||||
fut2: Option<<TcpConnector<T> as Service>::Future>,
|
Connect(<TcpConnector<T> as Service>::Future),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Address + Unpin> TcpConnectState<T> {
|
||||||
|
fn poll(
|
||||||
|
&mut self,
|
||||||
|
cx: &mut Context,
|
||||||
|
) -> Either<Poll<Result<TcpStream, ConnectError>>, Connect<T>> {
|
||||||
|
match self {
|
||||||
|
TcpConnectState::Resolve(ref mut fut) => match Pin::new(fut).poll(cx) {
|
||||||
|
Poll::Pending => (),
|
||||||
|
Poll::Ready(Ok(res)) => return Either::Right(res),
|
||||||
|
Poll::Ready(Err(err)) => return Either::Left(Poll::Ready(Err(err))),
|
||||||
|
},
|
||||||
|
TcpConnectState::Connect(ref mut fut) => {
|
||||||
|
if let Poll::Ready(res) = Pin::new(fut).poll(cx) {
|
||||||
|
return match res {
|
||||||
|
Ok(conn) => Either::Left(Poll::Ready(Ok(conn.into_parts().0))),
|
||||||
|
Err(err) => Either::Left(Poll::Ready(Err(err))),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Either::Left(Poll::Pending)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TcpConnectServiceResponse<T: Address + Unpin> {
|
||||||
|
state: TcpConnectState<T>,
|
||||||
tcp: TcpConnector<T>,
|
tcp: TcpConnector<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> Future for TcpConnectServiceResponse<T> {
|
impl<T: Address + Unpin> Future for TcpConnectServiceResponse<T> {
|
||||||
type Item = TcpStream;
|
type Output = Result<TcpStream, ConnectError>;
|
||||||
type Error = ConnectError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||||
if let Some(ref mut fut) = self.fut1 {
|
let res = match self.state.poll(cx) {
|
||||||
let res = try_ready!(fut.poll());
|
Either::Right(res) => {
|
||||||
let _ = self.fut1.take();
|
self.state = TcpConnectState::Connect(self.tcp.call(res));
|
||||||
self.fut2 = Some(self.tcp.call(res));
|
self.state.poll(cx)
|
||||||
}
|
}
|
||||||
|
Either::Left(res) => return res,
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(ref mut fut) = self.fut2 {
|
match res {
|
||||||
if let Async::Ready(conn) = fut.poll()? {
|
Either::Left(res) => res,
|
||||||
return Ok(Async::Ready(conn.into_parts().0));
|
Either::Right(_) => panic!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Async::NotReady)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
//! SSL Services
|
//! SSL Services
|
||||||
|
|
||||||
#[cfg(feature = "ssl")]
|
#[cfg(feature = "openssl")]
|
||||||
mod openssl;
|
mod openssl;
|
||||||
#[cfg(feature = "ssl")]
|
#[cfg(feature = "openssl")]
|
||||||
pub use self::openssl::{
|
pub use self::openssl::{
|
||||||
OpensslConnectService, OpensslConnectServiceFactory, OpensslConnector,
|
OpensslConnectService, OpensslConnectServiceFactory, OpensslConnector,
|
||||||
};
|
};
|
||||||
#[cfg(feature = "rust-tls")]
|
|
||||||
|
#[cfg(feature = "rustls")]
|
||||||
mod rustls;
|
mod rustls;
|
||||||
#[cfg(feature = "rust-tls")]
|
#[cfg(feature = "rustls")]
|
||||||
pub use self::rustls::RustlsConnector;
|
pub use self::rustls::RustlsConnector;
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
use std::{fmt, io};
|
use std::{fmt, io};
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
use actix_codec::{AsyncRead, AsyncWrite};
|
||||||
use actix_service::{NewService, Service};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::{future::ok, future::FutureResult, try_ready, Async, Future, Poll};
|
use futures::future::{err, ok, Either, FutureExt, LocalBoxFuture, Ready};
|
||||||
use openssl::ssl::{HandshakeError, SslConnector};
|
use open_ssl::ssl::SslConnector;
|
||||||
use tokio_openssl::{ConnectAsync, SslConnectorExt, SslStream};
|
use tokio_net::tcp::TcpStream;
|
||||||
use tokio_tcp::TcpStream;
|
use tokio_openssl::{HandshakeError, SslStream};
|
||||||
use trust_dns_resolver::AsyncResolver;
|
use trust_dns_resolver::AsyncResolver;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -30,15 +33,15 @@ impl<T, U> OpensslConnector<T, U> {
|
|||||||
|
|
||||||
impl<T, U> OpensslConnector<T, U>
|
impl<T, U> OpensslConnector<T, U>
|
||||||
where
|
where
|
||||||
T: Address,
|
T: Address + Unpin + 'static,
|
||||||
U: AsyncRead + AsyncWrite + fmt::Debug,
|
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
|
||||||
{
|
{
|
||||||
pub fn service(
|
pub fn service(
|
||||||
connector: SslConnector,
|
connector: SslConnector,
|
||||||
) -> impl Service<
|
) -> impl Service<
|
||||||
Request = Connection<T, U>,
|
Request = Connection<T, U>,
|
||||||
Response = Connection<T, SslStream<U>>,
|
Response = Connection<T, SslStream<U>>,
|
||||||
Error = HandshakeError<U>,
|
Error = io::Error,
|
||||||
> {
|
> {
|
||||||
OpensslConnectorService {
|
OpensslConnectorService {
|
||||||
connector: connector,
|
connector: connector,
|
||||||
@ -56,17 +59,17 @@ impl<T, U> Clone for OpensslConnector<T, U> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address, U> NewService for OpensslConnector<T, U>
|
impl<T: Address + Unpin + 'static, U> ServiceFactory for OpensslConnector<T, U>
|
||||||
where
|
where
|
||||||
U: AsyncRead + AsyncWrite + fmt::Debug,
|
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
|
||||||
{
|
{
|
||||||
type Request = Connection<T, U>;
|
type Request = Connection<T, U>;
|
||||||
type Response = Connection<T, SslStream<U>>;
|
type Response = Connection<T, SslStream<U>>;
|
||||||
type Error = HandshakeError<U>;
|
type Error = io::Error;
|
||||||
type Config = ();
|
type Config = ();
|
||||||
type Service = OpensslConnectorService<T, U>;
|
type Service = OpensslConnectorService<T, U>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, _: &()) -> Self::Future {
|
fn new_service(&self, _: &()) -> Self::Future {
|
||||||
ok(OpensslConnectorService {
|
ok(OpensslConnectorService {
|
||||||
@ -90,52 +93,62 @@ impl<T, U> Clone for OpensslConnectorService<T, U> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address, U> Service for OpensslConnectorService<T, U>
|
impl<T: Address + Unpin + 'static, U> Service for OpensslConnectorService<T, U>
|
||||||
where
|
where
|
||||||
U: AsyncRead + AsyncWrite + fmt::Debug,
|
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
|
||||||
{
|
{
|
||||||
type Request = Connection<T, U>;
|
type Request = Connection<T, U>;
|
||||||
type Response = Connection<T, SslStream<U>>;
|
type Response = Connection<T, SslStream<U>>;
|
||||||
type Error = HandshakeError<U>;
|
type Error = io::Error;
|
||||||
type Future = ConnectAsyncExt<T, U>;
|
type Future = Either<ConnectAsyncExt<T, U>, Ready<Result<Self::Response, Self::Error>>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, stream: Connection<T, U>) -> Self::Future {
|
fn call(&mut self, stream: Connection<T, U>) -> Self::Future {
|
||||||
trace!("SSL Handshake start for: {:?}", stream.host());
|
trace!("SSL Handshake start for: {:?}", stream.host());
|
||||||
let (io, stream) = stream.replace(());
|
let (io, stream) = stream.replace(());
|
||||||
ConnectAsyncExt {
|
let host = stream.host().to_string();
|
||||||
fut: SslConnectorExt::connect_async(&self.connector, stream.host(), io),
|
|
||||||
|
match self.connector.configure() {
|
||||||
|
Err(e) => Either::Right(err(io::Error::new(io::ErrorKind::Other, e))),
|
||||||
|
Ok(config) => Either::Left(ConnectAsyncExt {
|
||||||
|
fut: async move { tokio_openssl::connect(config, &host, io).await }
|
||||||
|
.boxed_local(),
|
||||||
stream: Some(stream),
|
stream: Some(stream),
|
||||||
|
_t: PhantomData,
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ConnectAsyncExt<T, U> {
|
pub struct ConnectAsyncExt<T, U> {
|
||||||
fut: ConnectAsync<U>,
|
fut: LocalBoxFuture<'static, Result<SslStream<U>, HandshakeError<U>>>,
|
||||||
stream: Option<Connection<T, ()>>,
|
stream: Option<Connection<T, ()>>,
|
||||||
|
_t: PhantomData<U>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address, U> Future for ConnectAsyncExt<T, U>
|
impl<T: Address + Unpin, U> Future for ConnectAsyncExt<T, U>
|
||||||
where
|
where
|
||||||
U: AsyncRead + AsyncWrite + fmt::Debug,
|
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
|
||||||
{
|
{
|
||||||
type Item = Connection<T, SslStream<U>>;
|
type Output = Result<Connection<T, SslStream<U>>, io::Error>;
|
||||||
type Error = HandshakeError<U>;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||||
match self.fut.poll().map_err(|e| {
|
let this = self.get_mut();
|
||||||
trace!("SSL Handshake error: {:?}", e);
|
|
||||||
e
|
match Pin::new(&mut this.fut).poll(cx) {
|
||||||
})? {
|
Poll::Ready(Ok(stream)) => {
|
||||||
Async::Ready(stream) => {
|
let s = this.stream.take().unwrap();
|
||||||
let s = self.stream.take().unwrap();
|
|
||||||
trace!("SSL Handshake success: {:?}", s.host());
|
trace!("SSL Handshake success: {:?}", s.host());
|
||||||
Ok(Async::Ready(s.replace(stream).1))
|
Poll::Ready(Ok(s.replace(stream).1))
|
||||||
}
|
}
|
||||||
Async::NotReady => Ok(Async::NotReady),
|
Poll::Ready(Err(e)) => {
|
||||||
|
trace!("SSL Handshake error: {:?}", e);
|
||||||
|
Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, format!("{}", e))))
|
||||||
|
}
|
||||||
|
Poll::Pending => Poll::Pending,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -145,7 +158,7 @@ pub struct OpensslConnectServiceFactory<T> {
|
|||||||
openssl: OpensslConnector<T, TcpStream>,
|
openssl: OpensslConnector<T, TcpStream>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> OpensslConnectServiceFactory<T> {
|
impl<T: Unpin> OpensslConnectServiceFactory<T> {
|
||||||
/// Construct new OpensslConnectService factory
|
/// Construct new OpensslConnectService factory
|
||||||
pub fn new(connector: SslConnector) -> Self {
|
pub fn new(connector: SslConnector) -> Self {
|
||||||
OpensslConnectServiceFactory {
|
OpensslConnectServiceFactory {
|
||||||
@ -183,14 +196,14 @@ impl<T> Clone for OpensslConnectServiceFactory<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> NewService for OpensslConnectServiceFactory<T> {
|
impl<T: Address + Unpin + 'static> ServiceFactory for OpensslConnectServiceFactory<T> {
|
||||||
type Request = Connect<T>;
|
type Request = Connect<T>;
|
||||||
type Response = SslStream<TcpStream>;
|
type Response = SslStream<TcpStream>;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Config = ();
|
type Config = ();
|
||||||
type Service = OpensslConnectService<T>;
|
type Service = OpensslConnectService<T>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, _: &()) -> Self::Future {
|
fn new_service(&self, _: &()) -> Self::Future {
|
||||||
ok(self.service())
|
ok(self.service())
|
||||||
@ -203,14 +216,14 @@ pub struct OpensslConnectService<T> {
|
|||||||
openssl: OpensslConnectorService<T, TcpStream>,
|
openssl: OpensslConnectorService<T, TcpStream>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> Service for OpensslConnectService<T> {
|
impl<T: Address + Unpin + 'static> Service for OpensslConnectService<T> {
|
||||||
type Request = Connect<T>;
|
type Request = Connect<T>;
|
||||||
type Response = SslStream<TcpStream>;
|
type Response = SslStream<TcpStream>;
|
||||||
type Error = ConnectError;
|
type Error = ConnectError;
|
||||||
type Future = OpensslConnectServiceResponse<T>;
|
type Future = OpensslConnectServiceResponse<T>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Connect<T>) -> Self::Future {
|
fn call(&mut self, req: Connect<T>) -> Self::Future {
|
||||||
@ -222,30 +235,36 @@ impl<T: Address> Service for OpensslConnectService<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct OpensslConnectServiceResponse<T: Address> {
|
pub struct OpensslConnectServiceResponse<T: Address + Unpin + 'static> {
|
||||||
fut1: Option<<ConnectService<T> as Service>::Future>,
|
fut1: Option<<ConnectService<T> as Service>::Future>,
|
||||||
fut2: Option<<OpensslConnectorService<T, TcpStream> as Service>::Future>,
|
fut2: Option<<OpensslConnectorService<T, TcpStream> as Service>::Future>,
|
||||||
openssl: OpensslConnectorService<T, TcpStream>,
|
openssl: OpensslConnectorService<T, TcpStream>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address> Future for OpensslConnectServiceResponse<T> {
|
impl<T: Address + Unpin> Future for OpensslConnectServiceResponse<T> {
|
||||||
type Item = SslStream<TcpStream>;
|
type Output = Result<SslStream<TcpStream>, ConnectError>;
|
||||||
type Error = ConnectError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||||
if let Some(ref mut fut) = self.fut1 {
|
if let Some(ref mut fut) = self.fut1 {
|
||||||
let res = try_ready!(fut.poll());
|
match futures::ready!(Pin::new(fut).poll(cx)) {
|
||||||
|
Ok(res) => {
|
||||||
let _ = self.fut1.take();
|
let _ = self.fut1.take();
|
||||||
self.fut2 = Some(self.openssl.call(res));
|
self.fut2 = Some(self.openssl.call(res));
|
||||||
}
|
}
|
||||||
|
Err(e) => return Poll::Ready(Err(e.into())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(ref mut fut) = self.fut2 {
|
if let Some(ref mut fut) = self.fut2 {
|
||||||
let connect = try_ready!(fut
|
match futures::ready!(Pin::new(fut).poll(cx)) {
|
||||||
.poll()
|
Ok(connect) => Poll::Ready(Ok(connect.into_parts().0)),
|
||||||
.map_err(|e| ConnectError::Io(io::Error::new(io::ErrorKind::Other, e))));
|
Err(e) => Poll::Ready(Err(ConnectError::Io(io::Error::new(
|
||||||
Ok(Async::Ready(connect.into_parts().0))
|
io::ErrorKind::Other,
|
||||||
|
e,
|
||||||
|
)))),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite};
|
use actix_codec::{AsyncRead, AsyncWrite};
|
||||||
use actix_service::{NewService, Service};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
|
use futures::future::{ok, Ready};
|
||||||
use std::sync::Arc;
|
|
||||||
use tokio_rustls::{client::TlsStream, rustls::ClientConfig, Connect, TlsConnector};
|
use tokio_rustls::{client::TlsStream, rustls::ClientConfig, Connect, TlsConnector};
|
||||||
use webpki::DNSNameRef;
|
use webpki::DNSNameRef;
|
||||||
|
|
||||||
@ -27,8 +30,8 @@ impl<T, U> RustlsConnector<T, U> {
|
|||||||
|
|
||||||
impl<T, U> RustlsConnector<T, U>
|
impl<T, U> RustlsConnector<T, U>
|
||||||
where
|
where
|
||||||
T: Address,
|
T: Address + Unpin,
|
||||||
U: AsyncRead + AsyncWrite + fmt::Debug,
|
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug,
|
||||||
{
|
{
|
||||||
pub fn service(
|
pub fn service(
|
||||||
connector: Arc<ClientConfig>,
|
connector: Arc<ClientConfig>,
|
||||||
@ -53,9 +56,9 @@ impl<T, U> Clone for RustlsConnector<T, U> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address, U> NewService for RustlsConnector<T, U>
|
impl<T: Address + Unpin, U> ServiceFactory for RustlsConnector<T, U>
|
||||||
where
|
where
|
||||||
U: AsyncRead + AsyncWrite + fmt::Debug,
|
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug,
|
||||||
{
|
{
|
||||||
type Request = Connection<T, U>;
|
type Request = Connection<T, U>;
|
||||||
type Response = Connection<T, TlsStream<U>>;
|
type Response = Connection<T, TlsStream<U>>;
|
||||||
@ -63,7 +66,7 @@ where
|
|||||||
type Config = ();
|
type Config = ();
|
||||||
type Service = RustlsConnectorService<T, U>;
|
type Service = RustlsConnectorService<T, U>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, _: &()) -> Self::Future {
|
fn new_service(&self, _: &()) -> Self::Future {
|
||||||
ok(RustlsConnectorService {
|
ok(RustlsConnectorService {
|
||||||
@ -78,17 +81,17 @@ pub struct RustlsConnectorService<T, U> {
|
|||||||
_t: PhantomData<(T, U)>,
|
_t: PhantomData<(T, U)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address, U> Service for RustlsConnectorService<T, U>
|
impl<T: Address + Unpin, U> Service for RustlsConnectorService<T, U>
|
||||||
where
|
where
|
||||||
U: AsyncRead + AsyncWrite + fmt::Debug,
|
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug,
|
||||||
{
|
{
|
||||||
type Request = Connection<T, U>;
|
type Request = Connection<T, U>;
|
||||||
type Response = Connection<T, TlsStream<U>>;
|
type Response = Connection<T, TlsStream<U>>;
|
||||||
type Error = std::io::Error;
|
type Error = std::io::Error;
|
||||||
type Future = ConnectAsyncExt<T, U>;
|
type Future = ConnectAsyncExt<T, U>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, stream: Connection<T, U>) -> Self::Future {
|
fn call(&mut self, stream: Connection<T, U>) -> Self::Future {
|
||||||
@ -108,24 +111,20 @@ pub struct ConnectAsyncExt<T, U> {
|
|||||||
stream: Option<Connection<T, ()>>,
|
stream: Option<Connection<T, ()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Address, U> Future for ConnectAsyncExt<T, U>
|
impl<T: Address + Unpin, U> Future for ConnectAsyncExt<T, U>
|
||||||
where
|
where
|
||||||
U: AsyncRead + AsyncWrite + fmt::Debug,
|
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug,
|
||||||
{
|
{
|
||||||
type Item = Connection<T, TlsStream<U>>;
|
type Output = Result<Connection<T, TlsStream<U>>, std::io::Error>;
|
||||||
type Error = std::io::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||||
match self.fut.poll().map_err(|e| {
|
let this = self.get_mut();
|
||||||
trace!("SSL Handshake error: {:?}", e);
|
Poll::Ready(
|
||||||
e
|
futures::ready!(Pin::new(&mut this.fut).poll(cx)).map(|stream| {
|
||||||
})? {
|
let s = this.stream.take().unwrap();
|
||||||
Async::Ready(stream) => {
|
|
||||||
let s = self.stream.take().unwrap();
|
|
||||||
trace!("SSL Handshake success: {:?}", s.host());
|
trace!("SSL Handshake success: {:?}", s.host());
|
||||||
Ok(Async::Ready(s.replace(stream).1))
|
s.replace(stream).1
|
||||||
}
|
}),
|
||||||
Async::NotReady => Ok(Async::NotReady),
|
)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,135 +1,137 @@
|
|||||||
|
use std::io;
|
||||||
|
|
||||||
use actix_codec::{BytesCodec, Framed};
|
use actix_codec::{BytesCodec, Framed};
|
||||||
use actix_server_config::Io;
|
use actix_server_config::Io;
|
||||||
use actix_service::{service_fn, NewService, Service};
|
use actix_service::{service_fn, Service, ServiceFactory};
|
||||||
use actix_testing::{self as test, TestServer};
|
use actix_testing::{self as test, TestServer};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures::{future::lazy, Future, Sink};
|
use futures::SinkExt;
|
||||||
use http::{HttpTryFrom, Uri};
|
|
||||||
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
||||||
|
|
||||||
use actix_connect::{default_connector, Connect};
|
use actix_connect::Connect;
|
||||||
|
|
||||||
#[cfg(feature = "ssl")]
|
#[cfg(feature = "openssl")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_string() {
|
fn test_string() {
|
||||||
let srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
service_fn(|io: Io<tokio_net::tcp::TcpStream>| {
|
||||||
Framed::new(io.into_parts().0, BytesCodec)
|
async {
|
||||||
.send(Bytes::from_static(b"test"))
|
let mut framed = Framed::new(io.into_parts().0, BytesCodec);
|
||||||
.then(|_| Ok::<_, ()>(()))
|
framed.send(Bytes::from_static(b"test")).await?;
|
||||||
|
Ok::<_, io::Error>(())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut conn = default_connector();
|
let mut conn = actix_connect::default_connector();
|
||||||
let addr = format!("localhost:{}", srv.port());
|
let addr = format!("localhost:{}", srv.port());
|
||||||
let con = test::call_service(&mut conn, addr.into());
|
let con = test::call_service(&mut conn, addr.into());
|
||||||
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "rust-tls")]
|
#[cfg(feature = "rustls")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rustls_string() {
|
fn test_rustls_string() {
|
||||||
let srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
service_fn(|io: Io<tokio_net::tcp::TcpStream>| {
|
||||||
Framed::new(io.into_parts().0, BytesCodec)
|
async {
|
||||||
.send(Bytes::from_static(b"test"))
|
let mut framed = Framed::new(io.into_parts().0, BytesCodec);
|
||||||
.then(|_| Ok::<_, ()>(()))
|
framed.send(Bytes::from_static(b"test")).await?;
|
||||||
|
Ok::<_, io::Error>(())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut conn = default_connector();
|
let mut conn = actix_connect::default_connector();
|
||||||
let addr = format!("localhost:{}", srv.port());
|
let addr = format!("localhost:{}", srv.port());
|
||||||
let con = test::call_service(&mut conn, addr.into());
|
let con = test::call_service(&mut conn, addr.into());
|
||||||
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_static_str() {
|
async fn test_static_str() {
|
||||||
let srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
service_fn(|io: Io<tokio_net::tcp::TcpStream>| {
|
||||||
Framed::new(io.into_parts().0, BytesCodec)
|
async {
|
||||||
.send(Bytes::from_static(b"test"))
|
let mut framed = Framed::new(io.into_parts().0, BytesCodec);
|
||||||
.then(|_| Ok::<_, ()>(()))
|
framed.send(Bytes::from_static(b"test")).await?;
|
||||||
|
Ok::<_, io::Error>(())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let resolver = test::block_on(lazy(
|
let resolver = actix_connect::start_default_resolver();
|
||||||
|| Ok::<_, ()>(actix_connect::start_default_resolver()),
|
let mut conn = actix_connect::new_connector(resolver.clone());
|
||||||
))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut conn = test::block_on(lazy(|| {
|
let con = conn.call(Connect::with("10", srv.addr())).await.unwrap();
|
||||||
Ok::<_, ()>(actix_connect::new_connector(resolver.clone()))
|
|
||||||
}))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let con = test::block_on(conn.call(Connect::with("10", srv.addr()))).unwrap();
|
|
||||||
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
|
|
||||||
let connect = Connect::new(srv.host().to_owned());
|
let connect = Connect::new(srv.host().to_owned());
|
||||||
let mut conn =
|
let mut conn = actix_connect::new_connector(resolver);
|
||||||
test::block_on(lazy(|| Ok::<_, ()>(actix_connect::new_connector(resolver)))).unwrap();
|
let con = conn.call(connect).await;
|
||||||
let con = test::block_on(conn.call(connect));
|
|
||||||
assert!(con.is_err());
|
assert!(con.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_new_service() {
|
fn test_new_service() {
|
||||||
let srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
service_fn(|io: Io<tokio_net::tcp::TcpStream>| {
|
||||||
Framed::new(io.into_parts().0, BytesCodec)
|
async {
|
||||||
.send(Bytes::from_static(b"test"))
|
let mut framed = Framed::new(io.into_parts().0, BytesCodec);
|
||||||
.then(|_| Ok::<_, ()>(()))
|
framed.send(Bytes::from_static(b"test")).await?;
|
||||||
|
Ok::<_, io::Error>(())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let resolver = test::block_on(lazy(|| {
|
let resolver = test::block_on(async {
|
||||||
Ok::<_, ()>(actix_connect::start_resolver(
|
actix_connect::start_resolver(ResolverConfig::default(), ResolverOpts::default())
|
||||||
ResolverConfig::default(),
|
});
|
||||||
ResolverOpts::default(),
|
let factory = test::block_on(async { actix_connect::new_connector_factory(resolver) });
|
||||||
))
|
|
||||||
}))
|
|
||||||
.unwrap();
|
|
||||||
let factory = test::block_on(lazy(|| {
|
|
||||||
Ok::<_, ()>(actix_connect::new_connector_factory(resolver))
|
|
||||||
}))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut conn = test::block_on(factory.new_service(&())).unwrap();
|
let mut conn = test::block_on(factory.new_service(&())).unwrap();
|
||||||
let con = test::block_on(conn.call(Connect::with("10", srv.addr()))).unwrap();
|
let con = test::block_on(conn.call(Connect::with("10", srv.addr()))).unwrap();
|
||||||
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "ssl")]
|
#[cfg(feature = "openssl")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_uri() {
|
fn test_uri() {
|
||||||
|
use http::HttpTryFrom;
|
||||||
|
|
||||||
let srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
service_fn(|io: Io<tokio_net::tcp::TcpStream>| {
|
||||||
Framed::new(io.into_parts().0, BytesCodec)
|
async {
|
||||||
.send(Bytes::from_static(b"test"))
|
let mut framed = Framed::new(io.into_parts().0, BytesCodec);
|
||||||
.then(|_| Ok::<_, ()>(()))
|
framed.send(Bytes::from_static(b"test")).await?;
|
||||||
|
Ok::<_, io::Error>(())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut conn = default_connector();
|
let mut conn = actix_connect::default_connector();
|
||||||
let addr = Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
|
let addr = http::Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
|
||||||
let con = test::call_service(&mut conn, addr.into());
|
let con = test::call_service(&mut conn, addr.into());
|
||||||
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "rust-tls")]
|
#[cfg(feature = "rustls")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rustls_uri() {
|
fn test_rustls_uri() {
|
||||||
|
use http::HttpTryFrom;
|
||||||
|
|
||||||
let srv = TestServer::with(|| {
|
let srv = TestServer::with(|| {
|
||||||
service_fn(|io: Io<tokio_tcp::TcpStream>| {
|
service_fn(|io: Io<tokio_net::tcp::TcpStream>| {
|
||||||
Framed::new(io.into_parts().0, BytesCodec)
|
async {
|
||||||
.send(Bytes::from_static(b"test"))
|
let mut framed = Framed::new(io.into_parts().0, BytesCodec);
|
||||||
.then(|_| Ok::<_, ()>(()))
|
framed.send(Bytes::from_static(b"test")).await?;
|
||||||
|
Ok::<_, io::Error>(())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut conn = default_connector();
|
let mut conn = actix_connect::default_connector();
|
||||||
let addr = Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
|
let addr = http::Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap();
|
||||||
let con = test::call_service(&mut conn, addr.into());
|
let con = test::call_service(&mut conn, addr.into());
|
||||||
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
assert_eq!(con.peer_addr().unwrap(), srv.addr());
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-ioframe"
|
name = "actix-ioframe"
|
||||||
version = "0.1.1"
|
version = "0.3.0-alpha.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix framed service"
|
description = "Actix framed service"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -18,18 +18,20 @@ name = "actix_ioframe"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-service = "0.4.1"
|
actix-service = "1.0.0-alpha.1"
|
||||||
actix-codec = "0.1.2"
|
actix-codec = "0.2.0-alpha.1"
|
||||||
|
actix-utils = "0.5.0-alpha.1"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
either = "1.5.2"
|
either = "1.5.2"
|
||||||
futures = "0.1.25"
|
futures = "0.3.1"
|
||||||
tokio-current-thread = "0.1.4"
|
pin-project = "0.4.5"
|
||||||
|
tokio-executor = "=0.2.0-alpha.6"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "0.2.2"
|
actix-rt = "1.0.0-alpha.1"
|
||||||
actix-connect = "0.3.0"
|
actix-connect = "1.0.0-alpha.1"
|
||||||
actix-testing = "0.2.0"
|
actix-testing = "0.3.0-alpha.1"
|
||||||
actix-server-config = "0.2.0"
|
actix-server-config = "0.3.0-alpha.1"
|
||||||
tokio-tcp = "0.1"
|
tokio-net = "=0.2.0-alpha.6"
|
||||||
tokio-timer = "0.2"
|
tokio-timer = "=0.3.0-alpha.6"
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
||||||
use futures::unsync::mpsc;
|
use actix_utils::mpsc;
|
||||||
|
use futures::Stream;
|
||||||
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use crate::dispatcher::FramedMessage;
|
use crate::dispatcher::FramedMessage;
|
||||||
use crate::sink::Sink;
|
use crate::sink::Sink;
|
||||||
@ -13,7 +17,7 @@ pub struct Connect<Io, St = (), Codec = ()> {
|
|||||||
|
|
||||||
impl<Io> Connect<Io>
|
impl<Io> Connect<Io>
|
||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite + Unpin,
|
||||||
{
|
{
|
||||||
pub(crate) fn new(io: Io) -> Self {
|
pub(crate) fn new(io: Io) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -24,9 +28,9 @@ where
|
|||||||
|
|
||||||
pub fn codec<Codec>(self, codec: Codec) -> ConnectResult<Io, (), Codec>
|
pub fn codec<Codec>(self, codec: Codec) -> ConnectResult<Io, (), Codec>
|
||||||
where
|
where
|
||||||
Codec: Encoder + Decoder,
|
Codec: Encoder + Decoder + Unpin,
|
||||||
{
|
{
|
||||||
let (tx, rx) = mpsc::unbounded();
|
let (tx, rx) = mpsc::channel();
|
||||||
let sink = Sink::new(tx);
|
let sink = Sink::new(tx);
|
||||||
|
|
||||||
ConnectResult {
|
ConnectResult {
|
||||||
@ -38,10 +42,12 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pin_project]
|
||||||
pub struct ConnectResult<Io, St, Codec: Encoder + Decoder> {
|
pub struct ConnectResult<Io, St, Codec: Encoder + Decoder> {
|
||||||
pub(crate) state: St,
|
pub(crate) state: St,
|
||||||
|
#[pin]
|
||||||
pub(crate) framed: Framed<Io, Codec>,
|
pub(crate) framed: Framed<Io, Codec>,
|
||||||
pub(crate) rx: mpsc::UnboundedReceiver<FramedMessage<<Codec as Encoder>::Item>>,
|
pub(crate) rx: mpsc::Receiver<FramedMessage<<Codec as Encoder>::Item>>,
|
||||||
pub(crate) sink: Sink<<Codec as Encoder>::Item>,
|
pub(crate) sink: Sink<<Codec as Encoder>::Item>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,39 +78,41 @@ impl<Io, St, Codec: Encoder + Decoder> ConnectResult<Io, St, Codec> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Io, St, Codec> futures::Stream for ConnectResult<Io, St, Codec>
|
impl<Io, St, Codec> Stream for ConnectResult<Io, St, Codec>
|
||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite + Unpin,
|
||||||
Codec: Encoder + Decoder,
|
Codec: Encoder + Decoder + Unpin,
|
||||||
{
|
{
|
||||||
type Item = <Codec as Decoder>::Item;
|
type Item = Result<<Codec as Decoder>::Item, <Codec as Decoder>::Error>;
|
||||||
type Error = <Codec as Decoder>::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
||||||
self.framed.poll()
|
self.project().framed.poll_next(cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Io, St, Codec> futures::Sink for ConnectResult<Io, St, Codec>
|
impl<Io, St, Codec> futures::Sink<<Codec as Encoder>::Item> for ConnectResult<Io, St, Codec>
|
||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite + Unpin,
|
||||||
Codec: Encoder + Decoder,
|
Codec: Encoder + Decoder + Unpin,
|
||||||
{
|
{
|
||||||
type SinkItem = <Codec as Encoder>::Item;
|
type Error = <Codec as Encoder>::Error;
|
||||||
type SinkError = <Codec as Encoder>::Error;
|
|
||||||
|
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
|
self.project().framed.poll_ready(cx)
|
||||||
|
}
|
||||||
|
|
||||||
fn start_send(
|
fn start_send(
|
||||||
&mut self,
|
self: Pin<&mut Self>,
|
||||||
item: Self::SinkItem,
|
item: <Codec as Encoder>::Item,
|
||||||
) -> futures::StartSend<Self::SinkItem, Self::SinkError> {
|
) -> Result<(), Self::Error> {
|
||||||
self.framed.start_send(item)
|
self.project().framed.start_send(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_complete(&mut self) -> futures::Poll<(), Self::SinkError> {
|
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.framed.poll_complete()
|
self.project().framed.poll_flush(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn close(&mut self) -> futures::Poll<(), Self::SinkError> {
|
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.framed.close()
|
self.project().framed.poll_close(cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
//! Framed dispatcher service and related utilities
|
//! Framed dispatcher service and related utilities
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
use std::future::Future;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
use std::pin::Pin;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
||||||
use actix_service::{IntoService, Service};
|
use actix_service::{IntoService, Service};
|
||||||
use futures::task::AtomicTask;
|
use actix_utils::task::LocalWaker;
|
||||||
use futures::unsync::{mpsc, oneshot};
|
use actix_utils::{mpsc, oneshot};
|
||||||
use futures::{Async, Future, Poll, Sink as FutureSink, Stream};
|
use futures::future::ready;
|
||||||
|
use futures::{FutureExt, Sink as FutureSink, Stream};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
|
||||||
use crate::cell::Cell;
|
use crate::cell::Cell;
|
||||||
@ -27,13 +31,14 @@ pub(crate) enum FramedMessage<T> {
|
|||||||
|
|
||||||
/// FramedTransport - is a future that reads frames from Framed object
|
/// FramedTransport - is a future that reads frames from Framed object
|
||||||
/// and pass then to the service.
|
/// and pass then to the service.
|
||||||
|
#[pin_project::pin_project]
|
||||||
pub(crate) struct FramedDispatcher<St, S, T, U>
|
pub(crate) struct FramedDispatcher<St, S, T, U>
|
||||||
where
|
where
|
||||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
U: Encoder + Decoder,
|
U: Encoder + Decoder + Unpin,
|
||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
@ -42,7 +47,7 @@ where
|
|||||||
state: State<St>,
|
state: State<St>,
|
||||||
dispatch_state: FramedState<S, U>,
|
dispatch_state: FramedState<S, U>,
|
||||||
framed: Framed<T, U>,
|
framed: Framed<T, U>,
|
||||||
rx: Option<mpsc::UnboundedReceiver<FramedMessage<<U as Encoder>::Item>>>,
|
rx: Option<mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>>,
|
||||||
inner: Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
|
inner: Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
|
||||||
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
||||||
}
|
}
|
||||||
@ -52,8 +57,8 @@ where
|
|||||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
U: Decoder + Encoder,
|
U: Decoder + Encoder + Unpin,
|
||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
@ -61,7 +66,7 @@ where
|
|||||||
framed: Framed<T, U>,
|
framed: Framed<T, U>,
|
||||||
state: State<St>,
|
state: State<St>,
|
||||||
service: F,
|
service: F,
|
||||||
rx: mpsc::UnboundedReceiver<FramedMessage<<U as Encoder>::Item>>,
|
rx: mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>,
|
||||||
sink: Sink<<U as Encoder>::Item>,
|
sink: Sink<<U as Encoder>::Item>,
|
||||||
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
@ -75,13 +80,13 @@ where
|
|||||||
dispatch_state: FramedState::Processing,
|
dispatch_state: FramedState::Processing,
|
||||||
inner: Cell::new(FramedDispatcherInner {
|
inner: Cell::new(FramedDispatcherInner {
|
||||||
buf: VecDeque::new(),
|
buf: VecDeque::new(),
|
||||||
task: AtomicTask::new(),
|
task: LocalWaker::new(),
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum FramedState<S: Service, U: Encoder + Decoder> {
|
enum FramedState<S: Service, U: Encoder + Decoder + Unpin> {
|
||||||
Processing,
|
Processing,
|
||||||
Error(ServiceError<S::Error, U>),
|
Error(ServiceError<S::Error, U>),
|
||||||
FramedError(ServiceError<S::Error, U>),
|
FramedError(ServiceError<S::Error, U>),
|
||||||
@ -89,7 +94,7 @@ enum FramedState<S: Service, U: Encoder + Decoder> {
|
|||||||
Stopping,
|
Stopping,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Service, U: Encoder + Decoder> FramedState<S, U> {
|
impl<S: Service, U: Encoder + Decoder + Unpin> FramedState<S, U> {
|
||||||
fn stop(&mut self, tx: Option<oneshot::Sender<()>>) {
|
fn stop(&mut self, tx: Option<oneshot::Sender<()>>) {
|
||||||
match self {
|
match self {
|
||||||
FramedState::FlushAndStop(ref mut vec) => {
|
FramedState::FlushAndStop(ref mut vec) => {
|
||||||
@ -115,123 +120,252 @@ impl<S: Service, U: Encoder + Decoder> FramedState<S, U> {
|
|||||||
|
|
||||||
struct FramedDispatcherInner<I, E> {
|
struct FramedDispatcherInner<I, E> {
|
||||||
buf: VecDeque<Result<I, E>>,
|
buf: VecDeque<Result<I, E>>,
|
||||||
task: AtomicTask,
|
task: LocalWaker,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<St, S, T, U> FramedDispatcher<St, S, T, U>
|
impl<St, S, T, U> Future for FramedDispatcher<St, S, T, U>
|
||||||
where
|
where
|
||||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
U: Decoder + Encoder,
|
U: Decoder + Encoder + Unpin,
|
||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
fn disconnect(&mut self, error: bool) {
|
type Output = Result<(), ServiceError<S::Error, U>>;
|
||||||
if let Some(ref disconnect) = self.disconnect {
|
|
||||||
(&*disconnect)(&mut *self.state.get_mut(), error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn poll_read(&mut self) -> bool {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||||
|
unsafe { self.inner.get_ref().task.register(cx.waker()) };
|
||||||
|
|
||||||
|
let this = self.project();
|
||||||
|
poll(
|
||||||
|
cx,
|
||||||
|
this.service,
|
||||||
|
this.state,
|
||||||
|
this.sink,
|
||||||
|
this.framed,
|
||||||
|
this.dispatch_state,
|
||||||
|
this.rx,
|
||||||
|
this.inner,
|
||||||
|
this.disconnect,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll<St, S, T, U>(
|
||||||
|
cx: &mut Context,
|
||||||
|
srv: &mut S,
|
||||||
|
state: &mut State<St>,
|
||||||
|
sink: &mut Sink<<U as Encoder>::Item>,
|
||||||
|
framed: &mut Framed<T, U>,
|
||||||
|
dispatch_state: &mut FramedState<S, U>,
|
||||||
|
rx: &mut Option<mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>>,
|
||||||
|
inner: &mut Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
|
||||||
|
disconnect: &mut Option<Rc<dyn Fn(&mut St, bool)>>,
|
||||||
|
) -> Poll<Result<(), ServiceError<S::Error, U>>>
|
||||||
|
where
|
||||||
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||||
|
S::Error: 'static,
|
||||||
|
S::Future: 'static,
|
||||||
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
|
U: Decoder + Encoder + Unpin,
|
||||||
|
<U as Encoder>::Item: 'static,
|
||||||
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
match mem::replace(dispatch_state, FramedState::Processing) {
|
||||||
|
FramedState::Processing => {
|
||||||
|
if poll_read(cx, srv, state, sink, framed, dispatch_state, inner)
|
||||||
|
|| poll_write(cx, framed, dispatch_state, rx, inner)
|
||||||
|
{
|
||||||
|
poll(
|
||||||
|
cx,
|
||||||
|
srv,
|
||||||
|
state,
|
||||||
|
sink,
|
||||||
|
framed,
|
||||||
|
dispatch_state,
|
||||||
|
rx,
|
||||||
|
inner,
|
||||||
|
disconnect,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Poll::Pending
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FramedState::Error(err) => {
|
||||||
|
if framed.is_write_buf_empty()
|
||||||
|
|| (poll_write(cx, framed, dispatch_state, rx, inner)
|
||||||
|
|| framed.is_write_buf_empty())
|
||||||
|
{
|
||||||
|
if let Some(ref disconnect) = disconnect {
|
||||||
|
(&*disconnect)(&mut *state.get_mut(), true);
|
||||||
|
}
|
||||||
|
Poll::Ready(Err(err))
|
||||||
|
} else {
|
||||||
|
*dispatch_state = FramedState::Error(err);
|
||||||
|
Poll::Pending
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FramedState::FlushAndStop(mut vec) => {
|
||||||
|
if !framed.is_write_buf_empty() {
|
||||||
|
match Pin::new(framed).poll_flush(cx) {
|
||||||
|
Poll::Ready(Err(err)) => {
|
||||||
|
debug!("Error sending data: {:?}", err);
|
||||||
|
}
|
||||||
|
Poll::Pending => {
|
||||||
|
*dispatch_state = FramedState::FlushAndStop(vec);
|
||||||
|
return Poll::Pending;
|
||||||
|
}
|
||||||
|
Poll::Ready(_) => (),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
for tx in vec.drain(..) {
|
||||||
|
let _ = tx.send(());
|
||||||
|
}
|
||||||
|
if let Some(ref disconnect) = disconnect {
|
||||||
|
(&*disconnect)(&mut *state.get_mut(), false);
|
||||||
|
}
|
||||||
|
Poll::Ready(Ok(()))
|
||||||
|
}
|
||||||
|
FramedState::FramedError(err) => {
|
||||||
|
if let Some(ref disconnect) = disconnect {
|
||||||
|
(&*disconnect)(&mut *state.get_mut(), true);
|
||||||
|
}
|
||||||
|
Poll::Ready(Err(err))
|
||||||
|
}
|
||||||
|
FramedState::Stopping => {
|
||||||
|
if let Some(ref disconnect) = disconnect {
|
||||||
|
(&*disconnect)(&mut *state.get_mut(), false);
|
||||||
|
}
|
||||||
|
Poll::Ready(Ok(()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_read<St, S, T, U>(
|
||||||
|
cx: &mut Context,
|
||||||
|
srv: &mut S,
|
||||||
|
state: &mut State<St>,
|
||||||
|
sink: &mut Sink<<U as Encoder>::Item>,
|
||||||
|
framed: &mut Framed<T, U>,
|
||||||
|
dispatch_state: &mut FramedState<S, U>,
|
||||||
|
inner: &mut Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
|
||||||
|
) -> bool
|
||||||
|
where
|
||||||
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||||
|
S::Error: 'static,
|
||||||
|
S::Future: 'static,
|
||||||
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
|
U: Decoder + Encoder + Unpin,
|
||||||
|
<U as Encoder>::Item: 'static,
|
||||||
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
|
{
|
||||||
loop {
|
loop {
|
||||||
match self.service.poll_ready() {
|
match srv.poll_ready(cx) {
|
||||||
Ok(Async::Ready(_)) => {
|
Poll::Ready(Ok(_)) => {
|
||||||
let item = match self.framed.poll() {
|
let item = match framed.next_item(cx) {
|
||||||
Ok(Async::Ready(Some(el))) => el,
|
Poll::Ready(Some(Ok(el))) => el,
|
||||||
Err(err) => {
|
Poll::Ready(Some(Err(err))) => {
|
||||||
self.dispatch_state =
|
*dispatch_state = FramedState::FramedError(ServiceError::Decoder(err));
|
||||||
FramedState::FramedError(ServiceError::Decoder(err));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Ok(Async::NotReady) => return false,
|
Poll::Pending => return false,
|
||||||
Ok(Async::Ready(None)) => {
|
Poll::Ready(None) => {
|
||||||
log::trace!("Client disconnected");
|
log::trace!("Client disconnected");
|
||||||
self.dispatch_state = FramedState::Stopping;
|
*dispatch_state = FramedState::Stopping;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut cell = self.inner.clone();
|
let mut cell = inner.clone();
|
||||||
tokio_current_thread::spawn(
|
tokio_executor::current_thread::spawn(
|
||||||
self.service
|
srv.call(Item::new(state.clone(), sink.clone(), item))
|
||||||
.call(Item::new(self.state.clone(), self.sink.clone(), item))
|
|
||||||
.then(move |item| {
|
.then(move |item| {
|
||||||
let item = match item {
|
let item = match item {
|
||||||
Ok(Some(item)) => Ok(item),
|
Ok(Some(item)) => Ok(item),
|
||||||
Ok(None) => return Ok(()),
|
Ok(None) => return ready(()),
|
||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
let inner = cell.get_mut();
|
let inner = cell.get_mut();
|
||||||
inner.buf.push_back(item);
|
inner.buf.push_back(item);
|
||||||
inner.task.notify();
|
inner.task.wake();
|
||||||
}
|
}
|
||||||
Ok(())
|
ready(())
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Ok(Async::NotReady) => return false,
|
Poll::Pending => return false,
|
||||||
Err(err) => {
|
Poll::Ready(Err(err)) => {
|
||||||
self.dispatch_state = FramedState::Error(ServiceError::Service(err));
|
*dispatch_state = FramedState::Error(ServiceError::Service(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// write to framed object
|
/// write to framed object
|
||||||
fn poll_write(&mut self) -> bool {
|
fn poll_write<St, S, T, U>(
|
||||||
let inner = unsafe { self.inner.get_mut() };
|
cx: &mut Context,
|
||||||
let mut rx_done = self.rx.is_none();
|
framed: &mut Framed<T, U>,
|
||||||
|
dispatch_state: &mut FramedState<S, U>,
|
||||||
|
rx: &mut Option<mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>>,
|
||||||
|
inner: &mut Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
|
||||||
|
) -> bool
|
||||||
|
where
|
||||||
|
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||||
|
S::Error: 'static,
|
||||||
|
S::Future: 'static,
|
||||||
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
|
U: Decoder + Encoder + Unpin,
|
||||||
|
<U as Encoder>::Item: 'static,
|
||||||
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
let inner = unsafe { inner.get_mut() };
|
||||||
|
let mut rx_done = rx.is_none();
|
||||||
let mut buf_empty = inner.buf.is_empty();
|
let mut buf_empty = inner.buf.is_empty();
|
||||||
loop {
|
loop {
|
||||||
while !self.framed.is_write_buf_full() {
|
while !framed.is_write_buf_full() {
|
||||||
if !buf_empty {
|
if !buf_empty {
|
||||||
match inner.buf.pop_front().unwrap() {
|
match inner.buf.pop_front().unwrap() {
|
||||||
Ok(msg) => {
|
Ok(msg) => {
|
||||||
if let Err(err) = self.framed.force_send(msg) {
|
if let Err(err) = framed.force_send(msg) {
|
||||||
self.dispatch_state =
|
*dispatch_state =
|
||||||
FramedState::FramedError(ServiceError::Encoder(err));
|
FramedState::FramedError(ServiceError::Encoder(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
buf_empty = inner.buf.is_empty();
|
buf_empty = inner.buf.is_empty();
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
self.dispatch_state =
|
*dispatch_state = FramedState::Error(ServiceError::Service(err));
|
||||||
FramedState::Error(ServiceError::Service(err));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !rx_done && self.rx.is_some() {
|
if !rx_done && rx.is_some() {
|
||||||
match self.rx.as_mut().unwrap().poll() {
|
match Pin::new(rx.as_mut().unwrap()).poll_next(cx) {
|
||||||
Ok(Async::Ready(Some(FramedMessage::Message(msg)))) => {
|
Poll::Ready(Some(FramedMessage::Message(msg))) => {
|
||||||
if let Err(err) = self.framed.force_send(msg) {
|
if let Err(err) = framed.force_send(msg) {
|
||||||
self.dispatch_state =
|
*dispatch_state =
|
||||||
FramedState::FramedError(ServiceError::Encoder(err));
|
FramedState::FramedError(ServiceError::Encoder(err));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Async::Ready(Some(FramedMessage::Close))) => {
|
Poll::Ready(Some(FramedMessage::Close)) => {
|
||||||
self.dispatch_state.stop(None);
|
dispatch_state.stop(None);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Ok(Async::Ready(Some(FramedMessage::WaitClose(tx)))) => {
|
Poll::Ready(Some(FramedMessage::WaitClose(tx))) => {
|
||||||
self.dispatch_state.stop(Some(tx));
|
dispatch_state.stop(Some(tx));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Ok(Async::Ready(None)) => {
|
Poll::Ready(None) => {
|
||||||
rx_done = true;
|
rx_done = true;
|
||||||
let _ = self.rx.take();
|
let _ = rx.take();
|
||||||
}
|
|
||||||
Ok(Async::NotReady) => rx_done = true,
|
|
||||||
Err(_e) => {
|
|
||||||
rx_done = true;
|
|
||||||
let _ = self.rx.take();
|
|
||||||
}
|
}
|
||||||
|
Poll::Pending => rx_done = true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -240,16 +374,15 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.framed.is_write_buf_empty() {
|
if !framed.is_write_buf_empty() {
|
||||||
match self.framed.poll_complete() {
|
match framed.flush(cx) {
|
||||||
Ok(Async::NotReady) => break,
|
Poll::Pending => break,
|
||||||
Err(err) => {
|
Poll::Ready(Err(err)) => {
|
||||||
debug!("Error sending data: {:?}", err);
|
debug!("Error sending data: {:?}", err);
|
||||||
self.dispatch_state =
|
*dispatch_state = FramedState::FramedError(ServiceError::Encoder(err));
|
||||||
FramedState::FramedError(ServiceError::Encoder(err));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Ok(Async::Ready(_)) => (),
|
Poll::Ready(_) => (),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@ -257,71 +390,4 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
false
|
false
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<St, S, T, U> Future for FramedDispatcher<St, S, T, U>
|
|
||||||
where
|
|
||||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
|
||||||
S::Error: 'static,
|
|
||||||
S::Future: 'static,
|
|
||||||
T: AsyncRead + AsyncWrite,
|
|
||||||
U: Decoder + Encoder,
|
|
||||||
<U as Encoder>::Item: 'static,
|
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
|
||||||
{
|
|
||||||
type Item = ();
|
|
||||||
type Error = ServiceError<S::Error, U>;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
unsafe { self.inner.get_ref().task.register() };
|
|
||||||
|
|
||||||
match mem::replace(&mut self.dispatch_state, FramedState::Processing) {
|
|
||||||
FramedState::Processing => {
|
|
||||||
if self.poll_read() || self.poll_write() {
|
|
||||||
self.poll()
|
|
||||||
} else {
|
|
||||||
Ok(Async::NotReady)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FramedState::Error(err) => {
|
|
||||||
if self.framed.is_write_buf_empty()
|
|
||||||
|| (self.poll_write() || self.framed.is_write_buf_empty())
|
|
||||||
{
|
|
||||||
self.disconnect(true);
|
|
||||||
Err(err)
|
|
||||||
} else {
|
|
||||||
self.dispatch_state = FramedState::Error(err);
|
|
||||||
Ok(Async::NotReady)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FramedState::FlushAndStop(mut vec) => {
|
|
||||||
if !self.framed.is_write_buf_empty() {
|
|
||||||
match self.framed.poll_complete() {
|
|
||||||
Err(err) => {
|
|
||||||
debug!("Error sending data: {:?}", err);
|
|
||||||
}
|
|
||||||
Ok(Async::NotReady) => {
|
|
||||||
self.dispatch_state = FramedState::FlushAndStop(vec);
|
|
||||||
return Ok(Async::NotReady);
|
|
||||||
}
|
|
||||||
Ok(Async::Ready(_)) => (),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
for tx in vec.drain(..) {
|
|
||||||
let _ = tx.send(());
|
|
||||||
}
|
|
||||||
self.disconnect(false);
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
FramedState::FramedError(err) => {
|
|
||||||
self.disconnect(true);
|
|
||||||
Err(err)
|
|
||||||
}
|
|
||||||
FramedState::Stopping => {
|
|
||||||
self.disconnect(false);
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder};
|
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder};
|
||||||
use actix_service::{IntoNewService, IntoService, NewService, Service};
|
use actix_service::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
||||||
use futures::{Async, Future, Poll};
|
use either::Either;
|
||||||
|
use futures::future::{FutureExt, LocalBoxFuture};
|
||||||
|
use pin_project::{pin_project, project};
|
||||||
|
|
||||||
use crate::connect::{Connect, ConnectResult};
|
use crate::connect::{Connect, ConnectResult};
|
||||||
use crate::dispatcher::FramedDispatcher;
|
use crate::dispatcher::FramedDispatcher;
|
||||||
@ -27,9 +32,9 @@ impl<St, Codec> Builder<St, Codec> {
|
|||||||
pub fn service<Io, C, F>(self, connect: F) -> ServiceBuilder<St, C, Io, Codec>
|
pub fn service<Io, C, F>(self, connect: F) -> ServiceBuilder<St, C, Io, Codec>
|
||||||
where
|
where
|
||||||
F: IntoService<C>,
|
F: IntoService<C>,
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite + Unpin,
|
||||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||||
Codec: Decoder + Encoder,
|
Codec: Decoder + Encoder + Unpin,
|
||||||
{
|
{
|
||||||
ServiceBuilder {
|
ServiceBuilder {
|
||||||
connect: connect.into_service(),
|
connect: connect.into_service(),
|
||||||
@ -41,19 +46,19 @@ impl<St, Codec> Builder<St, Codec> {
|
|||||||
/// Construct framed handler new service with specified connect service
|
/// Construct framed handler new service with specified connect service
|
||||||
pub fn factory<Io, C, F>(self, connect: F) -> NewServiceBuilder<St, C, Io, Codec>
|
pub fn factory<Io, C, F>(self, connect: F) -> NewServiceBuilder<St, C, Io, Codec>
|
||||||
where
|
where
|
||||||
F: IntoNewService<C>,
|
F: IntoServiceFactory<C>,
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite + Unpin,
|
||||||
C: NewService<
|
C: ServiceFactory<
|
||||||
Config = (),
|
Config = (),
|
||||||
Request = Connect<Io>,
|
Request = Connect<Io>,
|
||||||
Response = ConnectResult<Io, St, Codec>,
|
Response = ConnectResult<Io, St, Codec>,
|
||||||
>,
|
>,
|
||||||
C::Error: 'static,
|
C::Error: 'static,
|
||||||
C::Future: 'static,
|
C::Future: 'static,
|
||||||
Codec: Decoder + Encoder,
|
Codec: Decoder + Encoder + Unpin,
|
||||||
{
|
{
|
||||||
NewServiceBuilder {
|
NewServiceBuilder {
|
||||||
connect: connect.into_new_service(),
|
connect: connect.into_factory(),
|
||||||
disconnect: None,
|
disconnect: None,
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
@ -69,10 +74,10 @@ pub struct ServiceBuilder<St, C, Io, Codec> {
|
|||||||
impl<St, C, Io, Codec> ServiceBuilder<St, C, Io, Codec>
|
impl<St, C, Io, Codec> ServiceBuilder<St, C, Io, Codec>
|
||||||
where
|
where
|
||||||
St: 'static,
|
St: 'static,
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite + Unpin,
|
||||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||||
C::Error: 'static,
|
C::Error: 'static,
|
||||||
Codec: Decoder + Encoder,
|
Codec: Decoder + Encoder + Unpin,
|
||||||
<Codec as Encoder>::Item: 'static,
|
<Codec as Encoder>::Item: 'static,
|
||||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
@ -93,8 +98,8 @@ where
|
|||||||
service: F,
|
service: F,
|
||||||
) -> impl Service<Request = Io, Response = (), Error = ServiceError<C::Error, Codec>>
|
) -> impl Service<Request = Io, Response = (), Error = ServiceError<C::Error, Codec>>
|
||||||
where
|
where
|
||||||
F: IntoNewService<T>,
|
F: IntoServiceFactory<T>,
|
||||||
T: NewService<
|
T: ServiceFactory<
|
||||||
Config = St,
|
Config = St,
|
||||||
Request = RequestItem<St, Codec>,
|
Request = RequestItem<St, Codec>,
|
||||||
Response = ResponseItem<Codec>,
|
Response = ResponseItem<Codec>,
|
||||||
@ -104,7 +109,7 @@ where
|
|||||||
{
|
{
|
||||||
FramedServiceImpl {
|
FramedServiceImpl {
|
||||||
connect: self.connect,
|
connect: self.connect,
|
||||||
handler: Rc::new(service.into_new_service()),
|
handler: Rc::new(service.into_factory()),
|
||||||
disconnect: self.disconnect.clone(),
|
disconnect: self.disconnect.clone(),
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
@ -120,11 +125,15 @@ pub struct NewServiceBuilder<St, C, Io, Codec> {
|
|||||||
impl<St, C, Io, Codec> NewServiceBuilder<St, C, Io, Codec>
|
impl<St, C, Io, Codec> NewServiceBuilder<St, C, Io, Codec>
|
||||||
where
|
where
|
||||||
St: 'static,
|
St: 'static,
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite + Unpin,
|
||||||
C: NewService<Config = (), Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
C: ServiceFactory<
|
||||||
|
Config = (),
|
||||||
|
Request = Connect<Io>,
|
||||||
|
Response = ConnectResult<Io, St, Codec>,
|
||||||
|
>,
|
||||||
C::Error: 'static,
|
C::Error: 'static,
|
||||||
C::Future: 'static,
|
C::Future: 'static,
|
||||||
Codec: Decoder + Encoder,
|
Codec: Decoder + Encoder + Unpin,
|
||||||
<Codec as Encoder>::Item: 'static,
|
<Codec as Encoder>::Item: 'static,
|
||||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
@ -142,15 +151,15 @@ where
|
|||||||
pub fn finish<F, T, Cfg>(
|
pub fn finish<F, T, Cfg>(
|
||||||
self,
|
self,
|
||||||
service: F,
|
service: F,
|
||||||
) -> impl NewService<
|
) -> impl ServiceFactory<
|
||||||
Config = Cfg,
|
Config = Cfg,
|
||||||
Request = Io,
|
Request = Io,
|
||||||
Response = (),
|
Response = (),
|
||||||
Error = ServiceError<C::Error, Codec>,
|
Error = ServiceError<C::Error, Codec>,
|
||||||
>
|
>
|
||||||
where
|
where
|
||||||
F: IntoNewService<T>,
|
F: IntoServiceFactory<T>,
|
||||||
T: NewService<
|
T: ServiceFactory<
|
||||||
Config = St,
|
Config = St,
|
||||||
Request = RequestItem<St, Codec>,
|
Request = RequestItem<St, Codec>,
|
||||||
Response = ResponseItem<Codec>,
|
Response = ResponseItem<Codec>,
|
||||||
@ -160,7 +169,7 @@ where
|
|||||||
{
|
{
|
||||||
FramedService {
|
FramedService {
|
||||||
connect: self.connect,
|
connect: self.connect,
|
||||||
handler: Rc::new(service.into_new_service()),
|
handler: Rc::new(service.into_factory()),
|
||||||
disconnect: self.disconnect,
|
disconnect: self.disconnect,
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
@ -174,21 +183,25 @@ pub(crate) struct FramedService<St, C, T, Io, Codec, Cfg> {
|
|||||||
_t: PhantomData<(St, Io, Codec, Cfg)>,
|
_t: PhantomData<(St, Io, Codec, Cfg)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<St, C, T, Io, Codec, Cfg> NewService for FramedService<St, C, T, Io, Codec, Cfg>
|
impl<St, C, T, Io, Codec, Cfg> ServiceFactory for FramedService<St, C, T, Io, Codec, Cfg>
|
||||||
where
|
where
|
||||||
St: 'static,
|
St: 'static,
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite + Unpin,
|
||||||
C: NewService<Config = (), Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
C: ServiceFactory<
|
||||||
|
Config = (),
|
||||||
|
Request = Connect<Io>,
|
||||||
|
Response = ConnectResult<Io, St, Codec>,
|
||||||
|
>,
|
||||||
C::Error: 'static,
|
C::Error: 'static,
|
||||||
C::Future: 'static,
|
C::Future: 'static,
|
||||||
T: NewService<
|
T: ServiceFactory<
|
||||||
Config = St,
|
Config = St,
|
||||||
Request = RequestItem<St, Codec>,
|
Request = RequestItem<St, Codec>,
|
||||||
Response = ResponseItem<Codec>,
|
Response = ResponseItem<Codec>,
|
||||||
Error = C::Error,
|
Error = C::Error,
|
||||||
InitError = C::Error,
|
InitError = C::Error,
|
||||||
> + 'static,
|
> + 'static,
|
||||||
Codec: Decoder + Encoder,
|
Codec: Decoder + Encoder + Unpin,
|
||||||
<Codec as Encoder>::Item: 'static,
|
<Codec as Encoder>::Item: 'static,
|
||||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
@ -198,23 +211,24 @@ where
|
|||||||
type Error = ServiceError<C::Error, Codec>;
|
type Error = ServiceError<C::Error, Codec>;
|
||||||
type InitError = C::InitError;
|
type InitError = C::InitError;
|
||||||
type Service = FramedServiceImpl<St, C::Service, T, Io, Codec>;
|
type Service = FramedServiceImpl<St, C::Service, T, Io, Codec>;
|
||||||
type Future = Box<dyn Future<Item = Self::Service, Error = Self::InitError>>;
|
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, _: &Cfg) -> Self::Future {
|
fn new_service(&self, _: &Cfg) -> Self::Future {
|
||||||
let handler = self.handler.clone();
|
let handler = self.handler.clone();
|
||||||
let disconnect = self.disconnect.clone();
|
let disconnect = self.disconnect.clone();
|
||||||
|
|
||||||
// create connect service and then create service impl
|
// create connect service and then create service impl
|
||||||
Box::new(
|
|
||||||
self.connect
|
self.connect
|
||||||
.new_service(&())
|
.new_service(&())
|
||||||
.map(move |connect| FramedServiceImpl {
|
.map(move |result| {
|
||||||
|
result.map(move |connect| FramedServiceImpl {
|
||||||
connect,
|
connect,
|
||||||
handler,
|
handler,
|
||||||
disconnect,
|
disconnect,
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}),
|
})
|
||||||
)
|
})
|
||||||
|
.boxed_local()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,18 +241,18 @@ pub struct FramedServiceImpl<St, C, T, Io, Codec> {
|
|||||||
|
|
||||||
impl<St, C, T, Io, Codec> Service for FramedServiceImpl<St, C, T, Io, Codec>
|
impl<St, C, T, Io, Codec> Service for FramedServiceImpl<St, C, T, Io, Codec>
|
||||||
where
|
where
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite + Unpin,
|
||||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||||
C::Error: 'static,
|
C::Error: 'static,
|
||||||
T: NewService<
|
T: ServiceFactory<
|
||||||
Config = St,
|
Config = St,
|
||||||
Request = RequestItem<St, Codec>,
|
Request = RequestItem<St, Codec>,
|
||||||
Response = ResponseItem<Codec>,
|
Response = ResponseItem<Codec>,
|
||||||
Error = C::Error,
|
Error = C::Error,
|
||||||
InitError = C::Error,
|
InitError = C::Error,
|
||||||
>,
|
>,
|
||||||
<<T as NewService>::Service as Service>::Future: 'static,
|
<<T as ServiceFactory>::Service as Service>::Future: 'static,
|
||||||
Codec: Decoder + Encoder,
|
Codec: Decoder + Encoder + Unpin,
|
||||||
<Codec as Encoder>::Item: 'static,
|
<Codec as Encoder>::Item: 'static,
|
||||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
@ -247,8 +261,8 @@ where
|
|||||||
type Error = ServiceError<C::Error, Codec>;
|
type Error = ServiceError<C::Error, Codec>;
|
||||||
type Future = FramedServiceImplResponse<St, Io, Codec, C, T>;
|
type Future = FramedServiceImplResponse<St, Io, Codec, C, T>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
self.connect.poll_ready().map_err(|e| e.into())
|
self.connect.poll_ready(cx).map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Io) -> Self::Future {
|
fn call(&mut self, req: Io) -> Self::Future {
|
||||||
@ -256,108 +270,155 @@ where
|
|||||||
inner: FramedServiceImplResponseInner::Connect(
|
inner: FramedServiceImplResponseInner::Connect(
|
||||||
self.connect.call(Connect::new(req)),
|
self.connect.call(Connect::new(req)),
|
||||||
self.handler.clone(),
|
self.handler.clone(),
|
||||||
|
self.disconnect.clone(),
|
||||||
),
|
),
|
||||||
disconnect: self.disconnect.clone(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pin_project]
|
||||||
pub struct FramedServiceImplResponse<St, Io, Codec, C, T>
|
pub struct FramedServiceImplResponse<St, Io, Codec, C, T>
|
||||||
where
|
where
|
||||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||||
C::Error: 'static,
|
C::Error: 'static,
|
||||||
T: NewService<
|
T: ServiceFactory<
|
||||||
Config = St,
|
Config = St,
|
||||||
Request = RequestItem<St, Codec>,
|
Request = RequestItem<St, Codec>,
|
||||||
Response = ResponseItem<Codec>,
|
Response = ResponseItem<Codec>,
|
||||||
Error = C::Error,
|
Error = C::Error,
|
||||||
InitError = C::Error,
|
InitError = C::Error,
|
||||||
>,
|
>,
|
||||||
<<T as NewService>::Service as Service>::Future: 'static,
|
<<T as ServiceFactory>::Service as Service>::Future: 'static,
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite + Unpin,
|
||||||
Codec: Encoder + Decoder,
|
Codec: Encoder + Decoder + Unpin,
|
||||||
<Codec as Encoder>::Item: 'static,
|
<Codec as Encoder>::Item: 'static,
|
||||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
inner: FramedServiceImplResponseInner<St, Io, Codec, C, T>,
|
inner: FramedServiceImplResponseInner<St, Io, Codec, C, T>,
|
||||||
disconnect: Option<Rc<dyn Fn(&mut St, bool)>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum FramedServiceImplResponseInner<St, Io, Codec, C, T>
|
|
||||||
where
|
|
||||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
|
||||||
C::Error: 'static,
|
|
||||||
T: NewService<
|
|
||||||
Config = St,
|
|
||||||
Request = RequestItem<St, Codec>,
|
|
||||||
Response = ResponseItem<Codec>,
|
|
||||||
Error = C::Error,
|
|
||||||
InitError = C::Error,
|
|
||||||
>,
|
|
||||||
<<T as NewService>::Service as Service>::Future: 'static,
|
|
||||||
Io: AsyncRead + AsyncWrite,
|
|
||||||
Codec: Encoder + Decoder,
|
|
||||||
<Codec as Encoder>::Item: 'static,
|
|
||||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
|
||||||
{
|
|
||||||
Connect(C::Future, Rc<T>),
|
|
||||||
Handler(T::Future, Option<ConnectResult<Io, St, Codec>>),
|
|
||||||
Dispatcher(FramedDispatcher<St, T::Service, Io, Codec>),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<St, Io, Codec, C, T> Future for FramedServiceImplResponse<St, Io, Codec, C, T>
|
impl<St, Io, Codec, C, T> Future for FramedServiceImplResponse<St, Io, Codec, C, T>
|
||||||
where
|
where
|
||||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||||
C::Error: 'static,
|
C::Error: 'static,
|
||||||
T: NewService<
|
T: ServiceFactory<
|
||||||
Config = St,
|
Config = St,
|
||||||
Request = RequestItem<St, Codec>,
|
Request = RequestItem<St, Codec>,
|
||||||
Response = ResponseItem<Codec>,
|
Response = ResponseItem<Codec>,
|
||||||
Error = C::Error,
|
Error = C::Error,
|
||||||
InitError = C::Error,
|
InitError = C::Error,
|
||||||
>,
|
>,
|
||||||
<<T as NewService>::Service as Service>::Future: 'static,
|
<<T as ServiceFactory>::Service as Service>::Future: 'static,
|
||||||
Io: AsyncRead + AsyncWrite,
|
Io: AsyncRead + AsyncWrite + Unpin,
|
||||||
Codec: Encoder + Decoder,
|
Codec: Encoder + Decoder + Unpin,
|
||||||
<Codec as Encoder>::Item: 'static,
|
<Codec as Encoder>::Item: 'static,
|
||||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
type Item = ();
|
type Output = Result<(), ServiceError<C::Error, Codec>>;
|
||||||
type Error = ServiceError<C::Error, Codec>;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||||
match self.inner {
|
let this = self.get_mut();
|
||||||
FramedServiceImplResponseInner::Connect(ref mut fut, ref handler) => {
|
|
||||||
match fut.poll()? {
|
loop {
|
||||||
Async::Ready(res) => {
|
match unsafe { Pin::new_unchecked(&mut this.inner) }.poll(cx) {
|
||||||
self.inner = FramedServiceImplResponseInner::Handler(
|
Either::Left(new) => this.inner = new,
|
||||||
|
Either::Right(poll) => return poll,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pin_project]
|
||||||
|
enum FramedServiceImplResponseInner<St, Io, Codec, C, T>
|
||||||
|
where
|
||||||
|
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||||
|
C::Error: 'static,
|
||||||
|
T: ServiceFactory<
|
||||||
|
Config = St,
|
||||||
|
Request = RequestItem<St, Codec>,
|
||||||
|
Response = ResponseItem<Codec>,
|
||||||
|
Error = C::Error,
|
||||||
|
InitError = C::Error,
|
||||||
|
>,
|
||||||
|
<<T as ServiceFactory>::Service as Service>::Future: 'static,
|
||||||
|
Io: AsyncRead + AsyncWrite + Unpin,
|
||||||
|
Codec: Encoder + Decoder + Unpin,
|
||||||
|
<Codec as Encoder>::Item: 'static,
|
||||||
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
Connect(#[pin] C::Future, Rc<T>, Option<Rc<dyn Fn(&mut St, bool)>>),
|
||||||
|
Handler(
|
||||||
|
#[pin] T::Future,
|
||||||
|
Option<ConnectResult<Io, St, Codec>>,
|
||||||
|
Option<Rc<dyn Fn(&mut St, bool)>>,
|
||||||
|
),
|
||||||
|
Dispatcher(FramedDispatcher<St, T::Service, Io, Codec>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<St, Io, Codec, C, T> FramedServiceImplResponseInner<St, Io, Codec, C, T>
|
||||||
|
where
|
||||||
|
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||||
|
C::Error: 'static,
|
||||||
|
T: ServiceFactory<
|
||||||
|
Config = St,
|
||||||
|
Request = RequestItem<St, Codec>,
|
||||||
|
Response = ResponseItem<Codec>,
|
||||||
|
Error = C::Error,
|
||||||
|
InitError = C::Error,
|
||||||
|
>,
|
||||||
|
<<T as ServiceFactory>::Service as Service>::Future: 'static,
|
||||||
|
Io: AsyncRead + AsyncWrite + Unpin,
|
||||||
|
Codec: Encoder + Decoder + Unpin,
|
||||||
|
<Codec as Encoder>::Item: 'static,
|
||||||
|
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
#[project]
|
||||||
|
fn poll(
|
||||||
|
self: Pin<&mut Self>,
|
||||||
|
cx: &mut Context,
|
||||||
|
) -> Either<
|
||||||
|
FramedServiceImplResponseInner<St, Io, Codec, C, T>,
|
||||||
|
Poll<Result<(), ServiceError<C::Error, Codec>>>,
|
||||||
|
> {
|
||||||
|
#[project]
|
||||||
|
match self.project() {
|
||||||
|
FramedServiceImplResponseInner::Connect(
|
||||||
|
ref mut fut,
|
||||||
|
ref handler,
|
||||||
|
ref mut disconnect,
|
||||||
|
) => match Pin::new(fut).poll(cx) {
|
||||||
|
Poll::Ready(Ok(res)) => Either::Left(FramedServiceImplResponseInner::Handler(
|
||||||
handler.new_service(&res.state),
|
handler.new_service(&res.state),
|
||||||
Some(res),
|
Some(res),
|
||||||
);
|
disconnect.take(),
|
||||||
self.poll()
|
)),
|
||||||
}
|
Poll::Pending => Either::Right(Poll::Pending),
|
||||||
Async::NotReady => Ok(Async::NotReady),
|
Poll::Ready(Err(e)) => Either::Right(Poll::Ready(Err(e.into()))),
|
||||||
}
|
},
|
||||||
}
|
FramedServiceImplResponseInner::Handler(
|
||||||
FramedServiceImplResponseInner::Handler(ref mut fut, ref mut res) => {
|
ref mut fut,
|
||||||
match fut.poll()? {
|
ref mut res,
|
||||||
Async::Ready(handler) => {
|
ref mut disconnect,
|
||||||
|
) => match Pin::new(fut).poll(cx) {
|
||||||
|
Poll::Ready(Ok(handler)) => {
|
||||||
let res = res.take().unwrap();
|
let res = res.take().unwrap();
|
||||||
self.inner =
|
Either::Left(FramedServiceImplResponseInner::Dispatcher(
|
||||||
FramedServiceImplResponseInner::Dispatcher(FramedDispatcher::new(
|
FramedDispatcher::new(
|
||||||
res.framed,
|
res.framed,
|
||||||
State::new(res.state),
|
State::new(res.state),
|
||||||
handler,
|
handler,
|
||||||
res.rx,
|
res.rx,
|
||||||
res.sink,
|
res.sink,
|
||||||
self.disconnect.clone(),
|
disconnect.take(),
|
||||||
));
|
),
|
||||||
self.poll()
|
))
|
||||||
}
|
}
|
||||||
Async::NotReady => Ok(Async::NotReady),
|
Poll::Pending => Either::Right(Poll::Pending),
|
||||||
|
Poll::Ready(Err(e)) => Either::Right(Poll::Ready(Err(e.into()))),
|
||||||
|
},
|
||||||
|
FramedServiceImplResponseInner::Dispatcher(ref mut fut) => {
|
||||||
|
Either::Right(Pin::new(fut).poll(cx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
FramedServiceImplResponseInner::Dispatcher(ref mut fut) => fut.poll(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use futures::unsync::{mpsc, oneshot};
|
use actix_utils::{mpsc, oneshot};
|
||||||
use futures::Future;
|
use futures::future::{Future, FutureExt};
|
||||||
|
|
||||||
use crate::dispatcher::FramedMessage;
|
use crate::dispatcher::FramedMessage;
|
||||||
|
|
||||||
pub struct Sink<T>(mpsc::UnboundedSender<FramedMessage<T>>);
|
pub struct Sink<T>(mpsc::Sender<FramedMessage<T>>);
|
||||||
|
|
||||||
impl<T> Clone for Sink<T> {
|
impl<T> Clone for Sink<T> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
@ -14,26 +14,26 @@ impl<T> Clone for Sink<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Sink<T> {
|
impl<T> Sink<T> {
|
||||||
pub(crate) fn new(tx: mpsc::UnboundedSender<FramedMessage<T>>) -> Self {
|
pub(crate) fn new(tx: mpsc::Sender<FramedMessage<T>>) -> Self {
|
||||||
Sink(tx)
|
Sink(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Close connection
|
/// Close connection
|
||||||
pub fn close(&self) {
|
pub fn close(&self) {
|
||||||
let _ = self.0.unbounded_send(FramedMessage::Close);
|
let _ = self.0.send(FramedMessage::Close);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Close connection
|
/// Close connection
|
||||||
pub fn wait_close(&self) -> impl Future<Item = (), Error = ()> {
|
pub fn wait_close(&self) -> impl Future<Output = ()> {
|
||||||
let (tx, rx) = oneshot::channel();
|
let (tx, rx) = oneshot::channel();
|
||||||
let _ = self.0.unbounded_send(FramedMessage::WaitClose(tx));
|
let _ = self.0.send(FramedMessage::WaitClose(tx));
|
||||||
|
|
||||||
rx.map_err(|_| ())
|
rx.map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send item
|
/// Send item
|
||||||
pub fn send(&self, item: T) {
|
pub fn send(&self, item: T) {
|
||||||
let _ = self.0.unbounded_send(FramedMessage::Message(item));
|
let _ = self.0.send(FramedMessage::Message(item));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,11 +4,11 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use actix_codec::BytesCodec;
|
use actix_codec::BytesCodec;
|
||||||
use actix_server_config::Io;
|
use actix_server_config::Io;
|
||||||
use actix_service::{new_apply_fn, Service};
|
use actix_service::{apply_fn_factory, service_fn, Service};
|
||||||
use actix_testing::{self as test, TestServer};
|
use actix_testing::{self as test, TestServer};
|
||||||
use futures::Future;
|
use futures::future::ok;
|
||||||
use tokio_tcp::TcpStream;
|
use tokio_net::tcp::TcpStream;
|
||||||
use tokio_timer::sleep;
|
use tokio_timer::delay_for;
|
||||||
|
|
||||||
use actix_ioframe::{Builder, Connect};
|
use actix_ioframe::{Builder, Connect};
|
||||||
|
|
||||||
@ -22,13 +22,15 @@ fn test_disconnect() -> std::io::Result<()> {
|
|||||||
let srv = TestServer::with(move || {
|
let srv = TestServer::with(move || {
|
||||||
let disconnect1 = disconnect1.clone();
|
let disconnect1 = disconnect1.clone();
|
||||||
|
|
||||||
new_apply_fn(
|
apply_fn_factory(
|
||||||
Builder::new()
|
Builder::new()
|
||||||
.factory(|conn: Connect<_>| Ok(conn.codec(BytesCodec).state(State)))
|
.factory(service_fn(|conn: Connect<_>| {
|
||||||
|
ok(conn.codec(BytesCodec).state(State))
|
||||||
|
}))
|
||||||
.disconnect(move |_, _| {
|
.disconnect(move |_, _| {
|
||||||
disconnect1.store(true, Ordering::Relaxed);
|
disconnect1.store(true, Ordering::Relaxed);
|
||||||
})
|
})
|
||||||
.finish(|_t| Ok(None)),
|
.finish(service_fn(|_t| ok(None))),
|
||||||
|io: Io<TcpStream>, srv| srv.call(io.into_parts().0),
|
|io: Io<TcpStream>, srv| srv.call(io.into_parts().0),
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
@ -37,9 +39,9 @@ fn test_disconnect() -> std::io::Result<()> {
|
|||||||
.service(|conn: Connect<_>| {
|
.service(|conn: Connect<_>| {
|
||||||
let conn = conn.codec(BytesCodec).state(State);
|
let conn = conn.codec(BytesCodec).state(State);
|
||||||
conn.sink().close();
|
conn.sink().close();
|
||||||
Ok(conn)
|
ok(conn)
|
||||||
})
|
})
|
||||||
.finish(|_t| Ok(None));
|
.finish(service_fn(|_t| ok(None)));
|
||||||
|
|
||||||
let conn = test::block_on(
|
let conn = test::block_on(
|
||||||
actix_connect::default_connector()
|
actix_connect::default_connector()
|
||||||
@ -48,11 +50,7 @@ fn test_disconnect() -> std::io::Result<()> {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
test::block_on(client.call(conn.into_parts().0)).unwrap();
|
test::block_on(client.call(conn.into_parts().0)).unwrap();
|
||||||
let _ = test::block_on(
|
let _ = test::block_on(delay_for(Duration::from_millis(100)));
|
||||||
sleep(Duration::from_millis(100))
|
|
||||||
.map(|_| ())
|
|
||||||
.map_err(|_| ()),
|
|
||||||
);
|
|
||||||
assert!(disconnect.load(Ordering::Relaxed));
|
assert!(disconnect.load(Ordering::Relaxed));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-rt"
|
name = "actix-rt"
|
||||||
version = "0.2.6"
|
version = "1.0.0-alpha.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix runtime"
|
description = "Actix runtime"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -18,10 +18,13 @@ name = "actix_rt"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-threadpool = "0.1.1"
|
actix-threadpool = "0.2"
|
||||||
futures = "0.1.25"
|
futures = "0.3.1"
|
||||||
tokio-current-thread = "0.1"
|
|
||||||
tokio-executor = "0.1.5"
|
# TODO: Replace this with dependency on tokio-runtime once it is ready
|
||||||
tokio-reactor = "0.1.7"
|
tokio = { version = "0.2.0-alpha.6" }
|
||||||
tokio-timer = "0.2.8"
|
tokio-timer = "=0.3.0-alpha.6"
|
||||||
|
tokio-executor = "=0.2.0-alpha.6"
|
||||||
|
tokio-net = "=0.2.0-alpha.6"
|
||||||
|
|
||||||
copyless = "0.1.4"
|
copyless = "0.1.4"
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
use std::any::{Any, TypeId};
|
use std::any::{Any, TypeId};
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::pin::Pin;
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
use std::task::{Context, Poll};
|
||||||
use std::{fmt, thread};
|
use std::{fmt, thread};
|
||||||
|
|
||||||
use futures::sync::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
|
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
|
||||||
use futures::sync::oneshot::{channel, Canceled, Sender};
|
use futures::channel::oneshot::{channel, Canceled, Sender};
|
||||||
use futures::{future, Async, Future, IntoFuture, Poll, Stream};
|
use futures::{future, Future, FutureExt, Stream};
|
||||||
use tokio_current_thread::spawn;
|
use tokio::runtime::current_thread::spawn;
|
||||||
|
|
||||||
use crate::builder::Builder;
|
use crate::builder::Builder;
|
||||||
use crate::system::System;
|
use crate::system::System;
|
||||||
@ -17,7 +19,7 @@ use copyless::BoxHelper;
|
|||||||
thread_local!(
|
thread_local!(
|
||||||
static ADDR: RefCell<Option<Arbiter>> = RefCell::new(None);
|
static ADDR: RefCell<Option<Arbiter>> = RefCell::new(None);
|
||||||
static RUNNING: Cell<bool> = Cell::new(false);
|
static RUNNING: Cell<bool> = Cell::new(false);
|
||||||
static Q: RefCell<Vec<Box<dyn Future<Item = (), Error = ()>>>> = RefCell::new(Vec::new());
|
static Q: RefCell<Vec<Box<dyn Future<Output = ()>>>> = RefCell::new(Vec::new());
|
||||||
static STORAGE: RefCell<HashMap<TypeId, Box<dyn Any>>> = RefCell::new(HashMap::new());
|
static STORAGE: RefCell<HashMap<TypeId, Box<dyn Any>>> = RefCell::new(HashMap::new());
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -25,7 +27,7 @@ pub(crate) static COUNT: AtomicUsize = AtomicUsize::new(0);
|
|||||||
|
|
||||||
pub(crate) enum ArbiterCommand {
|
pub(crate) enum ArbiterCommand {
|
||||||
Stop,
|
Stop,
|
||||||
Execute(Box<dyn Future<Item = (), Error = ()> + Send>),
|
Execute(Box<dyn Future<Output = ()> + Unpin + Send>),
|
||||||
ExecuteFn(Box<dyn FnExec>),
|
ExecuteFn(Box<dyn FnExec>),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +98,9 @@ impl Arbiter {
|
|||||||
let (arb_tx, arb_rx) = unbounded();
|
let (arb_tx, arb_rx) = unbounded();
|
||||||
let arb_tx2 = arb_tx.clone();
|
let arb_tx2 = arb_tx.clone();
|
||||||
|
|
||||||
let handle = thread::Builder::new().name(name.clone()).spawn(move || {
|
let handle = thread::Builder::new()
|
||||||
|
.name(name.clone())
|
||||||
|
.spawn(move || {
|
||||||
let mut rt = Builder::new().build_rt().expect("Can not create Runtime");
|
let mut rt = Builder::new().build_rt().expect("Can not create Runtime");
|
||||||
let arb = Arbiter::with_sender(arb_tx);
|
let arb = Arbiter::with_sender(arb_tx);
|
||||||
|
|
||||||
@ -128,9 +132,15 @@ impl Arbiter {
|
|||||||
let _ = System::current()
|
let _ = System::current()
|
||||||
.sys()
|
.sys()
|
||||||
.unbounded_send(SystemCommand::UnregisterArbiter(id));
|
.unbounded_send(SystemCommand::UnregisterArbiter(id));
|
||||||
}).unwrap_or_else(|err| panic!("Cannot spawn an arbiter's thread {:?}: {:?}", &name, err));
|
})
|
||||||
|
.unwrap_or_else(|err| {
|
||||||
|
panic!("Cannot spawn an arbiter's thread {:?}: {:?}", &name, err)
|
||||||
|
});
|
||||||
|
|
||||||
Arbiter{sender: arb_tx2, thread_handle: Some(handle)}
|
Arbiter {
|
||||||
|
sender: arb_tx2,
|
||||||
|
thread_handle: Some(handle),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn run_system() {
|
pub(crate) fn run_system() {
|
||||||
@ -138,7 +148,9 @@ impl Arbiter {
|
|||||||
Q.with(|cell| {
|
Q.with(|cell| {
|
||||||
let mut v = cell.borrow_mut();
|
let mut v = cell.borrow_mut();
|
||||||
for fut in v.drain(..) {
|
for fut in v.drain(..) {
|
||||||
spawn(fut);
|
// We pin the boxed future, so it can never again be moved.
|
||||||
|
let fut = unsafe { Pin::new_unchecked(fut) };
|
||||||
|
tokio_executor::current_thread::spawn(fut);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -152,12 +164,15 @@ impl Arbiter {
|
|||||||
/// thread.
|
/// thread.
|
||||||
pub fn spawn<F>(future: F)
|
pub fn spawn<F>(future: F)
|
||||||
where
|
where
|
||||||
F: Future<Item = (), Error = ()> + 'static,
|
F: Future<Output = ()> + 'static,
|
||||||
{
|
{
|
||||||
RUNNING.with(move |cell| {
|
RUNNING.with(move |cell| {
|
||||||
if cell.get() {
|
if cell.get() {
|
||||||
spawn(Box::alloc().init(future));
|
// Spawn the future on running executor
|
||||||
|
spawn(future);
|
||||||
} else {
|
} else {
|
||||||
|
// Box the future and push it to the queue, this results in double boxing
|
||||||
|
// because the executor boxes the future again, but works for now
|
||||||
Q.with(move |cell| cell.borrow_mut().push(Box::alloc().init(future)));
|
Q.with(move |cell| cell.borrow_mut().push(Box::alloc().init(future)));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -169,15 +184,15 @@ impl Arbiter {
|
|||||||
pub fn spawn_fn<F, R>(f: F)
|
pub fn spawn_fn<F, R>(f: F)
|
||||||
where
|
where
|
||||||
F: FnOnce() -> R + 'static,
|
F: FnOnce() -> R + 'static,
|
||||||
R: IntoFuture<Item = (), Error = ()> + 'static,
|
R: Future<Output = ()> + 'static,
|
||||||
{
|
{
|
||||||
Arbiter::spawn(future::lazy(f))
|
Arbiter::spawn(future::lazy(|_| f()).flatten())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send a future to the Arbiter's thread, and spawn it.
|
/// Send a future to the Arbiter's thread, and spawn it.
|
||||||
pub fn send<F>(&self, future: F)
|
pub fn send<F>(&self, future: F)
|
||||||
where
|
where
|
||||||
F: Future<Item = (), Error = ()> + Send + 'static,
|
F: Future<Output = ()> + Send + Unpin + 'static,
|
||||||
{
|
{
|
||||||
let _ = self
|
let _ = self
|
||||||
.sender
|
.sender
|
||||||
@ -200,7 +215,7 @@ impl Arbiter {
|
|||||||
/// Send a function to the Arbiter's thread. This function will be executed asynchronously.
|
/// Send a function to the Arbiter's thread. This function will be executed asynchronously.
|
||||||
/// A future is created, and when resolved will contain the result of the function sent
|
/// A future is created, and when resolved will contain the result of the function sent
|
||||||
/// to the Arbiters thread.
|
/// to the Arbiters thread.
|
||||||
pub fn exec<F, R>(&self, f: F) -> impl Future<Item = R, Error = Canceled>
|
pub fn exec<F, R>(&self, f: F) -> impl Future<Output = Result<R, Canceled>>
|
||||||
where
|
where
|
||||||
F: FnOnce() -> R + Send + 'static,
|
F: FnOnce() -> R + Send + 'static,
|
||||||
R: Send + 'static,
|
R: Send + 'static,
|
||||||
@ -261,15 +276,17 @@ impl Arbiter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn with_sender(sender: UnboundedSender<ArbiterCommand>) -> Self {
|
fn with_sender(sender: UnboundedSender<ArbiterCommand>) -> Self {
|
||||||
Self{sender, thread_handle: None}
|
Self {
|
||||||
|
sender,
|
||||||
|
thread_handle: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wait for the event loop to stop by joining the underlying thread (if have Some).
|
/// Wait for the event loop to stop by joining the underlying thread (if have Some).
|
||||||
pub fn join(&mut self) -> thread::Result<()>{
|
pub fn join(&mut self) -> thread::Result<()> {
|
||||||
if let Some(thread_handle) = self.thread_handle.take() {
|
if let Some(thread_handle) = self.thread_handle.take() {
|
||||||
thread_handle.join()
|
thread_handle.join()
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -294,19 +311,18 @@ impl Drop for ArbiterController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Future for ArbiterController {
|
impl Future for ArbiterController {
|
||||||
type Item = ();
|
type Output = ();
|
||||||
type Error = ();
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
loop {
|
loop {
|
||||||
match self.rx.poll() {
|
match Pin::new(&mut self.rx).poll_next(cx) {
|
||||||
Ok(Async::Ready(None)) | Err(_) => return Ok(Async::Ready(())),
|
Poll::Ready(None) => return Poll::Ready(()),
|
||||||
Ok(Async::Ready(Some(item))) => match item {
|
Poll::Ready(Some(item)) => match item {
|
||||||
ArbiterCommand::Stop => {
|
ArbiterCommand::Stop => {
|
||||||
if let Some(stop) = self.stop.take() {
|
if let Some(stop) = self.stop.take() {
|
||||||
let _ = stop.send(0);
|
let _ = stop.send(0);
|
||||||
};
|
};
|
||||||
return Ok(Async::Ready(()));
|
return Poll::Ready(());
|
||||||
}
|
}
|
||||||
ArbiterCommand::Execute(fut) => {
|
ArbiterCommand::Execute(fut) => {
|
||||||
spawn(fut);
|
spawn(fut);
|
||||||
@ -315,7 +331,7 @@ impl Future for ArbiterController {
|
|||||||
f.call_box();
|
f.call_box();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
Poll::Pending => return Poll::Pending,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -346,14 +362,13 @@ impl SystemArbiter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Future for SystemArbiter {
|
impl Future for SystemArbiter {
|
||||||
type Item = ();
|
type Output = ();
|
||||||
type Error = ();
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
loop {
|
loop {
|
||||||
match self.commands.poll() {
|
match Pin::new(&mut self.commands).poll_next(cx) {
|
||||||
Ok(Async::Ready(None)) | Err(_) => return Ok(Async::Ready(())),
|
Poll::Ready(None) => return Poll::Ready(()),
|
||||||
Ok(Async::Ready(Some(cmd))) => match cmd {
|
Poll::Ready(Some(cmd)) => match cmd {
|
||||||
SystemCommand::Exit(code) => {
|
SystemCommand::Exit(code) => {
|
||||||
// stop arbiters
|
// stop arbiters
|
||||||
for arb in self.arbiters.values() {
|
for arb in self.arbiters.values() {
|
||||||
@ -371,7 +386,7 @@ impl Future for SystemArbiter {
|
|||||||
self.arbiters.remove(&name);
|
self.arbiters.remove(&name);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
Poll::Pending => return Poll::Pending,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use futures::future;
|
use futures::channel::mpsc::unbounded;
|
||||||
|
use futures::channel::oneshot::{channel, Receiver};
|
||||||
use futures::future::{lazy, Future};
|
use futures::future::{lazy, Future};
|
||||||
use futures::sync::mpsc::unbounded;
|
use futures::{future, FutureExt};
|
||||||
use futures::sync::oneshot::{channel, Receiver};
|
|
||||||
|
|
||||||
use tokio_current_thread::{CurrentThread, Handle};
|
use tokio::runtime::current_thread::Handle;
|
||||||
use tokio_reactor::Reactor;
|
use tokio_net::driver::Reactor;
|
||||||
use tokio_timer::clock::Clock;
|
use tokio_timer::{clock::Clock, timer::Timer};
|
||||||
use tokio_timer::timer::Timer;
|
|
||||||
|
|
||||||
use crate::arbiter::{Arbiter, SystemArbiter};
|
use crate::arbiter::{Arbiter, SystemArbiter};
|
||||||
use crate::runtime::Runtime;
|
use crate::runtime::Runtime;
|
||||||
use crate::system::System;
|
use crate::system::System;
|
||||||
|
use tokio_executor::current_thread::CurrentThread;
|
||||||
|
|
||||||
/// Builder struct for a actix runtime.
|
/// Builder struct for a actix runtime.
|
||||||
///
|
///
|
||||||
@ -118,7 +118,7 @@ impl Builder {
|
|||||||
rt.spawn(arb);
|
rt.spawn(arb);
|
||||||
|
|
||||||
// init system arbiter and run configuration method
|
// init system arbiter and run configuration method
|
||||||
let _ = rt.block_on(lazy(move || {
|
let _ = rt.block_on(lazy(move |_| {
|
||||||
f();
|
f();
|
||||||
Ok::<_, ()>(())
|
Ok::<_, ()>(())
|
||||||
}));
|
}));
|
||||||
@ -159,13 +159,14 @@ pub(crate) struct AsyncSystemRunner {
|
|||||||
impl AsyncSystemRunner {
|
impl AsyncSystemRunner {
|
||||||
/// This function will start event loop and returns a future that
|
/// This function will start event loop and returns a future that
|
||||||
/// resolves once the `System::stop()` function is called.
|
/// resolves once the `System::stop()` function is called.
|
||||||
pub(crate) fn run_nonblocking(self) -> impl Future<Item = (), Error = io::Error> + Send {
|
pub(crate) fn run_nonblocking(self) -> impl Future<Output = Result<(), io::Error>> + Send {
|
||||||
let AsyncSystemRunner { stop, .. } = self;
|
let AsyncSystemRunner { stop, .. } = self;
|
||||||
|
|
||||||
// run loop
|
// run loop
|
||||||
future::lazy(|| {
|
future::lazy(|_| {
|
||||||
Arbiter::run_system();
|
Arbiter::run_system();
|
||||||
stop.then(|res| match res {
|
async {
|
||||||
|
let res = match stop.await {
|
||||||
Ok(code) => {
|
Ok(code) => {
|
||||||
if code != 0 {
|
if code != 0 {
|
||||||
Err(io::Error::new(
|
Err(io::Error::new(
|
||||||
@ -177,12 +178,12 @@ impl AsyncSystemRunner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
|
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
|
||||||
})
|
};
|
||||||
.then(|result| {
|
|
||||||
Arbiter::stop_system();
|
Arbiter::stop_system();
|
||||||
result
|
return res;
|
||||||
})
|
}
|
||||||
})
|
})
|
||||||
|
.flatten()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,10 +203,10 @@ impl SystemRunner {
|
|||||||
let SystemRunner { mut rt, stop, .. } = self;
|
let SystemRunner { mut rt, stop, .. } = self;
|
||||||
|
|
||||||
// run loop
|
// run loop
|
||||||
let _ = rt.block_on(lazy(move || {
|
let _ = rt.block_on(async {
|
||||||
Arbiter::run_system();
|
Arbiter::run_system();
|
||||||
Ok::<_, ()>(())
|
Ok::<_, ()>(())
|
||||||
}));
|
});
|
||||||
let result = match rt.block_on(stop) {
|
let result = match rt.block_on(stop) {
|
||||||
Ok(code) => {
|
Ok(code) => {
|
||||||
if code != 0 {
|
if code != 0 {
|
||||||
@ -224,19 +225,19 @@ impl SystemRunner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Execute a future and wait for result.
|
/// Execute a future and wait for result.
|
||||||
pub fn block_on<F, I, E>(&mut self, fut: F) -> Result<I, E>
|
pub fn block_on<F, O>(&mut self, fut: F) -> O
|
||||||
where
|
where
|
||||||
F: Future<Item = I, Error = E>,
|
F: Future<Output = O>,
|
||||||
{
|
{
|
||||||
let _ = self.rt.block_on(lazy(move || {
|
let _ = self.rt.block_on(async {
|
||||||
Arbiter::run_system();
|
Arbiter::run_system();
|
||||||
Ok::<_, ()>(())
|
});
|
||||||
}));
|
|
||||||
let res = self.rt.block_on(fut);
|
let res = self.rt.block_on(fut);
|
||||||
let _ = self.rt.block_on(lazy(move || {
|
let _ = self.rt.block_on(async {
|
||||||
Arbiter::stop_system();
|
Arbiter::stop_system();
|
||||||
Ok::<_, ()>(())
|
});
|
||||||
}));
|
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ pub use actix_threadpool as blocking;
|
|||||||
/// This function panics if actix system is not running.
|
/// This function panics if actix system is not running.
|
||||||
pub fn spawn<F>(f: F)
|
pub fn spawn<F>(f: F)
|
||||||
where
|
where
|
||||||
F: futures::Future<Item = (), Error = ()> + 'static,
|
F: futures::Future<Output = ()> + 'static,
|
||||||
{
|
{
|
||||||
if !System::is_set() {
|
if !System::is_set() {
|
||||||
panic!("System is not running");
|
panic!("System is not running");
|
||||||
|
@ -2,11 +2,12 @@ use std::error::Error;
|
|||||||
use std::{fmt, io};
|
use std::{fmt, io};
|
||||||
|
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use tokio_current_thread::{self as current_thread, CurrentThread};
|
use tokio_executor::current_thread::{self, CurrentThread};
|
||||||
use tokio_executor;
|
use tokio_net::driver::{Handle as ReactorHandle, Reactor};
|
||||||
use tokio_reactor::{self, Reactor};
|
use tokio_timer::{
|
||||||
use tokio_timer::clock::{self, Clock};
|
clock::Clock,
|
||||||
use tokio_timer::timer::{self, Timer};
|
timer::{self, Timer},
|
||||||
|
};
|
||||||
|
|
||||||
use crate::builder::Builder;
|
use crate::builder::Builder;
|
||||||
|
|
||||||
@ -18,7 +19,7 @@ use crate::builder::Builder;
|
|||||||
/// [mod]: index.html
|
/// [mod]: index.html
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Runtime {
|
pub struct Runtime {
|
||||||
reactor_handle: tokio_reactor::Handle,
|
reactor_handle: ReactorHandle,
|
||||||
timer_handle: timer::Handle,
|
timer_handle: timer::Handle,
|
||||||
clock: Clock,
|
clock: Clock,
|
||||||
executor: CurrentThread<Timer<Reactor>>,
|
executor: CurrentThread<Timer<Reactor>>,
|
||||||
@ -53,7 +54,7 @@ impl Runtime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn new2(
|
pub(super) fn new2(
|
||||||
reactor_handle: tokio_reactor::Handle,
|
reactor_handle: ReactorHandle,
|
||||||
timer_handle: timer::Handle,
|
timer_handle: timer::Handle,
|
||||||
clock: Clock,
|
clock: Clock,
|
||||||
executor: CurrentThread<Timer<Reactor>>,
|
executor: CurrentThread<Timer<Reactor>>,
|
||||||
@ -83,9 +84,8 @@ impl Runtime {
|
|||||||
/// let mut rt = Runtime::new().unwrap();
|
/// let mut rt = Runtime::new().unwrap();
|
||||||
///
|
///
|
||||||
/// // Spawn a future onto the runtime
|
/// // Spawn a future onto the runtime
|
||||||
/// rt.spawn(future::lazy(|| {
|
/// rt.spawn(future::lazy(|_| {
|
||||||
/// println!("running on the runtime");
|
/// println!("running on the runtime");
|
||||||
/// Ok(())
|
|
||||||
/// }));
|
/// }));
|
||||||
/// # }
|
/// # }
|
||||||
/// # pub fn main() {}
|
/// # pub fn main() {}
|
||||||
@ -97,7 +97,7 @@ impl Runtime {
|
|||||||
/// is currently at capacity and is unable to spawn a new future.
|
/// is currently at capacity and is unable to spawn a new future.
|
||||||
pub fn spawn<F>(&mut self, future: F) -> &mut Self
|
pub fn spawn<F>(&mut self, future: F) -> &mut Self
|
||||||
where
|
where
|
||||||
F: Future<Item = (), Error = ()> + 'static,
|
F: Future<Output = ()> + 'static,
|
||||||
{
|
{
|
||||||
self.executor.spawn(future);
|
self.executor.spawn(future);
|
||||||
self
|
self
|
||||||
@ -119,14 +119,14 @@ impl Runtime {
|
|||||||
///
|
///
|
||||||
/// The caller is responsible for ensuring that other spawned futures
|
/// The caller is responsible for ensuring that other spawned futures
|
||||||
/// complete execution by calling `block_on` or `run`.
|
/// complete execution by calling `block_on` or `run`.
|
||||||
pub fn block_on<F>(&mut self, f: F) -> Result<F::Item, F::Error>
|
pub fn block_on<F>(&mut self, f: F) -> F::Output
|
||||||
where
|
where
|
||||||
F: Future,
|
F: Future,
|
||||||
{
|
{
|
||||||
self.enter(|executor| {
|
self.enter(|executor| {
|
||||||
// Run the provided future
|
// Run the provided future
|
||||||
let ret = executor.block_on(f);
|
let ret = executor.block_on(f);
|
||||||
ret.map_err(|e| e.into_inner().expect("unexpected execution error"))
|
ret
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,7 +139,7 @@ impl Runtime {
|
|||||||
|
|
||||||
fn enter<F, R>(&mut self, f: F) -> R
|
fn enter<F, R>(&mut self, f: F) -> R
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut current_thread::Entered<Timer<Reactor>>) -> R,
|
F: FnOnce(&mut CurrentThread<Timer<Reactor>>) -> R,
|
||||||
{
|
{
|
||||||
let Runtime {
|
let Runtime {
|
||||||
ref reactor_handle,
|
ref reactor_handle,
|
||||||
@ -149,25 +149,13 @@ impl Runtime {
|
|||||||
..
|
..
|
||||||
} = *self;
|
} = *self;
|
||||||
|
|
||||||
// Binds an executor to this thread
|
// WARN: We do not enter the executor here, since in tokio 0.2 the executor is entered
|
||||||
let mut enter = tokio_executor::enter().expect("Multiple executors at once");
|
// automatically inside its `block_on` and `run` methods
|
||||||
|
tokio_executor::with_default(&mut current_thread::TaskExecutor::current(), || {
|
||||||
// This will set the default handle and timer to use inside the closure
|
tokio_timer::clock::with_default(clock, || {
|
||||||
// and run the future.
|
let _reactor_guard = tokio_net::driver::set_default(reactor_handle);
|
||||||
tokio_reactor::with_default(&reactor_handle, &mut enter, |enter| {
|
let _timer_guard = tokio_timer::set_default(timer_handle);
|
||||||
clock::with_default(clock, enter, |enter| {
|
f(executor)
|
||||||
timer::with_default(&timer_handle, enter, |enter| {
|
|
||||||
// The TaskExecutor is a fake executor that looks into the
|
|
||||||
// current single-threaded executor when used. This is a trick,
|
|
||||||
// because we need two mutable references to the executor (one
|
|
||||||
// to run the provided future, another to install as the default
|
|
||||||
// one). We use the fake one here as the default one.
|
|
||||||
let mut default_executor = current_thread::TaskExecutor::current();
|
|
||||||
tokio_executor::with_default(&mut default_executor, enter, |enter| {
|
|
||||||
let mut executor = executor.enter(enter);
|
|
||||||
f(&mut executor)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,9 @@ use std::cell::RefCell;
|
|||||||
use std::io;
|
use std::io;
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
use futures::sync::mpsc::UnboundedSender;
|
use futures::channel::mpsc::UnboundedSender;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use tokio_current_thread::Handle;
|
use tokio::runtime::current_thread::Handle;
|
||||||
|
|
||||||
use crate::arbiter::{Arbiter, SystemCommand};
|
use crate::arbiter::{Arbiter, SystemCommand};
|
||||||
use crate::builder::{Builder, SystemRunner};
|
use crate::builder::{Builder, SystemRunner};
|
||||||
@ -64,7 +64,7 @@ impl System {
|
|||||||
pub fn run_in_executor<T: Into<String>>(
|
pub fn run_in_executor<T: Into<String>>(
|
||||||
name: T,
|
name: T,
|
||||||
executor: Handle,
|
executor: Handle,
|
||||||
) -> impl Future<Item = (), Error = io::Error> + Send {
|
) -> impl Future<Output = Result<(), io::Error>> + Send {
|
||||||
Self::builder()
|
Self::builder()
|
||||||
.name(name)
|
.name(name)
|
||||||
.build_async(executor)
|
.build_async(executor)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-server-config"
|
name = "actix-server-config"
|
||||||
version = "0.2.0"
|
version = "0.3.0-alpha.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix server config utils"
|
description = "Actix server config utils"
|
||||||
homepage = "https://actix.rs"
|
homepage = "https://actix.rs"
|
||||||
@ -14,25 +14,14 @@ name = "actix_server_config"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
features = ["ssl", "rust-tls", "uds"]
|
features = ["openssl", "rustls"]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
|
openssl = ["tokio-openssl"]
|
||||||
# openssl
|
rustls = ["tokio-rustls"]
|
||||||
ssl = ["tokio-openssl"]
|
|
||||||
|
|
||||||
# rustls
|
|
||||||
rust-tls = ["rustls", "tokio-rustls"]
|
|
||||||
|
|
||||||
# unix domain sockets
|
|
||||||
uds = ["tokio-uds"]
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
futures = "0.1.25"
|
tokio = "0.2.0-alpha.6"
|
||||||
tokio-io = "0.1.12"
|
tokio-openssl = { version = "0.4.0-alpha.6", optional = true }
|
||||||
tokio-tcp = "0.1"
|
tokio-rustls = { version = "0.12.0-alpha.8", optional = true }
|
||||||
tokio-openssl = { version="0.3.0", optional = true }
|
|
||||||
rustls = { version = "0.16.0", optional = true }
|
|
||||||
tokio-rustls = { version = "0.10.0", optional = true }
|
|
||||||
tokio-uds = { version="0.2.5", optional = true }
|
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
|
//! Actix server config utils.
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::{fmt, io, net, time};
|
use std::{fmt, io, net, ops, time};
|
||||||
|
|
||||||
use tokio_io::{AsyncRead, AsyncWrite};
|
use tokio::io::{AsyncRead, AsyncWrite};
|
||||||
use tokio_tcp::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ServerConfig {
|
pub struct ServerConfig {
|
||||||
@ -13,6 +15,7 @@ pub struct ServerConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ServerConfig {
|
impl ServerConfig {
|
||||||
|
#[inline]
|
||||||
pub fn new(addr: SocketAddr) -> Self {
|
pub fn new(addr: SocketAddr) -> Self {
|
||||||
ServerConfig {
|
ServerConfig {
|
||||||
addr,
|
addr,
|
||||||
@ -21,16 +24,19 @@ impl ServerConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the address of the local half of this TCP server socket
|
/// Returns the address of the local half of this TCP server socket
|
||||||
|
#[inline]
|
||||||
pub fn local_addr(&self) -> SocketAddr {
|
pub fn local_addr(&self) -> SocketAddr {
|
||||||
self.addr
|
self.addr
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if connection is secure (tls enabled)
|
/// Returns true if connection is secure (tls enabled)
|
||||||
|
#[inline]
|
||||||
pub fn secure(&self) -> bool {
|
pub fn secure(&self) -> bool {
|
||||||
self.secure.as_ref().get()
|
self.secure.as_ref().get()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set secure flag
|
/// Set secure flag
|
||||||
|
#[inline]
|
||||||
pub fn set_secure(&self) {
|
pub fn set_secure(&self) {
|
||||||
self.secure.as_ref().set(true)
|
self.secure.as_ref().set(true)
|
||||||
}
|
}
|
||||||
@ -114,7 +120,7 @@ impl<T, P> Io<T, P> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, P> std::ops::Deref for Io<T, P> {
|
impl<T, P> ops::Deref for Io<T, P> {
|
||||||
type Target = T;
|
type Target = T;
|
||||||
|
|
||||||
fn deref(&self) -> &T {
|
fn deref(&self) -> &T {
|
||||||
@ -122,14 +128,14 @@ impl<T, P> std::ops::Deref for Io<T, P> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, P> std::ops::DerefMut for Io<T, P> {
|
impl<T, P> ops::DerefMut for Io<T, P> {
|
||||||
fn deref_mut(&mut self) -> &mut T {
|
fn deref_mut(&mut self) -> &mut T {
|
||||||
&mut self.io
|
&mut self.io
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: fmt::Debug, P> fmt::Debug for Io<T, P> {
|
impl<T: fmt::Debug, P> fmt::Debug for Io<T, P> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(f, "Io {{{:?}}}", self.io)
|
write!(f, "Io {{{:?}}}", self.io)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -171,31 +177,31 @@ impl IoStream for TcpStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "ssl"))]
|
#[cfg(feature = "openssl")]
|
||||||
impl<T: IoStream> IoStream for tokio_openssl::SslStream<T> {
|
impl<T: IoStream + Unpin> IoStream for tokio_openssl::SslStream<T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn peer_addr(&self) -> Option<net::SocketAddr> {
|
fn peer_addr(&self) -> Option<net::SocketAddr> {
|
||||||
self.get_ref().get_ref().peer_addr()
|
self.get_ref().peer_addr()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn set_nodelay(&mut self, nodelay: bool) -> io::Result<()> {
|
fn set_nodelay(&mut self, nodelay: bool) -> io::Result<()> {
|
||||||
self.get_mut().get_mut().set_nodelay(nodelay)
|
self.get_mut().set_nodelay(nodelay)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn set_linger(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
|
fn set_linger(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
|
||||||
self.get_mut().get_mut().set_linger(dur)
|
self.get_mut().set_linger(dur)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn set_keepalive(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
|
fn set_keepalive(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
|
||||||
self.get_mut().get_mut().set_keepalive(dur)
|
self.get_mut().set_keepalive(dur)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "rust-tls"))]
|
#[cfg(feature = "rustls")]
|
||||||
impl<T: IoStream> IoStream for tokio_rustls::server::TlsStream<T> {
|
impl<T: IoStream + Unpin> IoStream for tokio_rustls::server::TlsStream<T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn peer_addr(&self) -> Option<net::SocketAddr> {
|
fn peer_addr(&self) -> Option<net::SocketAddr> {
|
||||||
self.get_ref().0.peer_addr()
|
self.get_ref().0.peer_addr()
|
||||||
@ -217,8 +223,8 @@ impl<T: IoStream> IoStream for tokio_rustls::server::TlsStream<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(unix, feature = "uds"))]
|
#[cfg(unix)]
|
||||||
impl IoStream for tokio_uds::UnixStream {
|
impl IoStream for tokio::net::UnixStream {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn peer_addr(&self) -> Option<net::SocketAddr> {
|
fn peer_addr(&self) -> Option<net::SocketAddr> {
|
||||||
None
|
None
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
* Update `rustls` to 0.16
|
* Update `rustls` to 0.16
|
||||||
* Minimum required Rust version upped to 1.37.0
|
* Minimum required Rust version upped to 1.37.0
|
||||||
|
|
||||||
|
|
||||||
## [0.6.1] - 2019-09-25
|
## [0.6.1] - 2019-09-25
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-server"
|
name = "actix-server"
|
||||||
version = "0.7.0"
|
version = "0.8.0-alpha.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix server - General purpose tcp server"
|
description = "Actix server - General purpose tcp server"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -14,7 +14,7 @@ edition = "2018"
|
|||||||
workspace = ".."
|
workspace = ".."
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
features = ["ssl", "tls", "rust-tls", "uds"]
|
features = ["nativetls", "openssl", "rustls", "uds"]
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "actix_server"
|
name = "actix_server"
|
||||||
@ -22,55 +22,47 @@ path = "src/lib.rs"
|
|||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
|
nativetls = ["native-tls", "tokio-tls"]
|
||||||
# tls
|
openssl = ["open-ssl", "tokio-openssl", "actix-server-config/openssl"]
|
||||||
tls = ["native-tls"]
|
rustls = ["rust-tls", "tokio-rustls", "webpki", "webpki-roots", "actix-server-config/rustls"]
|
||||||
|
|
||||||
# openssl
|
|
||||||
ssl = ["openssl", "tokio-openssl", "actix-server-config/ssl"]
|
|
||||||
|
|
||||||
# rustls
|
|
||||||
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots", "actix-server-config/rust-tls"]
|
|
||||||
|
|
||||||
# uds
|
|
||||||
uds = ["mio-uds", "tokio-uds", "actix-server-config/uds"]
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "0.2.2"
|
actix-rt = "1.0.0-alpha.1"
|
||||||
actix-service = "0.4.1"
|
actix-service = "1.0.0-alpha.1"
|
||||||
actix-server-config = "0.2.0"
|
actix-server-config = "0.3.0-alpha.1"
|
||||||
|
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
num_cpus = "1.0"
|
num_cpus = "1.0"
|
||||||
|
pin-project = "0.4.5"
|
||||||
mio = "0.6.19"
|
mio = "0.6.19"
|
||||||
net2 = "0.2"
|
net2 = "0.2"
|
||||||
futures = "0.1"
|
futures = "0.3.1"
|
||||||
slab = "0.4"
|
slab = "0.4"
|
||||||
tokio-io = "0.1"
|
|
||||||
tokio-tcp = "0.1"
|
tokio = "0.2.0-alpha.6"
|
||||||
tokio-timer = "0.2.8"
|
tokio-io = "0.2.0-alpha.6"
|
||||||
tokio-reactor = "0.1"
|
tokio-net = { version = "0.2.0-alpha.6", features = ["signal"] }
|
||||||
tokio-signal = "0.2"
|
tokio-timer = "0.3.0-alpha.6"
|
||||||
|
|
||||||
# unix domain sockets
|
# unix domain sockets
|
||||||
mio-uds = { version="0.6.7", optional = true }
|
mio-uds = { version = "0.6.7", optional = true }
|
||||||
tokio-uds = { version="0.2.5", optional = true }
|
|
||||||
|
|
||||||
# native-tls
|
# nativetls
|
||||||
native-tls = { version="0.2", optional = true }
|
native-tls = { version = "0.2", optional = true }
|
||||||
|
tokio-tls = { version = "0.3.0-alpha.6", optional = true }
|
||||||
|
|
||||||
# openssl
|
# openssl
|
||||||
openssl = { version="0.10", optional = true }
|
open-ssl = { version = "0.10", package = "openssl", optional = true }
|
||||||
tokio-openssl = { version="0.3", optional = true }
|
tokio-openssl = { version = "0.4.0-alpha.6", optional = true }
|
||||||
|
|
||||||
# rustls
|
# rustls
|
||||||
rustls = { version = "0.16.0", optional = true }
|
rust-tls = { version = "0.16.0", package = "rustls", optional = true }
|
||||||
tokio-rustls = { version = "0.10.0", optional = true }
|
# tokio-rustls = { version = "0.12.0-alpha.2", optional = true }
|
||||||
|
tokio-rustls = { git = "https://github.com/quininer/tokio-rustls.git", branch = "tokio-0.2", optional = true }
|
||||||
webpki = { version = "0.21", optional = true }
|
webpki = { version = "0.21", optional = true }
|
||||||
webpki-roots = { version = "0.17", optional = true }
|
webpki-roots = { version = "0.17", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
actix-codec = "0.1.2"
|
actix-codec = "0.2.0-alpha.1"
|
||||||
env_logger = "0.6"
|
env_logger = "0.6"
|
||||||
|
@ -3,10 +3,10 @@ use std::time::{Duration, Instant};
|
|||||||
use std::{io, thread};
|
use std::{io, thread};
|
||||||
|
|
||||||
use actix_rt::System;
|
use actix_rt::System;
|
||||||
use futures::future::{lazy, Future};
|
use futures::FutureExt;
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use slab::Slab;
|
use slab::Slab;
|
||||||
use tokio_timer::Delay;
|
use tokio_timer::delay;
|
||||||
|
|
||||||
use crate::server::Server;
|
use crate::server::Server;
|
||||||
use crate::socket::{SocketAddr, SocketListener, StdListener};
|
use crate::socket::{SocketAddr, SocketListener, StdListener};
|
||||||
@ -370,7 +370,7 @@ impl Accept {
|
|||||||
match self.workers[self.next].send(msg) {
|
match self.workers[self.next].send(msg) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(tmp) => {
|
Err(tmp) => {
|
||||||
self.srv.worker_died(self.workers[self.next].idx);
|
self.srv.worker_faulted(self.workers[self.next].idx);
|
||||||
msg = tmp;
|
msg = tmp;
|
||||||
self.workers.swap_remove(self.next);
|
self.workers.swap_remove(self.next);
|
||||||
if self.workers.is_empty() {
|
if self.workers.is_empty() {
|
||||||
@ -396,7 +396,7 @@ impl Accept {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Err(tmp) => {
|
Err(tmp) => {
|
||||||
self.srv.worker_died(self.workers[self.next].idx);
|
self.srv.worker_faulted(self.workers[self.next].idx);
|
||||||
msg = tmp;
|
msg = tmp;
|
||||||
self.workers.swap_remove(self.next);
|
self.workers.swap_remove(self.next);
|
||||||
if self.workers.is_empty() {
|
if self.workers.is_empty() {
|
||||||
@ -440,14 +440,13 @@ impl Accept {
|
|||||||
info.timeout = Some(Instant::now() + Duration::from_millis(500));
|
info.timeout = Some(Instant::now() + Duration::from_millis(500));
|
||||||
|
|
||||||
let r = self.timer.1.clone();
|
let r = self.timer.1.clone();
|
||||||
System::current().arbiter().send(lazy(move || {
|
System::current().arbiter().send(
|
||||||
Delay::new(Instant::now() + Duration::from_millis(510))
|
async move {
|
||||||
.map_err(|_| ())
|
delay(Instant::now() + Duration::from_millis(510)).await;
|
||||||
.and_then(move |_| {
|
|
||||||
let _ = r.set_readiness(mio::Ready::readable());
|
let _ = r.set_readiness(mio::Ready::readable());
|
||||||
Ok(())
|
}
|
||||||
})
|
.boxed(),
|
||||||
}));
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,24 @@
|
|||||||
use std::time::Duration;
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
use std::{io, mem, net};
|
use std::{io, mem, net};
|
||||||
|
|
||||||
use actix_rt::{spawn, Arbiter, System};
|
use actix_rt::{spawn, Arbiter, System};
|
||||||
use futures::future::{lazy, ok};
|
use futures::channel::mpsc::{unbounded, UnboundedReceiver};
|
||||||
use futures::stream::futures_unordered;
|
use futures::future::ready;
|
||||||
use futures::sync::mpsc::{unbounded, UnboundedReceiver};
|
use futures::stream::FuturesUnordered;
|
||||||
use futures::{Async, Future, Poll, Stream};
|
use futures::{ready, Future, FutureExt, Stream, StreamExt};
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use net2::TcpBuilder;
|
use net2::TcpBuilder;
|
||||||
use num_cpus;
|
use num_cpus;
|
||||||
use tokio_tcp::TcpStream;
|
use tokio_net::tcp::TcpStream;
|
||||||
use tokio_timer::sleep;
|
use tokio_timer::delay;
|
||||||
|
|
||||||
use crate::accept::{AcceptLoop, AcceptNotify, Command};
|
use crate::accept::{AcceptLoop, AcceptNotify, Command};
|
||||||
use crate::config::{ConfiguredService, ServiceConfig};
|
use crate::config::{ConfiguredService, ServiceConfig};
|
||||||
use crate::server::{Server, ServerCommand};
|
use crate::server::{Server, ServerCommand};
|
||||||
use crate::services::{InternalServiceFactory, ServiceFactory, StreamNewService};
|
use crate::service::{InternalServiceFactory, ServiceFactory, StreamNewService};
|
||||||
use crate::signals::{Signal, Signals};
|
// use crate::signals::{Signal, Signals};
|
||||||
use crate::socket::StdListener;
|
use crate::socket::StdListener;
|
||||||
use crate::worker::{self, Worker, WorkerAvailability, WorkerClient};
|
use crate::worker::{self, Worker, WorkerAvailability, WorkerClient};
|
||||||
use crate::{ssl, Token};
|
use crate::{ssl, Token};
|
||||||
@ -301,7 +303,7 @@ impl ServerBuilder {
|
|||||||
|
|
||||||
// handle signals
|
// handle signals
|
||||||
if !self.no_signals {
|
if !self.no_signals {
|
||||||
Signals::start(self.server.clone());
|
// Signals::start(self.server.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
// start http server actor
|
// start http server actor
|
||||||
@ -320,10 +322,12 @@ impl ServerBuilder {
|
|||||||
let services: Vec<Box<dyn InternalServiceFactory>> =
|
let services: Vec<Box<dyn InternalServiceFactory>> =
|
||||||
self.services.iter().map(|v| v.clone_factory()).collect();
|
self.services.iter().map(|v| v.clone_factory()).collect();
|
||||||
|
|
||||||
Arbiter::new().send(lazy(move || {
|
Arbiter::new().send(
|
||||||
|
async move {
|
||||||
Worker::start(rx1, rx2, services, avail, timeout);
|
Worker::start(rx1, rx2, services, avail, timeout);
|
||||||
Ok::<_, ()>(())
|
}
|
||||||
}));
|
.boxed(),
|
||||||
|
);
|
||||||
|
|
||||||
worker
|
worker
|
||||||
}
|
}
|
||||||
@ -338,37 +342,37 @@ impl ServerBuilder {
|
|||||||
self.accept.send(Command::Resume);
|
self.accept.send(Command::Resume);
|
||||||
let _ = tx.send(());
|
let _ = tx.send(());
|
||||||
}
|
}
|
||||||
ServerCommand::Signal(sig) => {
|
// ServerCommand::Signal(sig) => {
|
||||||
// Signals support
|
// Signals support
|
||||||
// Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and stop actix system
|
// Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and stop actix system
|
||||||
match sig {
|
// match sig {
|
||||||
Signal::Int => {
|
// Signal::Int => {
|
||||||
info!("SIGINT received, exiting");
|
// info!("SIGINT received, exiting");
|
||||||
self.exit = true;
|
// self.exit = true;
|
||||||
self.handle_cmd(ServerCommand::Stop {
|
// self.handle_cmd(ServerCommand::Stop {
|
||||||
graceful: false,
|
// graceful: false,
|
||||||
completion: None,
|
// completion: None,
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
Signal::Term => {
|
// Signal::Term => {
|
||||||
info!("SIGTERM received, stopping");
|
// info!("SIGTERM received, stopping");
|
||||||
self.exit = true;
|
// self.exit = true;
|
||||||
self.handle_cmd(ServerCommand::Stop {
|
// self.handle_cmd(ServerCommand::Stop {
|
||||||
graceful: true,
|
// graceful: true,
|
||||||
completion: None,
|
// completion: None,
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
Signal::Quit => {
|
// Signal::Quit => {
|
||||||
info!("SIGQUIT received, exiting");
|
// info!("SIGQUIT received, exiting");
|
||||||
self.exit = true;
|
// self.exit = true;
|
||||||
self.handle_cmd(ServerCommand::Stop {
|
// self.handle_cmd(ServerCommand::Stop {
|
||||||
graceful: false,
|
// graceful: false,
|
||||||
completion: None,
|
// completion: None,
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
_ => (),
|
// _ => (),
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
ServerCommand::Stop {
|
ServerCommand::Stop {
|
||||||
graceful,
|
graceful,
|
||||||
completion,
|
completion,
|
||||||
@ -381,39 +385,44 @@ impl ServerBuilder {
|
|||||||
// stop workers
|
// stop workers
|
||||||
if !self.workers.is_empty() && graceful {
|
if !self.workers.is_empty() && graceful {
|
||||||
spawn(
|
spawn(
|
||||||
futures_unordered(
|
|
||||||
self.workers
|
self.workers
|
||||||
.iter()
|
.iter()
|
||||||
.map(move |worker| worker.1.stop(graceful)),
|
.map(move |worker| worker.1.stop(graceful))
|
||||||
)
|
.collect::<FuturesUnordered<_>>()
|
||||||
.collect()
|
.collect::<Vec<_>>()
|
||||||
.then(move |_| {
|
.then(move |_| {
|
||||||
if let Some(tx) = completion {
|
if let Some(tx) = completion {
|
||||||
let _ = tx.send(());
|
let _ = tx.send(());
|
||||||
}
|
}
|
||||||
if exit {
|
if exit {
|
||||||
spawn(sleep(Duration::from_millis(300)).then(|_| {
|
spawn(
|
||||||
|
async {
|
||||||
|
delay(Instant::now() + Duration::from_millis(300))
|
||||||
|
.await;
|
||||||
System::current().stop();
|
System::current().stop();
|
||||||
ok(())
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
ok(())
|
.boxed(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
ready(())
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
// we need to stop system if server was spawned
|
// we need to stop system if server was spawned
|
||||||
if self.exit {
|
if self.exit {
|
||||||
spawn(sleep(Duration::from_millis(300)).then(|_| {
|
spawn(
|
||||||
|
delay(Instant::now() + Duration::from_millis(300)).then(|_| {
|
||||||
System::current().stop();
|
System::current().stop();
|
||||||
ok(())
|
ready(())
|
||||||
}));
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if let Some(tx) = completion {
|
if let Some(tx) = completion {
|
||||||
let _ = tx.send(());
|
let _ = tx.send(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ServerCommand::WorkerDied(idx) => {
|
ServerCommand::WorkerFaulted(idx) => {
|
||||||
let mut found = false;
|
let mut found = false;
|
||||||
for i in 0..self.workers.len() {
|
for i in 0..self.workers.len() {
|
||||||
if self.workers[i].0 == idx {
|
if self.workers[i].0 == idx {
|
||||||
@ -447,15 +456,15 @@ impl ServerBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Future for ServerBuilder {
|
impl Future for ServerBuilder {
|
||||||
type Item = ();
|
type Output = ();
|
||||||
type Error = ();
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
loop {
|
loop {
|
||||||
match self.cmd.poll() {
|
match ready!(Pin::new(&mut self.cmd).poll_next(cx)) {
|
||||||
Ok(Async::Ready(None)) | Err(_) => return Ok(Async::Ready(())),
|
Some(it) => self.as_mut().get_mut().handle_cmd(it),
|
||||||
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
None => {
|
||||||
Ok(Async::Ready(Some(item))) => self.handle_cmd(item),
|
return Poll::Pending;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,18 +2,17 @@ use std::collections::HashMap;
|
|||||||
use std::{fmt, io, net};
|
use std::{fmt, io, net};
|
||||||
|
|
||||||
use actix_server_config::{Io, ServerConfig};
|
use actix_server_config::{Io, ServerConfig};
|
||||||
use actix_service::{IntoNewService, NewService};
|
use actix_service as actix;
|
||||||
use futures::future::{join_all, Future};
|
use futures::future::{Future, FutureExt, LocalBoxFuture};
|
||||||
use log::error;
|
use log::error;
|
||||||
use tokio_tcp::TcpStream;
|
use tokio_net::tcp::TcpStream;
|
||||||
|
|
||||||
use crate::counter::CounterGuard;
|
|
||||||
|
|
||||||
use super::builder::bind_addr;
|
use super::builder::bind_addr;
|
||||||
use super::services::{
|
use super::service::{
|
||||||
BoxedServerService, InternalServiceFactory, ServerMessage, StreamService,
|
BoxedServerService, InternalServiceFactory, ServerMessage, StreamService,
|
||||||
};
|
};
|
||||||
use super::Token;
|
use super::Token;
|
||||||
|
use crate::counter::CounterGuard;
|
||||||
|
|
||||||
pub struct ServiceConfig {
|
pub struct ServiceConfig {
|
||||||
pub(crate) services: Vec<(String, net::TcpListener)>,
|
pub(crate) services: Vec<(String, net::TcpListener)>,
|
||||||
@ -108,50 +107,39 @@ impl InternalServiceFactory for ConfiguredService {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create(&self) -> Box<dyn Future<Item = Vec<(Token, BoxedServerService)>, Error = ()>> {
|
fn create(&self) -> LocalBoxFuture<'static, Result<Vec<(Token, BoxedServerService)>, ()>> {
|
||||||
// configure services
|
// configure services
|
||||||
let mut rt = ServiceRuntime::new(self.services.clone());
|
let mut rt = ServiceRuntime::new(self.services.clone());
|
||||||
self.rt.configure(&mut rt);
|
self.rt.configure(&mut rt);
|
||||||
rt.validate();
|
rt.validate();
|
||||||
|
|
||||||
let services = rt.services;
|
|
||||||
|
|
||||||
// on start futures
|
|
||||||
if rt.onstart.is_empty() {
|
|
||||||
// construct services
|
|
||||||
let mut fut = Vec::new();
|
|
||||||
for (token, ns) in services {
|
|
||||||
let config = ServerConfig::new(self.names[&token].1);
|
|
||||||
fut.push(ns.new_service(&config).map(move |service| (token, service)));
|
|
||||||
}
|
|
||||||
|
|
||||||
Box::new(join_all(fut).map_err(|e| {
|
|
||||||
error!("Can not construct service: {:?}", e);
|
|
||||||
}))
|
|
||||||
} else {
|
|
||||||
let names = self.names.clone();
|
let names = self.names.clone();
|
||||||
|
|
||||||
// run onstart future and then construct services
|
|
||||||
Box::new(
|
|
||||||
join_all(rt.onstart)
|
|
||||||
.map_err(|e| {
|
|
||||||
error!("Can not construct service: {:?}", e);
|
|
||||||
})
|
|
||||||
.and_then(move |_| {
|
|
||||||
// construct services
|
// construct services
|
||||||
let mut fut = Vec::new();
|
async move {
|
||||||
for (token, ns) in services {
|
let services = rt.services;
|
||||||
|
// TODO: Proper error handling here
|
||||||
|
for f in rt.onstart.into_iter() {
|
||||||
|
f.await;
|
||||||
|
}
|
||||||
|
let mut res = vec![];
|
||||||
|
for (token, ns) in services.into_iter() {
|
||||||
let config = ServerConfig::new(names[&token].1);
|
let config = ServerConfig::new(names[&token].1);
|
||||||
fut.push(
|
|
||||||
ns.new_service(&config).map(move |service| (token, service)),
|
let newserv = ns.new_service(&config);
|
||||||
);
|
match newserv.await {
|
||||||
|
Ok(serv) => {
|
||||||
|
res.push((token, serv));
|
||||||
}
|
}
|
||||||
join_all(fut).map_err(|e| {
|
Err(e) => {
|
||||||
error!("Can not construct service: {:?}", e);
|
error!("Can not construct service {:?}", e);
|
||||||
})
|
return Err(e);
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return Ok(res);
|
||||||
|
}
|
||||||
|
.boxed_local()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,7 +169,7 @@ fn not_configured(_: &mut ServiceRuntime) {
|
|||||||
pub struct ServiceRuntime {
|
pub struct ServiceRuntime {
|
||||||
names: HashMap<String, Token>,
|
names: HashMap<String, Token>,
|
||||||
services: HashMap<Token, BoxedNewService>,
|
services: HashMap<Token, BoxedNewService>,
|
||||||
onstart: Vec<Box<dyn Future<Item = (), Error = ()>>>,
|
onstart: Vec<LocalBoxFuture<'static, ()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServiceRuntime {
|
impl ServiceRuntime {
|
||||||
@ -207,8 +195,8 @@ impl ServiceRuntime {
|
|||||||
/// *ServiceConfig::bind()* or *ServiceConfig::listen()* methods.
|
/// *ServiceConfig::bind()* or *ServiceConfig::listen()* methods.
|
||||||
pub fn service<T, F>(&mut self, name: &str, service: F)
|
pub fn service<T, F>(&mut self, name: &str, service: F)
|
||||||
where
|
where
|
||||||
F: IntoNewService<T>,
|
F: actix::IntoServiceFactory<T>,
|
||||||
T: NewService<Config = ServerConfig, Request = Io<TcpStream>> + 'static,
|
T: actix::ServiceFactory<Config = ServerConfig, Request = Io<TcpStream>> + 'static,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
T::Service: 'static,
|
T::Service: 'static,
|
||||||
T::InitError: fmt::Debug,
|
T::InitError: fmt::Debug,
|
||||||
@ -218,7 +206,7 @@ impl ServiceRuntime {
|
|||||||
self.services.insert(
|
self.services.insert(
|
||||||
token.clone(),
|
token.clone(),
|
||||||
Box::new(ServiceFactory {
|
Box::new(ServiceFactory {
|
||||||
inner: service.into_new_service(),
|
inner: service.into_factory(),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
@ -229,21 +217,21 @@ impl ServiceRuntime {
|
|||||||
/// Execute future before services initialization.
|
/// Execute future before services initialization.
|
||||||
pub fn on_start<F>(&mut self, fut: F)
|
pub fn on_start<F>(&mut self, fut: F)
|
||||||
where
|
where
|
||||||
F: Future<Item = (), Error = ()> + 'static,
|
F: Future<Output = ()> + 'static,
|
||||||
{
|
{
|
||||||
self.onstart.push(Box::new(fut))
|
self.onstart.push(fut.boxed_local())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type BoxedNewService = Box<
|
type BoxedNewService = Box<
|
||||||
dyn NewService<
|
dyn actix::ServiceFactory<
|
||||||
Request = (Option<CounterGuard>, ServerMessage),
|
Request = (Option<CounterGuard>, ServerMessage),
|
||||||
Response = (),
|
Response = (),
|
||||||
Error = (),
|
Error = (),
|
||||||
InitError = (),
|
InitError = (),
|
||||||
Config = ServerConfig,
|
Config = ServerConfig,
|
||||||
Service = BoxedServerService,
|
Service = BoxedServerService,
|
||||||
Future = Box<dyn Future<Item = BoxedServerService, Error = ()>>,
|
Future = LocalBoxFuture<'static, Result<BoxedServerService, ()>>,
|
||||||
>,
|
>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
@ -251,9 +239,9 @@ struct ServiceFactory<T> {
|
|||||||
inner: T,
|
inner: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> NewService for ServiceFactory<T>
|
impl<T> actix::ServiceFactory for ServiceFactory<T>
|
||||||
where
|
where
|
||||||
T: NewService<Config = ServerConfig, Request = Io<TcpStream>>,
|
T: actix::ServiceFactory<Config = ServerConfig, Request = Io<TcpStream>>,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
T::Service: 'static,
|
T::Service: 'static,
|
||||||
T::Error: 'static,
|
T::Error: 'static,
|
||||||
@ -265,12 +253,19 @@ where
|
|||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Config = ServerConfig;
|
type Config = ServerConfig;
|
||||||
type Service = BoxedServerService;
|
type Service = BoxedServerService;
|
||||||
type Future = Box<dyn Future<Item = BoxedServerService, Error = ()>>;
|
type Future = LocalBoxFuture<'static, Result<BoxedServerService, ()>>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
|
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
|
||||||
Box::new(self.inner.new_service(cfg).map_err(|_| ()).map(|s| {
|
let fut = self.inner.new_service(cfg);
|
||||||
let service: BoxedServerService = Box::new(StreamService::new(s));
|
async move {
|
||||||
service
|
return match fut.await {
|
||||||
}))
|
Ok(s) => Ok(Box::new(StreamService::new(s)) as BoxedServerService),
|
||||||
|
Err(e) => {
|
||||||
|
error!("Can not construct service: {:?}", e);
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
.boxed_local()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use futures::task::AtomicTask;
|
use futures::task::AtomicWaker;
|
||||||
|
use std::task;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
/// Simple counter with ability to notify task on reaching specific number
|
/// Simple counter with ability to notify task on reaching specific number
|
||||||
@ -13,7 +14,7 @@ pub struct Counter(Rc<CounterInner>);
|
|||||||
struct CounterInner {
|
struct CounterInner {
|
||||||
count: Cell<usize>,
|
count: Cell<usize>,
|
||||||
capacity: usize,
|
capacity: usize,
|
||||||
task: AtomicTask,
|
task: AtomicWaker,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Counter {
|
impl Counter {
|
||||||
@ -22,7 +23,7 @@ impl Counter {
|
|||||||
Counter(Rc::new(CounterInner {
|
Counter(Rc::new(CounterInner {
|
||||||
capacity,
|
capacity,
|
||||||
count: Cell::new(0),
|
count: Cell::new(0),
|
||||||
task: AtomicTask::new(),
|
task: AtomicWaker::new(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,8 +32,8 @@ impl Counter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Check if counter is not at capacity
|
/// Check if counter is not at capacity
|
||||||
pub fn available(&self) -> bool {
|
pub fn available(&self, cx: &mut task::Context) -> bool {
|
||||||
self.0.available()
|
self.0.available(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get total number of acquired counts
|
/// Get total number of acquired counts
|
||||||
@ -66,14 +67,14 @@ impl CounterInner {
|
|||||||
let num = self.count.get();
|
let num = self.count.get();
|
||||||
self.count.set(num - 1);
|
self.count.set(num - 1);
|
||||||
if num == self.capacity {
|
if num == self.capacity {
|
||||||
self.task.notify();
|
self.task.wake();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn available(&self) -> bool {
|
fn available(&self, cx: &mut task::Context) -> bool {
|
||||||
let avail = self.count.get() < self.capacity;
|
let avail = self.count.get() < self.capacity;
|
||||||
if !avail {
|
if !avail {
|
||||||
self.task.register();
|
self.task.register(cx.waker());
|
||||||
}
|
}
|
||||||
avail
|
avail
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,8 @@ mod builder;
|
|||||||
mod config;
|
mod config;
|
||||||
mod counter;
|
mod counter;
|
||||||
mod server;
|
mod server;
|
||||||
mod services;
|
mod service;
|
||||||
mod signals;
|
// mod signals;
|
||||||
mod socket;
|
mod socket;
|
||||||
pub mod ssl;
|
pub mod ssl;
|
||||||
mod worker;
|
mod worker;
|
||||||
@ -16,14 +16,11 @@ pub use actix_server_config::{Io, IoStream, Protocol, ServerConfig};
|
|||||||
pub use self::builder::ServerBuilder;
|
pub use self::builder::ServerBuilder;
|
||||||
pub use self::config::{ServiceConfig, ServiceRuntime};
|
pub use self::config::{ServiceConfig, ServiceRuntime};
|
||||||
pub use self::server::Server;
|
pub use self::server::Server;
|
||||||
pub use self::services::ServiceFactory;
|
pub use self::service::ServiceFactory;
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub use self::socket::FromStream;
|
pub use self::socket::FromStream;
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub use self::services::ServiceFactory as StreamServiceFactory;
|
|
||||||
|
|
||||||
/// Socket id token
|
/// Socket id token
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
pub(crate) struct Token(usize);
|
pub(crate) struct Token(usize);
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
use futures::sync::mpsc::UnboundedSender;
|
use futures::channel::mpsc::UnboundedSender;
|
||||||
use futures::sync::oneshot;
|
use futures::channel::oneshot;
|
||||||
use futures::Future;
|
use futures::{Future, TryFutureExt};
|
||||||
|
|
||||||
use crate::builder::ServerBuilder;
|
use crate::builder::ServerBuilder;
|
||||||
use crate::signals::Signal;
|
// use crate::signals::Signal;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) enum ServerCommand {
|
pub(crate) enum ServerCommand {
|
||||||
WorkerDied(usize),
|
WorkerFaulted(usize),
|
||||||
Pause(oneshot::Sender<()>),
|
Pause(oneshot::Sender<()>),
|
||||||
Resume(oneshot::Sender<()>),
|
Resume(oneshot::Sender<()>),
|
||||||
Signal(Signal),
|
// Signal(Signal),
|
||||||
/// Whether to try and shut down gracefully
|
/// Whether to try and shut down gracefully
|
||||||
Stop {
|
Stop {
|
||||||
graceful: bool,
|
graceful: bool,
|
||||||
@ -31,26 +31,26 @@ impl Server {
|
|||||||
ServerBuilder::default()
|
ServerBuilder::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn signal(&self, sig: Signal) {
|
// pub(crate) fn signal(&self, sig: Signal) {
|
||||||
let _ = self.0.unbounded_send(ServerCommand::Signal(sig));
|
// let _ = self.0.unbounded_send(ServerCommand::Signal(sig));
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub(crate) fn worker_died(&self, idx: usize) {
|
pub(crate) fn worker_faulted(&self, idx: usize) {
|
||||||
let _ = self.0.unbounded_send(ServerCommand::WorkerDied(idx));
|
let _ = self.0.unbounded_send(ServerCommand::WorkerFaulted(idx));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pause accepting incoming connections
|
/// Pause accepting incoming connections
|
||||||
///
|
///
|
||||||
/// If socket contains some pending connection, they might be dropped.
|
/// If socket contains some pending connection, they might be dropped.
|
||||||
/// All opened connection remains active.
|
/// All opened connection remains active.
|
||||||
pub fn pause(&self) -> impl Future<Item = (), Error = ()> {
|
pub fn pause(&self) -> impl Future<Output = Result<(), ()>> {
|
||||||
let (tx, rx) = oneshot::channel();
|
let (tx, rx) = oneshot::channel();
|
||||||
let _ = self.0.unbounded_send(ServerCommand::Pause(tx));
|
let _ = self.0.unbounded_send(ServerCommand::Pause(tx));
|
||||||
rx.map_err(|_| ())
|
rx.map_err(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resume accepting incoming connections
|
/// Resume accepting incoming connections
|
||||||
pub fn resume(&self) -> impl Future<Item = (), Error = ()> {
|
pub fn resume(&self) -> impl Future<Output = Result<(), ()>> {
|
||||||
let (tx, rx) = oneshot::channel();
|
let (tx, rx) = oneshot::channel();
|
||||||
let _ = self.0.unbounded_send(ServerCommand::Resume(tx));
|
let _ = self.0.unbounded_send(ServerCommand::Resume(tx));
|
||||||
rx.map_err(|_| ())
|
rx.map_err(|_| ())
|
||||||
@ -59,7 +59,7 @@ impl Server {
|
|||||||
/// Stop incoming connection processing, stop all workers and exit.
|
/// Stop incoming connection processing, stop all workers and exit.
|
||||||
///
|
///
|
||||||
/// If server starts with `spawn()` method, then spawned thread get terminated.
|
/// If server starts with `spawn()` method, then spawned thread get terminated.
|
||||||
pub fn stop(&self, graceful: bool) -> impl Future<Item = (), Error = ()> {
|
pub fn stop(&self, graceful: bool) -> impl Future<Output = Result<(), ()>> {
|
||||||
let (tx, rx) = oneshot::channel();
|
let (tx, rx) = oneshot::channel();
|
||||||
let _ = self.0.unbounded_send(ServerCommand::Stop {
|
let _ = self.0.unbounded_send(ServerCommand::Stop {
|
||||||
graceful,
|
graceful,
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use actix_rt::spawn;
|
use actix_rt::spawn;
|
||||||
use actix_server_config::{Io, ServerConfig};
|
use actix_server_config::{Io, ServerConfig};
|
||||||
use actix_service::{NewService, Service};
|
use actix_service::{self as actix, Service, ServiceFactory as ActixServiceFactory};
|
||||||
use futures::future::{err, ok, FutureResult};
|
use futures::future::{err, ok, LocalBoxFuture, Ready};
|
||||||
use futures::{Future, Poll};
|
use futures::{FutureExt, TryFutureExt};
|
||||||
use log::error;
|
use log::error;
|
||||||
|
|
||||||
use super::Token;
|
use super::Token;
|
||||||
@ -24,7 +25,7 @@ pub(crate) enum ServerMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait ServiceFactory<Stream: FromStream>: Send + Clone + 'static {
|
pub trait ServiceFactory<Stream: FromStream>: Send + Clone + 'static {
|
||||||
type NewService: NewService<Config = ServerConfig, Request = Io<Stream>>;
|
type NewService: actix::ServiceFactory<Config = ServerConfig, Request = Io<Stream>>;
|
||||||
|
|
||||||
fn create(&self) -> Self::NewService;
|
fn create(&self) -> Self::NewService;
|
||||||
}
|
}
|
||||||
@ -34,7 +35,7 @@ pub(crate) trait InternalServiceFactory: Send {
|
|||||||
|
|
||||||
fn clone_factory(&self) -> Box<dyn InternalServiceFactory>;
|
fn clone_factory(&self) -> Box<dyn InternalServiceFactory>;
|
||||||
|
|
||||||
fn create(&self) -> Box<dyn Future<Item = Vec<(Token, BoxedServerService)>, Error = ()>>;
|
fn create(&self) -> LocalBoxFuture<'static, Result<Vec<(Token, BoxedServerService)>, ()>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) type BoxedServerService = Box<
|
pub(crate) type BoxedServerService = Box<
|
||||||
@ -42,7 +43,7 @@ pub(crate) type BoxedServerService = Box<
|
|||||||
Request = (Option<CounterGuard>, ServerMessage),
|
Request = (Option<CounterGuard>, ServerMessage),
|
||||||
Response = (),
|
Response = (),
|
||||||
Error = (),
|
Error = (),
|
||||||
Future = FutureResult<(), ()>,
|
Future = Ready<Result<(), ()>>,
|
||||||
>,
|
>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
@ -66,10 +67,10 @@ where
|
|||||||
type Request = (Option<CounterGuard>, ServerMessage);
|
type Request = (Option<CounterGuard>, ServerMessage);
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<(), ()>;
|
type Future = Ready<Result<(), ()>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.service.poll_ready().map_err(|_| ())
|
self.service.poll_ready(ctx).map_err(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, (guard, req): (Option<CounterGuard>, ServerMessage)) -> Self::Future {
|
fn call(&mut self, (guard, req): (Option<CounterGuard>, ServerMessage)) -> Self::Future {
|
||||||
@ -80,10 +81,14 @@ where
|
|||||||
});
|
});
|
||||||
|
|
||||||
if let Ok(stream) = stream {
|
if let Ok(stream) = stream {
|
||||||
spawn(self.service.call(Io::new(stream)).then(move |res| {
|
let f = self.service.call(Io::new(stream));
|
||||||
|
spawn(
|
||||||
|
async move {
|
||||||
|
let _ = f.await;
|
||||||
drop(guard);
|
drop(guard);
|
||||||
res.map_err(|_| ()).map(|_| ())
|
}
|
||||||
}));
|
.boxed_local(),
|
||||||
|
);
|
||||||
ok(())
|
ok(())
|
||||||
} else {
|
} else {
|
||||||
err(())
|
err(())
|
||||||
@ -142,19 +147,19 @@ where
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create(&self) -> Box<dyn Future<Item = Vec<(Token, BoxedServerService)>, Error = ()>> {
|
fn create(&self) -> LocalBoxFuture<'static, Result<Vec<(Token, BoxedServerService)>, ()>> {
|
||||||
let token = self.token;
|
let token = self.token;
|
||||||
let config = ServerConfig::new(self.addr);
|
let config = ServerConfig::new(self.addr);
|
||||||
Box::new(
|
|
||||||
self.inner
|
self.inner
|
||||||
.create()
|
.create()
|
||||||
.new_service(&config)
|
.new_service(&config)
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
.map(move |inner| {
|
.map_ok(move |inner| {
|
||||||
let service: BoxedServerService = Box::new(StreamService::new(inner));
|
let service: BoxedServerService = Box::new(StreamService::new(inner));
|
||||||
vec![(token, service)]
|
vec![(token, service)]
|
||||||
}),
|
})
|
||||||
)
|
.boxed_local()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,7 +172,7 @@ impl InternalServiceFactory for Box<dyn InternalServiceFactory> {
|
|||||||
self.as_ref().clone_factory()
|
self.as_ref().clone_factory()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create(&self) -> Box<dyn Future<Item = Vec<(Token, BoxedServerService)>, Error = ()>> {
|
fn create(&self) -> LocalBoxFuture<'static, Result<Vec<(Token, BoxedServerService)>, ()>> {
|
||||||
self.as_ref().create()
|
self.as_ref().create()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -175,7 +180,7 @@ impl InternalServiceFactory for Box<dyn InternalServiceFactory> {
|
|||||||
impl<F, T, I> ServiceFactory<I> for F
|
impl<F, T, I> ServiceFactory<I> for F
|
||||||
where
|
where
|
||||||
F: Fn() -> T + Send + Clone + 'static,
|
F: Fn() -> T + Send + Clone + 'static,
|
||||||
T: NewService<Config = ServerConfig, Request = Io<I>>,
|
T: actix::ServiceFactory<Config = ServerConfig, Request = Io<I>>,
|
||||||
I: FromStream,
|
I: FromStream,
|
||||||
{
|
{
|
||||||
type NewService = T;
|
type NewService = T;
|
@ -1,8 +1,13 @@
|
|||||||
|
use std::future::Future;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_rt::spawn;
|
use actix_rt::spawn;
|
||||||
use futures::stream::futures_unordered;
|
use futures::future::LocalBoxFuture;
|
||||||
use futures::{Async, Future, Poll, Stream};
|
use futures::stream::{futures_unordered, FuturesUnordered, LocalBoxStream};
|
||||||
|
use futures::{FutureExt, Stream, StreamExt, TryFutureExt, TryStream, TryStreamExt};
|
||||||
|
use tokio_net::signal::unix::signal;
|
||||||
|
|
||||||
use crate::server::Server;
|
use crate::server::Server;
|
||||||
|
|
||||||
@ -27,14 +32,14 @@ pub(crate) struct Signals {
|
|||||||
streams: Vec<SigStream>,
|
streams: Vec<SigStream>,
|
||||||
}
|
}
|
||||||
|
|
||||||
type SigStream = Box<dyn Stream<Item = Signal, Error = io::Error>>;
|
type SigStream = LocalBoxStream<'static, Result<Signal, io::Error>>;
|
||||||
|
|
||||||
impl Signals {
|
impl Signals {
|
||||||
pub(crate) fn start(srv: Server) {
|
pub(crate) fn start(srv: Server) {
|
||||||
let fut = {
|
let fut = {
|
||||||
#[cfg(not(unix))]
|
#[cfg(not(unix))]
|
||||||
{
|
{
|
||||||
tokio_signal::ctrl_c()
|
tokio_net::signal::ctrl_c()
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
.and_then(move |stream| Signals {
|
.and_then(move |stream| Signals {
|
||||||
srv,
|
srv,
|
||||||
@ -44,51 +49,79 @@ impl Signals {
|
|||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
{
|
{
|
||||||
use tokio_signal::unix;
|
use tokio_net::signal::unix;
|
||||||
|
|
||||||
let mut sigs: Vec<Box<dyn Future<Item = SigStream, Error = io::Error>>> =
|
let mut sigs: Vec<_> = Vec::new();
|
||||||
Vec::new();
|
|
||||||
sigs.push(Box::new(
|
let mut SIG_MAP = [
|
||||||
tokio_signal::unix::Signal::new(tokio_signal::unix::SIGINT).map(|stream| {
|
(
|
||||||
|
tokio_net::signal::unix::SignalKind::interrupt(),
|
||||||
|
Signal::Int,
|
||||||
|
),
|
||||||
|
(tokio_net::signal::unix::SignalKind::hangup(), Signal::Hup),
|
||||||
|
(
|
||||||
|
tokio_net::signal::unix::SignalKind::terminate(),
|
||||||
|
Signal::Term,
|
||||||
|
),
|
||||||
|
(tokio_net::signal::unix::SignalKind::quit(), Signal::Quit),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (kind, sig) in SIG_MAP.into_iter() {
|
||||||
|
let sig = sig.clone();
|
||||||
|
let fut = signal(*kind).unwrap();
|
||||||
|
sigs.push(fut.map(move |_| Ok(sig)).boxed_local());
|
||||||
|
}
|
||||||
|
/* TODO: Finish rewriting this
|
||||||
|
sigs.push(
|
||||||
|
tokio_net::signal::unix::signal(tokio_net::signal::si).unwrap()
|
||||||
|
.map(|stream| {
|
||||||
let s: SigStream = Box::new(stream.map(|_| Signal::Int));
|
let s: SigStream = Box::new(stream.map(|_| Signal::Int));
|
||||||
s
|
s
|
||||||
}),
|
}).boxed()
|
||||||
));
|
);
|
||||||
sigs.push(Box::new(
|
sigs.push(
|
||||||
tokio_signal::unix::Signal::new(tokio_signal::unix::SIGHUP).map(
|
|
||||||
|stream: unix::Signal| {
|
tokio_net::signal::unix::signal(tokio_net::signal::unix::SignalKind::hangup()).unwrap()
|
||||||
|
.map(|stream: unix::Signal| {
|
||||||
let s: SigStream = Box::new(stream.map(|_| Signal::Hup));
|
let s: SigStream = Box::new(stream.map(|_| Signal::Hup));
|
||||||
s
|
s
|
||||||
},
|
}).boxed()
|
||||||
),
|
);
|
||||||
));
|
sigs.push(
|
||||||
sigs.push(Box::new(
|
tokio_net::signal::unix::signal(
|
||||||
tokio_signal::unix::Signal::new(tokio_signal::unix::SIGTERM).map(
|
tokio_net::signal::unix::SignalKind::terminate()
|
||||||
|stream| {
|
).unwrap()
|
||||||
|
.map(|stream| {
|
||||||
let s: SigStream = Box::new(stream.map(|_| Signal::Term));
|
let s: SigStream = Box::new(stream.map(|_| Signal::Term));
|
||||||
s
|
s
|
||||||
},
|
}).boxed(),
|
||||||
),
|
);
|
||||||
));
|
sigs.push(
|
||||||
sigs.push(Box::new(
|
tokio_net::signal::unix::signal(
|
||||||
tokio_signal::unix::Signal::new(tokio_signal::unix::SIGQUIT).map(
|
tokio_net::signal::unix::SignalKind::quit()
|
||||||
|stream| {
|
).unwrap()
|
||||||
|
.map(|stream| {
|
||||||
let s: SigStream = Box::new(stream.map(|_| Signal::Quit));
|
let s: SigStream = Box::new(stream.map(|_| Signal::Quit));
|
||||||
s
|
s
|
||||||
},
|
}).boxed()
|
||||||
),
|
);
|
||||||
));
|
*/
|
||||||
futures_unordered(sigs)
|
|
||||||
.collect()
|
Signals { srv, streams: sigs }
|
||||||
.map_err(|_| ())
|
|
||||||
.and_then(move |streams| Signals { srv, streams })
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
spawn(fut);
|
spawn(async {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Future for Signals {
|
impl Future for Signals {
|
||||||
|
type Output = ();
|
||||||
|
|
||||||
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
type Item = ();
|
type Item = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
|
|
||||||
@ -115,4 +148,5 @@ impl Future for Signals {
|
|||||||
Ok(Async::NotReady)
|
Ok(Async::NotReady)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use std::{fmt, io, net};
|
use std::{fmt, io, net};
|
||||||
|
|
||||||
use tokio_io::{AsyncRead, AsyncWrite};
|
use tokio_io::{AsyncRead, AsyncWrite};
|
||||||
use tokio_reactor::Handle;
|
use tokio_net::driver::Handle;
|
||||||
use tokio_tcp::TcpStream;
|
use tokio_net::tcp::TcpStream;
|
||||||
|
|
||||||
pub(crate) enum StdListener {
|
pub(crate) enum StdListener {
|
||||||
Tcp(net::TcpListener),
|
Tcp(net::TcpListener),
|
||||||
|
@ -3,19 +3,19 @@ use std::sync::atomic::{AtomicUsize, Ordering};
|
|||||||
|
|
||||||
use crate::counter::Counter;
|
use crate::counter::Counter;
|
||||||
|
|
||||||
#[cfg(feature = "ssl")]
|
#[cfg(feature = "openssl")]
|
||||||
mod openssl;
|
mod openssl;
|
||||||
#[cfg(feature = "ssl")]
|
#[cfg(feature = "openssl")]
|
||||||
pub use self::openssl::OpensslAcceptor;
|
pub use self::openssl::OpensslAcceptor;
|
||||||
|
|
||||||
#[cfg(feature = "tls")]
|
#[cfg(feature = "nativetls")]
|
||||||
mod nativetls;
|
mod nativetls;
|
||||||
#[cfg(feature = "tls")]
|
#[cfg(feature = "nativetls")]
|
||||||
pub use self::nativetls::{NativeTlsAcceptor, TlsStream};
|
pub use self::nativetls::NativeTlsAcceptor;
|
||||||
|
|
||||||
#[cfg(feature = "rust-tls")]
|
#[cfg(feature = "rustls")]
|
||||||
mod rustls;
|
mod rustls;
|
||||||
#[cfg(feature = "rust-tls")]
|
#[cfg(feature = "rustls")]
|
||||||
pub use self::rustls::RustlsAcceptor;
|
pub use self::rustls::RustlsAcceptor;
|
||||||
|
|
||||||
/// Sets the maximum per-worker concurrent ssl connection establish process.
|
/// Sets the maximum per-worker concurrent ssl connection establish process.
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
use std::io;
|
use std::convert::Infallible;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_service::{NewService, Service};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
|
use futures::future::{self, FutureExt as _, LocalBoxFuture, TryFutureExt as _};
|
||||||
use native_tls::{self, Error, HandshakeError, TlsAcceptor};
|
use native_tls::Error;
|
||||||
use tokio_io::{AsyncRead, AsyncWrite};
|
use tokio::io::{AsyncRead, AsyncWrite};
|
||||||
|
use tokio_tls::{TlsAcceptor, TlsStream};
|
||||||
|
|
||||||
use crate::counter::{Counter, CounterGuard};
|
use crate::counter::Counter;
|
||||||
use crate::ssl::MAX_CONN_COUNTER;
|
use crate::ssl::MAX_CONN_COUNTER;
|
||||||
use crate::{Io, Protocol, ServerConfig};
|
use crate::{Io, ServerConfig};
|
||||||
|
|
||||||
/// Support `SSL` connections via native-tls package
|
/// Support `SSL` connections via native-tls package
|
||||||
///
|
///
|
||||||
@ -18,8 +20,12 @@ pub struct NativeTlsAcceptor<T, P = ()> {
|
|||||||
io: PhantomData<(T, P)>,
|
io: PhantomData<(T, P)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite, P> NativeTlsAcceptor<T, P> {
|
impl<T, P> NativeTlsAcceptor<T, P>
|
||||||
|
where
|
||||||
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
|
{
|
||||||
/// Create `NativeTlsAcceptor` instance
|
/// Create `NativeTlsAcceptor` instance
|
||||||
|
#[inline]
|
||||||
pub fn new(acceptor: TlsAcceptor) -> Self {
|
pub fn new(acceptor: TlsAcceptor) -> Self {
|
||||||
NativeTlsAcceptor {
|
NativeTlsAcceptor {
|
||||||
acceptor,
|
acceptor,
|
||||||
@ -28,7 +34,8 @@ impl<T: AsyncRead + AsyncWrite, P> NativeTlsAcceptor<T, P> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite, P> Clone for NativeTlsAcceptor<T, P> {
|
impl<T, P> Clone for NativeTlsAcceptor<T, P> {
|
||||||
|
#[inline]
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
acceptor: self.acceptor.clone(),
|
acceptor: self.acceptor.clone(),
|
||||||
@ -37,21 +44,25 @@ impl<T: AsyncRead + AsyncWrite, P> Clone for NativeTlsAcceptor<T, P> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite, P> NewService for NativeTlsAcceptor<T, P> {
|
impl<T, P> ServiceFactory for NativeTlsAcceptor<T, P>
|
||||||
|
where
|
||||||
|
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
||||||
|
P: 'static,
|
||||||
|
{
|
||||||
type Request = Io<T, P>;
|
type Request = Io<T, P>;
|
||||||
type Response = Io<TlsStream<T>, P>;
|
type Response = Io<TlsStream<T>, P>;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
type Config = ServerConfig;
|
type Config = ServerConfig;
|
||||||
type Service = NativeTlsAcceptorService<T, P>;
|
type Service = NativeTlsAcceptorService<T, P>;
|
||||||
type InitError = ();
|
type InitError = Infallible;
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = future::Ready<Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
|
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
|
||||||
cfg.set_secure();
|
cfg.set_secure();
|
||||||
|
|
||||||
MAX_CONN_COUNTER.with(|conns| {
|
MAX_CONN_COUNTER.with(|conns| {
|
||||||
ok(NativeTlsAcceptorService {
|
future::ok(NativeTlsAcceptorService {
|
||||||
acceptor: self.acceptor.clone(),
|
acceptor: self.acceptor.clone(),
|
||||||
conns: conns.clone(),
|
conns: conns.clone(),
|
||||||
io: PhantomData,
|
io: PhantomData,
|
||||||
@ -66,117 +77,46 @@ pub struct NativeTlsAcceptorService<T, P> {
|
|||||||
conns: Counter,
|
conns: Counter,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite, P> Service for NativeTlsAcceptorService<T, P> {
|
impl<T, P> Clone for NativeTlsAcceptorService<T, P> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
acceptor: self.acceptor.clone(),
|
||||||
|
io: PhantomData,
|
||||||
|
conns: self.conns.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, P> Service for NativeTlsAcceptorService<T, P>
|
||||||
|
where
|
||||||
|
T: AsyncRead + AsyncWrite + Unpin + 'static,
|
||||||
|
P: 'static,
|
||||||
|
{
|
||||||
type Request = Io<T, P>;
|
type Request = Io<T, P>;
|
||||||
type Response = Io<TlsStream<T>, P>;
|
type Response = Io<TlsStream<T>, P>;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Accept<T, P>;
|
type Future = LocalBoxFuture<'static, Result<Io<TlsStream<T>, P>, Error>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
if self.conns.available() {
|
if self.conns.available(cx) {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||||
let (io, params, _) = req.into_parts();
|
let guard = self.conns.get();
|
||||||
Accept {
|
let this = self.clone();
|
||||||
_guard: self.conns.get(),
|
let (io, params, proto) = req.into_parts();
|
||||||
inner: Some(self.acceptor.accept(io)),
|
async move { this.acceptor.accept(io).await }
|
||||||
params: Some(params),
|
.map_ok(move |stream| Io::from_parts(stream, params, proto))
|
||||||
}
|
.map_ok(move |io| {
|
||||||
}
|
// Required to preserve `CounterGuard` until `Self::Future`
|
||||||
}
|
// is completely resolved.
|
||||||
|
let _ = guard;
|
||||||
/// A wrapper around an underlying raw stream which implements the TLS or SSL
|
io
|
||||||
/// protocol.
|
})
|
||||||
///
|
.boxed_local()
|
||||||
/// A `TlsStream<S>` represents a handshake that has been completed successfully
|
|
||||||
/// and both the server and the client are ready for receiving and sending
|
|
||||||
/// data. Bytes read from a `TlsStream` are decrypted from `S` and bytes written
|
|
||||||
/// to a `TlsStream` are encrypted when passing through to `S`.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct TlsStream<S> {
|
|
||||||
inner: native_tls::TlsStream<S>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Future returned from `NativeTlsAcceptor::accept` which will resolve
|
|
||||||
/// once the accept handshake has finished.
|
|
||||||
pub struct Accept<S, P> {
|
|
||||||
inner: Option<Result<native_tls::TlsStream<S>, HandshakeError<S>>>,
|
|
||||||
params: Option<P>,
|
|
||||||
_guard: CounterGuard,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite, P> Future for Accept<T, P> {
|
|
||||||
type Item = Io<TlsStream<T>, P>;
|
|
||||||
type Error = Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
match self.inner.take().expect("cannot poll MidHandshake twice") {
|
|
||||||
Ok(stream) => Ok(Async::Ready(Io::from_parts(
|
|
||||||
TlsStream { inner: stream },
|
|
||||||
self.params.take().unwrap(),
|
|
||||||
Protocol::Unknown,
|
|
||||||
))),
|
|
||||||
Err(HandshakeError::Failure(e)) => Err(e),
|
|
||||||
Err(HandshakeError::WouldBlock(s)) => match s.handshake() {
|
|
||||||
Ok(stream) => Ok(Async::Ready(Io::from_parts(
|
|
||||||
TlsStream { inner: stream },
|
|
||||||
self.params.take().unwrap(),
|
|
||||||
Protocol::Unknown,
|
|
||||||
))),
|
|
||||||
Err(HandshakeError::Failure(e)) => Err(e),
|
|
||||||
Err(HandshakeError::WouldBlock(s)) => {
|
|
||||||
self.inner = Some(Err(HandshakeError::WouldBlock(s)));
|
|
||||||
Ok(Async::NotReady)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> TlsStream<S> {
|
|
||||||
/// Get access to the internal `native_tls::TlsStream` stream which also
|
|
||||||
/// transitively allows access to `S`.
|
|
||||||
pub fn get_ref(&self) -> &native_tls::TlsStream<S> {
|
|
||||||
&self.inner
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get mutable access to the internal `native_tls::TlsStream` stream which
|
|
||||||
/// also transitively allows mutable access to `S`.
|
|
||||||
pub fn get_mut(&mut self) -> &mut native_tls::TlsStream<S> {
|
|
||||||
&mut self.inner
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: io::Read + io::Write> io::Read for TlsStream<S> {
|
|
||||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
||||||
self.inner.read(buf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: io::Read + io::Write> io::Write for TlsStream<S> {
|
|
||||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
||||||
self.inner.write(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn flush(&mut self) -> io::Result<()> {
|
|
||||||
self.inner.flush()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: AsyncRead + AsyncWrite> AsyncRead for TlsStream<S> {}
|
|
||||||
|
|
||||||
impl<S: AsyncRead + AsyncWrite> AsyncWrite for TlsStream<S> {
|
|
||||||
fn shutdown(&mut self) -> Poll<(), io::Error> {
|
|
||||||
match self.inner.shutdown() {
|
|
||||||
Ok(_) => (),
|
|
||||||
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => (),
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
}
|
|
||||||
self.inner.get_mut().shutdown()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_service::{NewService, Service};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
|
use futures::future::{ok, FutureExt, LocalBoxFuture, Ready};
|
||||||
use openssl::ssl::{HandshakeError, SslAcceptor};
|
use open_ssl::ssl::SslAcceptor;
|
||||||
|
use pin_project::pin_project;
|
||||||
use tokio_io::{AsyncRead, AsyncWrite};
|
use tokio_io::{AsyncRead, AsyncWrite};
|
||||||
use tokio_openssl::{AcceptAsync, SslAcceptorExt, SslStream};
|
use tokio_openssl::{HandshakeError, SslStream};
|
||||||
|
|
||||||
use crate::counter::{Counter, CounterGuard};
|
use crate::counter::{Counter, CounterGuard};
|
||||||
use crate::ssl::MAX_CONN_COUNTER;
|
use crate::ssl::MAX_CONN_COUNTER;
|
||||||
@ -37,14 +41,14 @@ impl<T: AsyncRead + AsyncWrite, P> Clone for OpensslAcceptor<T, P> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite, P> NewService for OpensslAcceptor<T, P> {
|
impl<T: AsyncRead + AsyncWrite + Unpin + 'static, P> ServiceFactory for OpensslAcceptor<T, P> {
|
||||||
type Request = Io<T, P>;
|
type Request = Io<T, P>;
|
||||||
type Response = Io<SslStream<T>, P>;
|
type Response = Io<SslStream<T>, P>;
|
||||||
type Error = HandshakeError<T>;
|
type Error = HandshakeError<T>;
|
||||||
type Config = ServerConfig;
|
type Config = ServerConfig;
|
||||||
type Service = OpensslAcceptorService<T, P>;
|
type Service = OpensslAcceptorService<T, P>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
|
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
|
||||||
cfg.set_secure();
|
cfg.set_secure();
|
||||||
@ -65,46 +69,54 @@ pub struct OpensslAcceptorService<T, P> {
|
|||||||
io: PhantomData<(T, P)>,
|
io: PhantomData<(T, P)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite, P> Service for OpensslAcceptorService<T, P> {
|
impl<T: AsyncRead + AsyncWrite + Unpin + 'static, P> Service for OpensslAcceptorService<T, P> {
|
||||||
type Request = Io<T, P>;
|
type Request = Io<T, P>;
|
||||||
type Response = Io<SslStream<T>, P>;
|
type Response = Io<SslStream<T>, P>;
|
||||||
type Error = HandshakeError<T>;
|
type Error = HandshakeError<T>;
|
||||||
type Future = OpensslAcceptorServiceFut<T, P>;
|
type Future = OpensslAcceptorServiceFut<T, P>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
if self.conns.available() {
|
if self.conns.available(ctx) {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||||
let (io, params, _) = req.into_parts();
|
let (io, params, _) = req.into_parts();
|
||||||
|
let acc = self.acceptor.clone();
|
||||||
OpensslAcceptorServiceFut {
|
OpensslAcceptorServiceFut {
|
||||||
_guard: self.conns.get(),
|
_guard: self.conns.get(),
|
||||||
fut: SslAcceptorExt::accept_async(&self.acceptor, io),
|
fut: async move {
|
||||||
|
let acc = acc;
|
||||||
|
tokio_openssl::accept(&acc, io).await
|
||||||
|
}
|
||||||
|
.boxed_local::<'static>(),
|
||||||
params: Some(params),
|
params: Some(params),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pin_project]
|
||||||
pub struct OpensslAcceptorServiceFut<T, P>
|
pub struct OpensslAcceptorServiceFut<T, P>
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
{
|
{
|
||||||
fut: AcceptAsync<T>,
|
#[pin]
|
||||||
|
fut: LocalBoxFuture<'static, Result<SslStream<T>, HandshakeError<T>>>,
|
||||||
params: Option<P>,
|
params: Option<P>,
|
||||||
_guard: CounterGuard,
|
_guard: CounterGuard,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite, P> Future for OpensslAcceptorServiceFut<T, P> {
|
impl<T: AsyncRead + AsyncWrite, P> Future for OpensslAcceptorServiceFut<T, P> {
|
||||||
type Item = Io<SslStream<T>, P>;
|
type Output = Result<Io<SslStream<T>, P>, HandshakeError<T>>;
|
||||||
type Error = HandshakeError<T>;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let io = futures::try_ready!(self.fut.poll());
|
let this = self.project();
|
||||||
let proto = if let Some(protos) = io.get_ref().ssl().selected_alpn_protocol() {
|
|
||||||
|
let io = futures::ready!(this.fut.poll(cx))?;
|
||||||
|
let proto = if let Some(protos) = io.ssl().selected_alpn_protocol() {
|
||||||
const H2: &[u8] = b"\x02h2";
|
const H2: &[u8] = b"\x02h2";
|
||||||
const HTTP10: &[u8] = b"\x08http/1.0";
|
const HTTP10: &[u8] = b"\x08http/1.0";
|
||||||
const HTTP11: &[u8] = b"\x08http/1.1";
|
const HTTP11: &[u8] = b"\x08http/1.1";
|
||||||
@ -121,10 +133,7 @@ impl<T: AsyncRead + AsyncWrite, P> Future for OpensslAcceptorServiceFut<T, P> {
|
|||||||
} else {
|
} else {
|
||||||
Protocol::Unknown
|
Protocol::Unknown
|
||||||
};
|
};
|
||||||
Ok(Async::Ready(Io::from_parts(
|
|
||||||
io,
|
Poll::Ready(Ok(Io::from_parts(io, this.params.take().unwrap(), proto)))
|
||||||
self.params.take().unwrap(),
|
|
||||||
proto,
|
|
||||||
)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
|
use std::future::Future;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_service::{NewService, Service};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
|
use futures::future::{ok, Ready};
|
||||||
use rustls::ServerConfig;
|
use pin_project::pin_project;
|
||||||
|
use rust_tls::ServerConfig;
|
||||||
use tokio_io::{AsyncRead, AsyncWrite};
|
use tokio_io::{AsyncRead, AsyncWrite};
|
||||||
use tokio_rustls::{server::TlsStream, Accept, TlsAcceptor};
|
use tokio_rustls::{server::TlsStream, Accept, TlsAcceptor};
|
||||||
|
|
||||||
@ -39,7 +43,7 @@ impl<T, P> Clone for RustlsAcceptor<T, P> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite, P> NewService for RustlsAcceptor<T, P> {
|
impl<T: AsyncRead + AsyncWrite + Unpin, P> ServiceFactory for RustlsAcceptor<T, P> {
|
||||||
type Request = Io<T, P>;
|
type Request = Io<T, P>;
|
||||||
type Response = Io<TlsStream<T>, P>;
|
type Response = Io<TlsStream<T>, P>;
|
||||||
type Error = io::Error;
|
type Error = io::Error;
|
||||||
@ -47,7 +51,7 @@ impl<T: AsyncRead + AsyncWrite, P> NewService for RustlsAcceptor<T, P> {
|
|||||||
type Config = SrvConfig;
|
type Config = SrvConfig;
|
||||||
type Service = RustlsAcceptorService<T, P>;
|
type Service = RustlsAcceptorService<T, P>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &SrvConfig) -> Self::Future {
|
fn new_service(&self, cfg: &SrvConfig) -> Self::Future {
|
||||||
cfg.set_secure();
|
cfg.set_secure();
|
||||||
@ -68,17 +72,17 @@ pub struct RustlsAcceptorService<T, P> {
|
|||||||
conns: Counter,
|
conns: Counter,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite, P> Service for RustlsAcceptorService<T, P> {
|
impl<T: AsyncRead + AsyncWrite + Unpin, P> Service for RustlsAcceptorService<T, P> {
|
||||||
type Request = Io<T, P>;
|
type Request = Io<T, P>;
|
||||||
type Response = Io<TlsStream<T>, P>;
|
type Response = Io<TlsStream<T>, P>;
|
||||||
type Error = io::Error;
|
type Error = io::Error;
|
||||||
type Future = RustlsAcceptorServiceFut<T, P>;
|
type Future = RustlsAcceptorServiceFut<T, P>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
if self.conns.available() {
|
if self.conns.available(cx) {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,25 +96,26 @@ impl<T: AsyncRead + AsyncWrite, P> Service for RustlsAcceptorService<T, P> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pin_project]
|
||||||
pub struct RustlsAcceptorServiceFut<T, P>
|
pub struct RustlsAcceptorServiceFut<T, P>
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
{
|
{
|
||||||
|
#[pin]
|
||||||
fut: Accept<T>,
|
fut: Accept<T>,
|
||||||
params: Option<P>,
|
params: Option<P>,
|
||||||
_guard: CounterGuard,
|
_guard: CounterGuard,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite, P> Future for RustlsAcceptorServiceFut<T, P> {
|
impl<T: AsyncRead + AsyncWrite + Unpin, P> Future for RustlsAcceptorServiceFut<T, P> {
|
||||||
type Item = Io<TlsStream<T>, P>;
|
type Output = Result<Io<TlsStream<T>, P>, io::Error>;
|
||||||
type Error = io::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||||
let io = futures::try_ready!(self.fut.poll());
|
let this = self.project();
|
||||||
Ok(Async::Ready(Io::from_parts(
|
let params = this.params.take().unwrap();
|
||||||
io,
|
Poll::Ready(
|
||||||
self.params.take().unwrap(),
|
futures::ready!(this.fut.poll(cx))
|
||||||
Protocol::Unknown,
|
.map(move |io| Io::from_parts(io, params, Protocol::Unknown)),
|
||||||
)))
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,20 @@
|
|||||||
|
use std::pin::Pin;
|
||||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
use std::{mem, time};
|
use std::{mem, time};
|
||||||
|
|
||||||
use actix_rt::{spawn, Arbiter};
|
use actix_rt::{spawn, Arbiter};
|
||||||
use futures::sync::mpsc::{UnboundedReceiver, UnboundedSender};
|
use futures::channel::mpsc::{UnboundedReceiver, UnboundedSender};
|
||||||
use futures::sync::oneshot;
|
use futures::channel::oneshot;
|
||||||
use futures::{future, Async, Future, Poll, Stream};
|
use futures::future::{join_all, LocalBoxFuture, MapOk};
|
||||||
|
use futures::{Future, FutureExt, Stream, TryFutureExt};
|
||||||
use log::{error, info, trace};
|
use log::{error, info, trace};
|
||||||
use tokio_timer::{sleep, Delay};
|
use tokio_timer::{delay, Delay};
|
||||||
|
|
||||||
use crate::accept::AcceptNotify;
|
use crate::accept::AcceptNotify;
|
||||||
use crate::counter::Counter;
|
use crate::counter::Counter;
|
||||||
use crate::services::{BoxedServerService, InternalServiceFactory, ServerMessage};
|
use crate::service::{BoxedServerService, InternalServiceFactory, ServerMessage};
|
||||||
use crate::socket::{SocketAddr, StdStream};
|
use crate::socket::{SocketAddr, StdStream};
|
||||||
use crate::Token;
|
use crate::Token;
|
||||||
|
|
||||||
@ -153,21 +156,21 @@ impl Worker {
|
|||||||
state: WorkerState::Unavailable(Vec::new()),
|
state: WorkerState::Unavailable(Vec::new()),
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut fut = Vec::new();
|
let mut fut: Vec<MapOk<LocalBoxFuture<'static, _>, _>> = Vec::new();
|
||||||
for (idx, factory) in wrk.factories.iter().enumerate() {
|
for (idx, factory) in wrk.factories.iter().enumerate() {
|
||||||
fut.push(factory.create().map(move |res| {
|
fut.push(factory.create().map_ok(move |r| {
|
||||||
res.into_iter()
|
r.into_iter()
|
||||||
.map(|(t, s)| (idx, t, s))
|
.map(|(t, s): (Token, _)| (idx, t, s))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
spawn(
|
spawn(
|
||||||
future::join_all(fut)
|
async move {
|
||||||
.map_err(|e| {
|
let res = join_all(fut).await;
|
||||||
error!("Can not start worker: {:?}", e);
|
let res: Result<Vec<_>, _> = res.into_iter().collect();
|
||||||
Arbiter::current().stop();
|
match res {
|
||||||
})
|
Ok(services) => {
|
||||||
.and_then(move |services| {
|
|
||||||
for item in services {
|
for item in services {
|
||||||
for (idx, token, service) in item {
|
for (idx, token, service) in item {
|
||||||
while token.0 >= wrk.services.len() {
|
while token.0 >= wrk.services.len() {
|
||||||
@ -176,8 +179,15 @@ impl Worker {
|
|||||||
wrk.services[token.0] = Some((idx, service));
|
wrk.services[token.0] = Some((idx, service));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wrk
|
}
|
||||||
}),
|
Err(e) => {
|
||||||
|
error!("Can not start worker: {:?}", e);
|
||||||
|
Arbiter::current().stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wrk.await
|
||||||
|
}
|
||||||
|
.boxed_local(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,13 +208,17 @@ impl Worker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_readiness(&mut self, trace: bool) -> Result<bool, (Token, usize)> {
|
fn check_readiness(
|
||||||
let mut ready = self.conns.available();
|
&mut self,
|
||||||
|
trace: bool,
|
||||||
|
cx: &mut Context<'_>,
|
||||||
|
) -> Result<bool, (Token, usize)> {
|
||||||
|
let mut ready = self.conns.available(cx);
|
||||||
let mut failed = None;
|
let mut failed = None;
|
||||||
for (token, service) in &mut self.services.iter_mut().enumerate() {
|
for (token, service) in &mut self.services.iter_mut().enumerate() {
|
||||||
if let Some(service) = service {
|
if let Some(service) = service {
|
||||||
match service.1.poll_ready() {
|
match service.1.poll_ready(cx) {
|
||||||
Ok(Async::Ready(_)) => {
|
Poll::Ready(Ok(_)) => {
|
||||||
if trace {
|
if trace {
|
||||||
trace!(
|
trace!(
|
||||||
"Service {:?} is available",
|
"Service {:?} is available",
|
||||||
@ -212,8 +226,8 @@ impl Worker {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Async::NotReady) => ready = false,
|
Poll::Pending => ready = false,
|
||||||
Err(_) => {
|
Poll::Ready(Err(_)) => {
|
||||||
error!(
|
error!(
|
||||||
"Service {:?} readiness check returned error, restarting",
|
"Service {:?} readiness check returned error, restarting",
|
||||||
self.factories[service.0].name(Token(token))
|
self.factories[service.0].name(Token(token))
|
||||||
@ -238,43 +252,44 @@ enum WorkerState {
|
|||||||
Restarting(
|
Restarting(
|
||||||
usize,
|
usize,
|
||||||
Token,
|
Token,
|
||||||
Box<dyn Future<Item = Vec<(Token, BoxedServerService)>, Error = ()>>,
|
Pin<Box<dyn Future<Output = Result<Vec<(Token, BoxedServerService)>, ()>>>>,
|
||||||
),
|
),
|
||||||
Shutdown(Delay, Delay, oneshot::Sender<bool>),
|
Shutdown(Delay, Delay, oneshot::Sender<bool>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Future for Worker {
|
impl Future for Worker {
|
||||||
type Item = ();
|
type Output = ();
|
||||||
type Error = ();
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
// `StopWorker` message handler
|
// `StopWorker` message handler
|
||||||
if let Ok(Async::Ready(Some(StopCommand { graceful, result }))) = self.rx2.poll() {
|
if let Poll::Ready(Some(StopCommand { graceful, result })) =
|
||||||
|
Pin::new(&mut self.rx2).poll_next(cx)
|
||||||
|
{
|
||||||
self.availability.set(false);
|
self.availability.set(false);
|
||||||
let num = num_connections();
|
let num = num_connections();
|
||||||
if num == 0 {
|
if num == 0 {
|
||||||
info!("Shutting down worker, 0 connections");
|
info!("Shutting down worker, 0 connections");
|
||||||
let _ = result.send(true);
|
let _ = result.send(true);
|
||||||
return Ok(Async::Ready(()));
|
return Poll::Ready(());
|
||||||
} else if graceful {
|
} else if graceful {
|
||||||
self.shutdown(false);
|
self.shutdown(false);
|
||||||
let num = num_connections();
|
let num = num_connections();
|
||||||
if num != 0 {
|
if num != 0 {
|
||||||
info!("Graceful worker shutdown, {} connections", num);
|
info!("Graceful worker shutdown, {} connections", num);
|
||||||
self.state = WorkerState::Shutdown(
|
self.state = WorkerState::Shutdown(
|
||||||
sleep(time::Duration::from_secs(1)),
|
delay(time::Instant::now() + time::Duration::from_secs(1)),
|
||||||
sleep(self.shutdown_timeout),
|
delay(time::Instant::now() + self.shutdown_timeout),
|
||||||
result,
|
result,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
let _ = result.send(true);
|
let _ = result.send(true);
|
||||||
return Ok(Async::Ready(()));
|
return Poll::Ready(());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
info!("Force shutdown worker, {} connections", num);
|
info!("Force shutdown worker, {} connections", num);
|
||||||
self.shutdown(true);
|
self.shutdown(true);
|
||||||
let _ = result.send(false);
|
let _ = result.send(false);
|
||||||
return Ok(Async::Ready(()));
|
return Poll::Ready(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -282,13 +297,13 @@ impl Future for Worker {
|
|||||||
|
|
||||||
match state {
|
match state {
|
||||||
WorkerState::Unavailable(mut conns) => {
|
WorkerState::Unavailable(mut conns) => {
|
||||||
match self.check_readiness(true) {
|
match self.check_readiness(true, cx) {
|
||||||
Ok(true) => {
|
Ok(true) => {
|
||||||
self.state = WorkerState::Available;
|
self.state = WorkerState::Available;
|
||||||
|
|
||||||
// process requests from wait queue
|
// process requests from wait queue
|
||||||
while let Some(msg) = conns.pop() {
|
while let Some(msg) = conns.pop() {
|
||||||
match self.check_readiness(false) {
|
match self.check_readiness(false, cx) {
|
||||||
Ok(true) => {
|
Ok(true) => {
|
||||||
let guard = self.conns.get();
|
let guard = self.conns.get();
|
||||||
let _ = self.services[msg.token.0]
|
let _ = self.services[msg.token.0]
|
||||||
@ -300,7 +315,7 @@ impl Future for Worker {
|
|||||||
Ok(false) => {
|
Ok(false) => {
|
||||||
trace!("Worker is unavailable");
|
trace!("Worker is unavailable");
|
||||||
self.state = WorkerState::Unavailable(conns);
|
self.state = WorkerState::Unavailable(conns);
|
||||||
return self.poll();
|
return self.poll(cx);
|
||||||
}
|
}
|
||||||
Err((token, idx)) => {
|
Err((token, idx)) => {
|
||||||
trace!(
|
trace!(
|
||||||
@ -312,16 +327,16 @@ impl Future for Worker {
|
|||||||
token,
|
token,
|
||||||
self.factories[idx].create(),
|
self.factories[idx].create(),
|
||||||
);
|
);
|
||||||
return self.poll();
|
return self.poll(cx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.availability.set(true);
|
self.availability.set(true);
|
||||||
return self.poll();
|
return self.poll(cx);
|
||||||
}
|
}
|
||||||
Ok(false) => {
|
Ok(false) => {
|
||||||
self.state = WorkerState::Unavailable(conns);
|
self.state = WorkerState::Unavailable(conns);
|
||||||
return Ok(Async::NotReady);
|
return Poll::Pending;
|
||||||
}
|
}
|
||||||
Err((token, idx)) => {
|
Err((token, idx)) => {
|
||||||
trace!(
|
trace!(
|
||||||
@ -330,13 +345,13 @@ impl Future for Worker {
|
|||||||
);
|
);
|
||||||
self.state =
|
self.state =
|
||||||
WorkerState::Restarting(idx, token, self.factories[idx].create());
|
WorkerState::Restarting(idx, token, self.factories[idx].create());
|
||||||
return self.poll();
|
return self.poll(cx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WorkerState::Restarting(idx, token, mut fut) => {
|
WorkerState::Restarting(idx, token, mut fut) => {
|
||||||
match fut.poll() {
|
match Pin::new(&mut fut).poll(cx) {
|
||||||
Ok(Async::Ready(item)) => {
|
Poll::Ready(Ok(item)) => {
|
||||||
for (token, service) in item {
|
for (token, service) in item {
|
||||||
trace!(
|
trace!(
|
||||||
"Service {:?} has been restarted",
|
"Service {:?} has been restarted",
|
||||||
@ -346,55 +361,55 @@ impl Future for Worker {
|
|||||||
self.state = WorkerState::Unavailable(Vec::new());
|
self.state = WorkerState::Unavailable(Vec::new());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Async::NotReady) => {
|
Poll::Ready(Err(_)) => {
|
||||||
self.state = WorkerState::Restarting(idx, token, fut);
|
|
||||||
return Ok(Async::NotReady);
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
panic!(
|
panic!(
|
||||||
"Can not restart {:?} service",
|
"Can not restart {:?} service",
|
||||||
self.factories[idx].name(token)
|
self.factories[idx].name(token)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Poll::Pending => {
|
||||||
|
self.state = WorkerState::Restarting(idx, token, fut);
|
||||||
|
return Poll::Pending;
|
||||||
}
|
}
|
||||||
return self.poll();
|
}
|
||||||
|
return self.poll(cx);
|
||||||
}
|
}
|
||||||
WorkerState::Shutdown(mut t1, mut t2, tx) => {
|
WorkerState::Shutdown(mut t1, mut t2, tx) => {
|
||||||
let num = num_connections();
|
let num = num_connections();
|
||||||
if num == 0 {
|
if num == 0 {
|
||||||
let _ = tx.send(true);
|
let _ = tx.send(true);
|
||||||
Arbiter::current().stop();
|
Arbiter::current().stop();
|
||||||
return Ok(Async::Ready(()));
|
return Poll::Ready(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// check graceful timeout
|
// check graceful timeout
|
||||||
match t2.poll().unwrap() {
|
match Pin::new(&mut t2).poll(cx) {
|
||||||
Async::NotReady => (),
|
Poll::Pending => (),
|
||||||
Async::Ready(_) => {
|
Poll::Ready(_) => {
|
||||||
self.shutdown(true);
|
self.shutdown(true);
|
||||||
let _ = tx.send(false);
|
let _ = tx.send(false);
|
||||||
Arbiter::current().stop();
|
Arbiter::current().stop();
|
||||||
return Ok(Async::Ready(()));
|
return Poll::Ready(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// sleep for 1 second and then check again
|
// sleep for 1 second and then check again
|
||||||
match t1.poll().unwrap() {
|
match Pin::new(&mut t1).poll(cx) {
|
||||||
Async::NotReady => (),
|
Poll::Pending => (),
|
||||||
Async::Ready(_) => {
|
Poll::Ready(_) => {
|
||||||
t1 = sleep(time::Duration::from_secs(1));
|
t1 = delay(time::Instant::now() + time::Duration::from_secs(1));
|
||||||
let _ = t1.poll();
|
let _ = Pin::new(&mut t1).poll(cx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.state = WorkerState::Shutdown(t1, t2, tx);
|
self.state = WorkerState::Shutdown(t1, t2, tx);
|
||||||
return Ok(Async::NotReady);
|
return Poll::Pending;
|
||||||
}
|
}
|
||||||
WorkerState::Available => {
|
WorkerState::Available => {
|
||||||
loop {
|
loop {
|
||||||
match self.rx.poll() {
|
match Pin::new(&mut self.rx).poll_next(cx) {
|
||||||
// handle incoming tcp stream
|
// handle incoming tcp stream
|
||||||
Ok(Async::Ready(Some(WorkerCommand(msg)))) => {
|
Poll::Ready(Some(WorkerCommand(msg))) => {
|
||||||
match self.check_readiness(false) {
|
match self.check_readiness(false, cx) {
|
||||||
Ok(true) => {
|
Ok(true) => {
|
||||||
let guard = self.conns.get();
|
let guard = self.conns.get();
|
||||||
let _ = self.services[msg.token.0]
|
let _ = self.services[msg.token.0]
|
||||||
@ -422,13 +437,13 @@ impl Future for Worker {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return self.poll();
|
return self.poll(cx);
|
||||||
}
|
}
|
||||||
Ok(Async::NotReady) => {
|
Poll::Pending => {
|
||||||
self.state = WorkerState::Available;
|
self.state = WorkerState::Available;
|
||||||
return Ok(Async::NotReady);
|
return Poll::Pending;
|
||||||
}
|
}
|
||||||
Ok(Async::Ready(None)) | Err(_) => return Ok(Async::Ready(())),
|
Poll::Ready(None) => return Poll::Ready(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,11 @@ use std::{net, thread, time};
|
|||||||
|
|
||||||
use actix_codec::{BytesCodec, Framed};
|
use actix_codec::{BytesCodec, Framed};
|
||||||
use actix_server::{Io, Server, ServerConfig};
|
use actix_server::{Io, Server, ServerConfig};
|
||||||
use actix_service::{new_service_cfg, service_fn, IntoService};
|
use actix_service::{factory_fn_cfg, service_fn, service_fn2};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use futures::{Future, Sink};
|
use futures::{future::ok, SinkExt};
|
||||||
use net2::TcpBuilder;
|
use net2::TcpBuilder;
|
||||||
use tokio_tcp::TcpStream;
|
use tokio_net::tcp::TcpStream;
|
||||||
|
|
||||||
fn unused_addr() -> net::SocketAddr {
|
fn unused_addr() -> net::SocketAddr {
|
||||||
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
||||||
@ -28,9 +28,9 @@ fn test_bind() {
|
|||||||
let sys = actix_rt::System::new("test");
|
let sys = actix_rt::System::new("test");
|
||||||
let srv = Server::build()
|
let srv = Server::build()
|
||||||
.bind("test", addr, move || {
|
.bind("test", addr, move || {
|
||||||
new_service_cfg(move |cfg: &ServerConfig| {
|
factory_fn_cfg(move |cfg: &ServerConfig| {
|
||||||
assert_eq!(cfg.local_addr(), addr);
|
assert_eq!(cfg.local_addr(), addr);
|
||||||
Ok::<_, ()>((|_| Ok::<_, ()>(())).into_service())
|
ok::<_, ()>(service_fn2(|_| ok::<_, ()>(())))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -54,7 +54,7 @@ fn test_bind_no_config() {
|
|||||||
let h = thread::spawn(move || {
|
let h = thread::spawn(move || {
|
||||||
let sys = actix_rt::System::new("test");
|
let sys = actix_rt::System::new("test");
|
||||||
let srv = Server::build()
|
let srv = Server::build()
|
||||||
.bind("test", addr, move || service_fn(|_| Ok::<_, ()>(())))
|
.bind("test", addr, move || service_fn(|_| ok::<_, ()>(())))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.start();
|
.start();
|
||||||
let _ = tx.send((srv, actix_rt::System::current()));
|
let _ = tx.send((srv, actix_rt::System::current()));
|
||||||
@ -76,9 +76,9 @@ fn test_listen() {
|
|||||||
let lst = net::TcpListener::bind(addr).unwrap();
|
let lst = net::TcpListener::bind(addr).unwrap();
|
||||||
let srv = Server::build()
|
let srv = Server::build()
|
||||||
.listen("test", lst, move || {
|
.listen("test", lst, move || {
|
||||||
new_service_cfg(move |cfg: &ServerConfig| {
|
factory_fn_cfg(move |cfg: &ServerConfig| {
|
||||||
assert_eq!(cfg.local_addr(), addr);
|
assert_eq!(cfg.local_addr(), addr);
|
||||||
Ok::<_, ()>((|_| Ok::<_, ()>(())).into_service())
|
ok::<_, ()>(service_fn2(|_| ok::<_, ()>(())))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -102,19 +102,21 @@ fn test_start() {
|
|||||||
|
|
||||||
let h = thread::spawn(move || {
|
let h = thread::spawn(move || {
|
||||||
let sys = actix_rt::System::new("test");
|
let sys = actix_rt::System::new("test");
|
||||||
let srv = Server::build()
|
let srv: Server = Server::build()
|
||||||
.backlog(100)
|
.backlog(100)
|
||||||
.bind("test", addr, move || {
|
.bind("test", addr, move || {
|
||||||
new_service_cfg(move |cfg: &ServerConfig| {
|
factory_fn_cfg(move |cfg: &ServerConfig| {
|
||||||
assert_eq!(cfg.local_addr(), addr);
|
assert_eq!(cfg.local_addr(), addr);
|
||||||
Ok::<_, ()>(
|
|
||||||
(|io: Io<TcpStream>| {
|
let srv = service_fn2(|io: Io<TcpStream>| {
|
||||||
Framed::new(io.into_parts().0, BytesCodec)
|
async {
|
||||||
.send(Bytes::from_static(b"test"))
|
let mut f = Framed::new(io.into_parts().0, BytesCodec);
|
||||||
.then(|_| Ok::<_, ()>(()))
|
f.send(Bytes::from_static(b"test")).await.unwrap();
|
||||||
})
|
Ok::<_, ()>(())
|
||||||
.into_service(),
|
}
|
||||||
)
|
});
|
||||||
|
|
||||||
|
ok::<_, ()>(srv)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -125,7 +127,7 @@ fn test_start() {
|
|||||||
});
|
});
|
||||||
let (srv, sys) = rx.recv().unwrap();
|
let (srv, sys) = rx.recv().unwrap();
|
||||||
|
|
||||||
let mut buf = [0u8; 4];
|
let mut buf = [1u8; 4];
|
||||||
let mut conn = net::TcpStream::connect(addr).unwrap();
|
let mut conn = net::TcpStream::connect(addr).unwrap();
|
||||||
let _ = conn.read_exact(&mut buf);
|
let _ = conn.read_exact(&mut buf);
|
||||||
assert_eq!(buf, b"test"[..]);
|
assert_eq!(buf, b"test"[..]);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-service"
|
name = "actix-service"
|
||||||
version = "0.4.2"
|
version = "1.0.0-alpha.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix Service"
|
description = "Actix Service"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -23,7 +23,9 @@ name = "actix_service"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
futures = "0.1.25"
|
futures = "0.3.1"
|
||||||
|
pin-project = "0.4.5"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
tokio = "0.2.0-alpha.5"
|
||||||
actix-rt = "0.2"
|
actix-rt = "0.2"
|
@ -1,6 +1,10 @@
|
|||||||
use futures::{Async, Future, Poll};
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use super::{IntoNewService, NewService, Service};
|
use pin_project::pin_project;
|
||||||
|
|
||||||
|
use super::{Service, ServiceFactory};
|
||||||
use crate::cell::Cell;
|
use crate::cell::Cell;
|
||||||
|
|
||||||
/// Service for the `and_then` combinator, chaining a computation onto the end
|
/// Service for the `and_then` combinator, chaining a computation onto the end
|
||||||
@ -45,12 +49,12 @@ where
|
|||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
type Future = AndThenFuture<A, B>;
|
type Future = AndThenFuture<A, B>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
let not_ready = self.a.poll_ready()?.is_not_ready();
|
let not_ready = !self.a.poll_ready(cx)?.is_ready();
|
||||||
if self.b.get_mut().poll_ready()?.is_not_ready() || not_ready {
|
if !self.b.get_mut().poll_ready(cx)?.is_ready() || not_ready {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,13 +63,16 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pin_project]
|
||||||
pub struct AndThenFuture<A, B>
|
pub struct AndThenFuture<A, B>
|
||||||
where
|
where
|
||||||
A: Service,
|
A: Service,
|
||||||
B: Service<Request = A::Response, Error = A::Error>,
|
B: Service<Request = A::Response, Error = A::Error>,
|
||||||
{
|
{
|
||||||
b: Cell<B>,
|
b: Cell<B>,
|
||||||
|
#[pin]
|
||||||
fut_b: Option<B::Future>,
|
fut_b: Option<B::Future>,
|
||||||
|
#[pin]
|
||||||
fut_a: Option<A::Future>,
|
fut_a: Option<A::Future>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,22 +95,33 @@ where
|
|||||||
A: Service,
|
A: Service,
|
||||||
B: Service<Request = A::Response, Error = A::Error>,
|
B: Service<Request = A::Response, Error = A::Error>,
|
||||||
{
|
{
|
||||||
type Item = B::Response;
|
type Output = Result<B::Response, A::Error>;
|
||||||
type Error = A::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
if let Some(ref mut fut) = self.fut_b {
|
let mut this = self.project();
|
||||||
return fut.poll();
|
|
||||||
|
loop {
|
||||||
|
let mut fut_a = this.fut_a.as_mut();
|
||||||
|
let mut fut_b = this.fut_b.as_mut();
|
||||||
|
|
||||||
|
if let Some(fut) = fut_b.as_mut().as_pin_mut() {
|
||||||
|
return fut.poll(cx);
|
||||||
}
|
}
|
||||||
|
|
||||||
match self.fut_a.as_mut().expect("Bug in actix-service").poll() {
|
match fut_a
|
||||||
Ok(Async::Ready(resp)) => {
|
.as_mut()
|
||||||
let _ = self.fut_a.take();
|
.as_pin_mut()
|
||||||
self.fut_b = Some(self.b.get_mut().call(resp));
|
.expect("Bug in actix-service")
|
||||||
self.poll()
|
.poll(cx)
|
||||||
|
{
|
||||||
|
Poll::Ready(Ok(resp)) => {
|
||||||
|
fut_a.set(None);
|
||||||
|
let new_fut = this.b.get_mut().call(resp);
|
||||||
|
fut_b.set(Some(new_fut));
|
||||||
|
}
|
||||||
|
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
|
||||||
|
Poll::Pending => return Poll::Pending,
|
||||||
}
|
}
|
||||||
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,8 +129,8 @@ where
|
|||||||
/// `AndThenNewService` new service combinator
|
/// `AndThenNewService` new service combinator
|
||||||
pub struct AndThenNewService<A, B>
|
pub struct AndThenNewService<A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
B: NewService,
|
B: ServiceFactory,
|
||||||
{
|
{
|
||||||
a: A,
|
a: A,
|
||||||
b: B,
|
b: B,
|
||||||
@ -120,8 +138,8 @@ where
|
|||||||
|
|
||||||
impl<A, B> AndThenNewService<A, B>
|
impl<A, B> AndThenNewService<A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
B: NewService<
|
B: ServiceFactory<
|
||||||
Config = A::Config,
|
Config = A::Config,
|
||||||
Request = A::Response,
|
Request = A::Response,
|
||||||
Error = A::Error,
|
Error = A::Error,
|
||||||
@ -129,18 +147,15 @@ where
|
|||||||
>,
|
>,
|
||||||
{
|
{
|
||||||
/// Create new `AndThen` combinator
|
/// Create new `AndThen` combinator
|
||||||
pub fn new<F: IntoNewService<B>>(a: A, f: F) -> Self {
|
pub fn new(a: A, b: B) -> Self {
|
||||||
Self {
|
Self { a, b }
|
||||||
a,
|
|
||||||
b: f.into_new_service(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B> NewService for AndThenNewService<A, B>
|
impl<A, B> ServiceFactory for AndThenNewService<A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
B: NewService<
|
B: ServiceFactory<
|
||||||
Config = A::Config,
|
Config = A::Config,
|
||||||
Request = A::Response,
|
Request = A::Response,
|
||||||
Error = A::Error,
|
Error = A::Error,
|
||||||
@ -163,8 +178,8 @@ where
|
|||||||
|
|
||||||
impl<A, B> Clone for AndThenNewService<A, B>
|
impl<A, B> Clone for AndThenNewService<A, B>
|
||||||
where
|
where
|
||||||
A: NewService + Clone,
|
A: ServiceFactory + Clone,
|
||||||
B: NewService + Clone,
|
B: ServiceFactory + Clone,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -174,21 +189,25 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pin_project]
|
||||||
pub struct AndThenNewServiceFuture<A, B>
|
pub struct AndThenNewServiceFuture<A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
B: NewService<Request = A::Response>,
|
B: ServiceFactory<Request = A::Response>,
|
||||||
{
|
{
|
||||||
|
#[pin]
|
||||||
fut_b: B::Future,
|
fut_b: B::Future,
|
||||||
|
#[pin]
|
||||||
fut_a: A::Future,
|
fut_a: A::Future,
|
||||||
|
|
||||||
a: Option<A::Service>,
|
a: Option<A::Service>,
|
||||||
b: Option<B::Service>,
|
b: Option<B::Service>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B> AndThenNewServiceFuture<A, B>
|
impl<A, B> AndThenNewServiceFuture<A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
B: NewService<Request = A::Response>,
|
B: ServiceFactory<Request = A::Response>,
|
||||||
{
|
{
|
||||||
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
|
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
|
||||||
AndThenNewServiceFuture {
|
AndThenNewServiceFuture {
|
||||||
@ -202,56 +221,55 @@ where
|
|||||||
|
|
||||||
impl<A, B> Future for AndThenNewServiceFuture<A, B>
|
impl<A, B> Future for AndThenNewServiceFuture<A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
B: ServiceFactory<Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
||||||
{
|
{
|
||||||
type Item = AndThen<A::Service, B::Service>;
|
type Output = Result<AndThen<A::Service, B::Service>, A::InitError>;
|
||||||
type Error = A::InitError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
if self.a.is_none() {
|
let this = self.project();
|
||||||
if let Async::Ready(service) = self.fut_a.poll()? {
|
if this.a.is_none() {
|
||||||
self.a = Some(service);
|
if let Poll::Ready(service) = this.fut_a.poll(cx)? {
|
||||||
|
*this.a = Some(service);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if this.b.is_none() {
|
||||||
if self.b.is_none() {
|
if let Poll::Ready(service) = this.fut_b.poll(cx)? {
|
||||||
if let Async::Ready(service) = self.fut_b.poll()? {
|
*this.b = Some(service);
|
||||||
self.b = Some(service);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if this.a.is_some() && this.b.is_some() {
|
||||||
if self.a.is_some() && self.b.is_some() {
|
Poll::Ready(Ok(AndThen::new(
|
||||||
Ok(Async::Ready(AndThen::new(
|
this.a.take().unwrap(),
|
||||||
self.a.take().unwrap(),
|
this.b.take().unwrap(),
|
||||||
self.b.take().unwrap(),
|
|
||||||
)))
|
)))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use futures::future::{ok, FutureResult};
|
|
||||||
use futures::{Async, Poll};
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use super::*;
|
use futures::future::{lazy, ok, ready, Ready};
|
||||||
use crate::{NewService, Service, ServiceExt};
|
|
||||||
|
use crate::{factory_fn, pipeline, pipeline_factory, Service, ServiceFactory};
|
||||||
|
|
||||||
struct Srv1(Rc<Cell<usize>>);
|
struct Srv1(Rc<Cell<usize>>);
|
||||||
|
|
||||||
impl Service for Srv1 {
|
impl Service for Srv1 {
|
||||||
type Request = &'static str;
|
type Request = &'static str;
|
||||||
type Response = &'static str;
|
type Response = &'static str;
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<Self::Response, ()>;
|
type Future = Ready<Result<Self::Response, ()>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.0.set(self.0.get() + 1);
|
self.0.set(self.0.get() + 1);
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: &'static str) -> Self::Future {
|
fn call(&mut self, req: &'static str) -> Self::Future {
|
||||||
@ -266,11 +284,11 @@ mod tests {
|
|||||||
type Request = &'static str;
|
type Request = &'static str;
|
||||||
type Response = (&'static str, &'static str);
|
type Response = (&'static str, &'static str);
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<Self::Response, ()>;
|
type Future = Ready<Result<Self::Response, ()>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
self.0.set(self.0.get() + 1);
|
self.0.set(self.0.get() + 1);
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: &'static str) -> Self::Future {
|
fn call(&mut self, req: &'static str) -> Self::Future {
|
||||||
@ -278,39 +296,35 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_poll_ready() {
|
async fn test_poll_ready() {
|
||||||
let cnt = Rc::new(Cell::new(0));
|
let cnt = Rc::new(Cell::new(0));
|
||||||
let mut srv = Srv1(cnt.clone()).and_then(Srv2(cnt.clone()));
|
let mut srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt.clone()));
|
||||||
let res = srv.poll_ready();
|
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
||||||
assert!(res.is_ok());
|
assert_eq!(res, Poll::Ready(Ok(())));
|
||||||
assert_eq!(res.unwrap(), Async::Ready(()));
|
|
||||||
assert_eq!(cnt.get(), 2);
|
assert_eq!(cnt.get(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_call() {
|
async fn test_call() {
|
||||||
let cnt = Rc::new(Cell::new(0));
|
let cnt = Rc::new(Cell::new(0));
|
||||||
let mut srv = Srv1(cnt.clone()).and_then(Srv2(cnt));
|
let mut srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt));
|
||||||
let res = srv.call("srv1").poll();
|
let res = srv.call("srv1").await;
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), Async::Ready(("srv1", "srv2")));
|
assert_eq!(res.unwrap(), (("srv1", "srv2")));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_new_service() {
|
async fn test_new_service() {
|
||||||
let cnt = Rc::new(Cell::new(0));
|
let cnt = Rc::new(Cell::new(0));
|
||||||
let cnt2 = cnt.clone();
|
let cnt2 = cnt.clone();
|
||||||
let blank = move || Ok::<_, ()>(Srv1(cnt2.clone()));
|
let new_srv =
|
||||||
let new_srv = blank
|
pipeline_factory(factory_fn(move || ready(Ok::<_, ()>(Srv1(cnt2.clone())))))
|
||||||
.into_new_service()
|
.and_then(move || ready(Ok(Srv2(cnt.clone()))));
|
||||||
.and_then(move || Ok(Srv2(cnt.clone())));
|
|
||||||
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
|
let mut srv = new_srv.new_service(&()).await.unwrap();
|
||||||
let res = srv.call("srv1").poll();
|
let res = srv.call("srv1").await;
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), Async::Ready(("srv1", "srv2")));
|
assert_eq!(res.unwrap(), ("srv1", "srv2"));
|
||||||
} else {
|
|
||||||
panic!()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,186 +0,0 @@
|
|||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use futures::{Async, Future, Poll};
|
|
||||||
|
|
||||||
use crate::and_then::AndThen;
|
|
||||||
use crate::from_err::FromErr;
|
|
||||||
use crate::{NewService, Transform};
|
|
||||||
|
|
||||||
/// `Apply` new service combinator
|
|
||||||
pub struct AndThenTransform<T, A, B> {
|
|
||||||
a: A,
|
|
||||||
b: B,
|
|
||||||
t: Rc<T>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, A, B> AndThenTransform<T, A, B>
|
|
||||||
where
|
|
||||||
A: NewService,
|
|
||||||
B: NewService<Config = A::Config, InitError = A::InitError>,
|
|
||||||
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
|
|
||||||
T::Error: From<A::Error>,
|
|
||||||
{
|
|
||||||
/// Create new `ApplyNewService` new service instance
|
|
||||||
pub fn new(t: T, a: A, b: B) -> Self {
|
|
||||||
Self {
|
|
||||||
a,
|
|
||||||
b,
|
|
||||||
t: Rc::new(t),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, A, B> Clone for AndThenTransform<T, A, B>
|
|
||||||
where
|
|
||||||
A: Clone,
|
|
||||||
B: Clone,
|
|
||||||
{
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self {
|
|
||||||
a: self.a.clone(),
|
|
||||||
b: self.b.clone(),
|
|
||||||
t: self.t.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, A, B> NewService for AndThenTransform<T, A, B>
|
|
||||||
where
|
|
||||||
A: NewService,
|
|
||||||
B: NewService<Config = A::Config, InitError = A::InitError>,
|
|
||||||
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
|
|
||||||
T::Error: From<A::Error>,
|
|
||||||
{
|
|
||||||
type Request = A::Request;
|
|
||||||
type Response = T::Response;
|
|
||||||
type Error = T::Error;
|
|
||||||
|
|
||||||
type Config = A::Config;
|
|
||||||
type InitError = T::InitError;
|
|
||||||
type Service = AndThen<FromErr<A::Service, T::Error>, T::Transform>;
|
|
||||||
type Future = AndThenTransformFuture<T, A, B>;
|
|
||||||
|
|
||||||
fn new_service(&self, cfg: &A::Config) -> Self::Future {
|
|
||||||
AndThenTransformFuture {
|
|
||||||
a: None,
|
|
||||||
t: None,
|
|
||||||
t_cell: self.t.clone(),
|
|
||||||
fut_a: self.a.new_service(cfg),
|
|
||||||
fut_b: self.b.new_service(cfg),
|
|
||||||
fut_t: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct AndThenTransformFuture<T, A, B>
|
|
||||||
where
|
|
||||||
A: NewService,
|
|
||||||
B: NewService<InitError = A::InitError>,
|
|
||||||
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
|
|
||||||
T::Error: From<A::Error>,
|
|
||||||
{
|
|
||||||
fut_a: A::Future,
|
|
||||||
fut_b: B::Future,
|
|
||||||
fut_t: Option<T::Future>,
|
|
||||||
a: Option<A::Service>,
|
|
||||||
t: Option<T::Transform>,
|
|
||||||
t_cell: Rc<T>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, A, B> Future for AndThenTransformFuture<T, A, B>
|
|
||||||
where
|
|
||||||
A: NewService,
|
|
||||||
B: NewService<InitError = A::InitError>,
|
|
||||||
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
|
|
||||||
T::Error: From<A::Error>,
|
|
||||||
{
|
|
||||||
type Item = AndThen<FromErr<A::Service, T::Error>, T::Transform>;
|
|
||||||
type Error = T::InitError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
if self.fut_t.is_none() {
|
|
||||||
if let Async::Ready(service) = self.fut_b.poll()? {
|
|
||||||
self.fut_t = Some(self.t_cell.new_transform(service));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.a.is_none() {
|
|
||||||
if let Async::Ready(service) = self.fut_a.poll()? {
|
|
||||||
self.a = Some(service);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(ref mut fut) = self.fut_t {
|
|
||||||
if let Async::Ready(transform) = fut.poll()? {
|
|
||||||
self.t = Some(transform);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.a.is_some() && self.t.is_some() {
|
|
||||||
Ok(Async::Ready(AndThen::new(
|
|
||||||
FromErr::new(self.a.take().unwrap()),
|
|
||||||
self.t.take().unwrap(),
|
|
||||||
)))
|
|
||||||
} else {
|
|
||||||
Ok(Async::NotReady)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use futures::future::{ok, FutureResult};
|
|
||||||
use futures::{Async, Future, Poll};
|
|
||||||
|
|
||||||
use crate::{IntoNewService, IntoService, NewService, Service, ServiceExt};
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct Srv;
|
|
||||||
impl Service for Srv {
|
|
||||||
type Request = ();
|
|
||||||
type Response = ();
|
|
||||||
type Error = ();
|
|
||||||
type Future = FutureResult<(), ()>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, _: ()) -> Self::Future {
|
|
||||||
ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_apply() {
|
|
||||||
let blank = |req| Ok(req);
|
|
||||||
|
|
||||||
let mut srv = blank
|
|
||||||
.into_service()
|
|
||||||
.apply_fn(Srv, |req: &'static str, srv: &mut Srv| {
|
|
||||||
srv.call(()).map(move |res| (req, res))
|
|
||||||
});
|
|
||||||
assert!(srv.poll_ready().is_ok());
|
|
||||||
let res = srv.call("srv").poll();
|
|
||||||
assert!(res.is_ok());
|
|
||||||
assert_eq!(res.unwrap(), Async::Ready(("srv", ())));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_new_service() {
|
|
||||||
let blank = || Ok::<_, ()>((|req| Ok(req)).into_service());
|
|
||||||
|
|
||||||
let new_srv = blank.into_new_service().apply(
|
|
||||||
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
|
|
||||||
|| Ok(Srv),
|
|
||||||
);
|
|
||||||
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
|
|
||||||
assert!(srv.poll_ready().is_ok());
|
|
||||||
let res = srv.call("srv").poll();
|
|
||||||
assert!(res.is_ok());
|
|
||||||
assert_eq!(res.unwrap(), Async::Ready(("srv", ())));
|
|
||||||
} else {
|
|
||||||
panic!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,307 +0,0 @@
|
|||||||
use std::marker::PhantomData;
|
|
||||||
|
|
||||||
use futures::{Async, Future, IntoFuture, Poll};
|
|
||||||
|
|
||||||
use super::{IntoNewService, IntoService, NewService, Service};
|
|
||||||
use crate::cell::Cell;
|
|
||||||
|
|
||||||
/// `Apply` service combinator
|
|
||||||
pub struct AndThenApply<A, B, F, Out>
|
|
||||||
where
|
|
||||||
A: Service,
|
|
||||||
B: Service<Error = A::Error>,
|
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
|
||||||
a: A,
|
|
||||||
b: Cell<B>,
|
|
||||||
f: Cell<F>,
|
|
||||||
r: PhantomData<(Out,)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, B, F, Out> AndThenApply<A, B, F, Out>
|
|
||||||
where
|
|
||||||
A: Service,
|
|
||||||
B: Service<Error = A::Error>,
|
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
|
||||||
/// Create new `Apply` combinator
|
|
||||||
pub fn new<A1: IntoService<A>, B1: IntoService<B>>(a: A1, b: B1, f: F) -> Self {
|
|
||||||
Self {
|
|
||||||
f: Cell::new(f),
|
|
||||||
a: a.into_service(),
|
|
||||||
b: Cell::new(b.into_service()),
|
|
||||||
r: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, B, F, Out> Clone for AndThenApply<A, B, F, Out>
|
|
||||||
where
|
|
||||||
A: Service + Clone,
|
|
||||||
B: Service<Error = A::Error>,
|
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
AndThenApply {
|
|
||||||
a: self.a.clone(),
|
|
||||||
b: self.b.clone(),
|
|
||||||
f: self.f.clone(),
|
|
||||||
r: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, B, F, Out> Service for AndThenApply<A, B, F, Out>
|
|
||||||
where
|
|
||||||
A: Service,
|
|
||||||
B: Service<Error = A::Error>,
|
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
|
||||||
type Request = A::Request;
|
|
||||||
type Response = Out::Item;
|
|
||||||
type Error = A::Error;
|
|
||||||
type Future = AndThenApplyFuture<A, B, F, Out>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
let not_ready = self.a.poll_ready()?.is_not_ready();
|
|
||||||
if self.b.get_mut().poll_ready()?.is_not_ready() || not_ready {
|
|
||||||
Ok(Async::NotReady)
|
|
||||||
} else {
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
|
||||||
AndThenApplyFuture {
|
|
||||||
b: self.b.clone(),
|
|
||||||
f: self.f.clone(),
|
|
||||||
fut_b: None,
|
|
||||||
fut_a: Some(self.a.call(req)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct AndThenApplyFuture<A, B, F, Out>
|
|
||||||
where
|
|
||||||
A: Service,
|
|
||||||
B: Service<Error = A::Error>,
|
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
|
||||||
b: Cell<B>,
|
|
||||||
f: Cell<F>,
|
|
||||||
fut_a: Option<A::Future>,
|
|
||||||
fut_b: Option<Out::Future>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, B, F, Out> Future for AndThenApplyFuture<A, B, F, Out>
|
|
||||||
where
|
|
||||||
A: Service,
|
|
||||||
B: Service<Error = A::Error>,
|
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
|
||||||
type Item = Out::Item;
|
|
||||||
type Error = A::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
if let Some(ref mut fut) = self.fut_b {
|
|
||||||
return fut.poll().map_err(|e| e.into());
|
|
||||||
}
|
|
||||||
|
|
||||||
match self.fut_a.as_mut().expect("Bug in actix-service").poll() {
|
|
||||||
Ok(Async::Ready(resp)) => {
|
|
||||||
let _ = self.fut_a.take();
|
|
||||||
self.fut_b =
|
|
||||||
Some((&mut *self.f.get_mut())(resp, self.b.get_mut()).into_future());
|
|
||||||
self.poll()
|
|
||||||
}
|
|
||||||
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `ApplyNewService` new service combinator
|
|
||||||
pub struct AndThenApplyNewService<A, B, F, Out> {
|
|
||||||
a: A,
|
|
||||||
b: B,
|
|
||||||
f: Cell<F>,
|
|
||||||
r: PhantomData<Out>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, B, F, Out> AndThenApplyNewService<A, B, F, Out>
|
|
||||||
where
|
|
||||||
A: NewService,
|
|
||||||
B: NewService<Config = A::Config, Error = A::Error, InitError = A::InitError>,
|
|
||||||
F: FnMut(A::Response, &mut B::Service) -> Out,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
|
||||||
/// Create new `ApplyNewService` new service instance
|
|
||||||
pub fn new<A1: IntoNewService<A>, B1: IntoNewService<B>>(a: A1, b: B1, f: F) -> Self {
|
|
||||||
Self {
|
|
||||||
f: Cell::new(f),
|
|
||||||
a: a.into_new_service(),
|
|
||||||
b: b.into_new_service(),
|
|
||||||
r: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, B, F, Out> Clone for AndThenApplyNewService<A, B, F, Out>
|
|
||||||
where
|
|
||||||
A: Clone,
|
|
||||||
B: Clone,
|
|
||||||
{
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self {
|
|
||||||
a: self.a.clone(),
|
|
||||||
b: self.b.clone(),
|
|
||||||
f: self.f.clone(),
|
|
||||||
r: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, B, F, Out> NewService for AndThenApplyNewService<A, B, F, Out>
|
|
||||||
where
|
|
||||||
A: NewService,
|
|
||||||
B: NewService<Config = A::Config, Error = A::Error, InitError = A::InitError>,
|
|
||||||
F: FnMut(A::Response, &mut B::Service) -> Out,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
|
||||||
type Request = A::Request;
|
|
||||||
type Response = Out::Item;
|
|
||||||
type Error = A::Error;
|
|
||||||
type Service = AndThenApply<A::Service, B::Service, F, Out>;
|
|
||||||
type Config = A::Config;
|
|
||||||
type InitError = A::InitError;
|
|
||||||
type Future = AndThenApplyNewServiceFuture<A, B, F, Out>;
|
|
||||||
|
|
||||||
fn new_service(&self, cfg: &A::Config) -> Self::Future {
|
|
||||||
AndThenApplyNewServiceFuture {
|
|
||||||
a: None,
|
|
||||||
b: None,
|
|
||||||
f: self.f.clone(),
|
|
||||||
fut_a: self.a.new_service(cfg).into_future(),
|
|
||||||
fut_b: self.b.new_service(cfg).into_future(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct AndThenApplyNewServiceFuture<A, B, F, Out>
|
|
||||||
where
|
|
||||||
A: NewService,
|
|
||||||
B: NewService<Error = A::Error, InitError = A::InitError>,
|
|
||||||
F: FnMut(A::Response, &mut B::Service) -> Out,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
|
||||||
fut_b: B::Future,
|
|
||||||
fut_a: A::Future,
|
|
||||||
f: Cell<F>,
|
|
||||||
a: Option<A::Service>,
|
|
||||||
b: Option<B::Service>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, B, F, Out> Future for AndThenApplyNewServiceFuture<A, B, F, Out>
|
|
||||||
where
|
|
||||||
A: NewService,
|
|
||||||
B: NewService<Error = A::Error, InitError = A::InitError>,
|
|
||||||
F: FnMut(A::Response, &mut B::Service) -> Out,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
|
||||||
type Item = AndThenApply<A::Service, B::Service, F, Out>;
|
|
||||||
type Error = A::InitError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
if self.a.is_none() {
|
|
||||||
if let Async::Ready(service) = self.fut_a.poll()? {
|
|
||||||
self.a = Some(service);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.b.is_none() {
|
|
||||||
if let Async::Ready(service) = self.fut_b.poll()? {
|
|
||||||
self.b = Some(service);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.a.is_some() && self.b.is_some() {
|
|
||||||
Ok(Async::Ready(AndThenApply {
|
|
||||||
f: self.f.clone(),
|
|
||||||
a: self.a.take().unwrap(),
|
|
||||||
b: Cell::new(self.b.take().unwrap()),
|
|
||||||
r: PhantomData,
|
|
||||||
}))
|
|
||||||
} else {
|
|
||||||
Ok(Async::NotReady)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use futures::future::{ok, FutureResult};
|
|
||||||
use futures::{Async, Future, Poll};
|
|
||||||
|
|
||||||
use crate::blank::{Blank, BlankNewService};
|
|
||||||
use crate::{NewService, Service, ServiceExt};
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct Srv;
|
|
||||||
impl Service for Srv {
|
|
||||||
type Request = ();
|
|
||||||
type Response = ();
|
|
||||||
type Error = ();
|
|
||||||
type Future = FutureResult<(), ()>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, _: ()) -> Self::Future {
|
|
||||||
ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_call() {
|
|
||||||
let mut srv = Blank::new().apply_fn(Srv, |req: &'static str, srv| {
|
|
||||||
srv.call(()).map(move |res| (req, res))
|
|
||||||
});
|
|
||||||
assert!(srv.poll_ready().is_ok());
|
|
||||||
let res = srv.call("srv").poll();
|
|
||||||
assert!(res.is_ok());
|
|
||||||
assert_eq!(res.unwrap(), Async::Ready(("srv", ())));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_new_service() {
|
|
||||||
let new_srv = BlankNewService::new_unit().apply_fn(
|
|
||||||
|| Ok(Srv),
|
|
||||||
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
|
|
||||||
);
|
|
||||||
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
|
|
||||||
assert!(srv.poll_ready().is_ok());
|
|
||||||
let res = srv.call("srv").poll();
|
|
||||||
assert!(res.is_ok());
|
|
||||||
assert_eq!(res.unwrap(), Async::Ready(("srv", ())));
|
|
||||||
} else {
|
|
||||||
panic!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,175 +1,158 @@
|
|||||||
|
use pin_project::pin_project;
|
||||||
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use futures::{Async, Future, IntoFuture, Poll};
|
use super::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
||||||
|
|
||||||
use super::{IntoNewService, IntoService, NewService, Service};
|
|
||||||
|
|
||||||
/// Apply tranform function to a service
|
/// Apply tranform function to a service
|
||||||
pub fn apply_fn<T, F, In, Out, U>(service: U, f: F) -> Apply<T, F, In, Out>
|
pub fn apply_fn<T, F, R, In, Out, Err, U>(
|
||||||
|
service: U,
|
||||||
|
f: F,
|
||||||
|
) -> impl Service<Request = In, Response = Out, Error = Err>
|
||||||
where
|
where
|
||||||
T: Service,
|
T: Service<Error = Err>,
|
||||||
F: FnMut(In, &mut T) -> Out,
|
F: FnMut(In, &mut T) -> R,
|
||||||
Out: IntoFuture,
|
R: Future<Output = Result<Out, Err>>,
|
||||||
Out::Error: From<T::Error>,
|
|
||||||
U: IntoService<T>,
|
U: IntoService<T>,
|
||||||
{
|
{
|
||||||
Apply::new(service.into_service(), f)
|
Apply::new(service.into_service(), f)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create factory for `apply` service.
|
/// Create factory for `apply` service.
|
||||||
pub fn new_apply_fn<T, F, In, Out, U>(service: U, f: F) -> ApplyNewService<T, F, In, Out>
|
pub fn apply_fn_factory<T, F, R, In, Out, Err, U>(
|
||||||
|
service: U,
|
||||||
|
f: F,
|
||||||
|
) -> impl ServiceFactory<
|
||||||
|
Config = T::Config,
|
||||||
|
Request = In,
|
||||||
|
Response = Out,
|
||||||
|
Error = Err,
|
||||||
|
InitError = T::InitError,
|
||||||
|
>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: ServiceFactory<Error = Err>,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
F: FnMut(In, &mut T::Service) -> R + Clone,
|
||||||
Out: IntoFuture,
|
R: Future<Output = Result<Out, Err>>,
|
||||||
Out::Error: From<T::Error>,
|
U: IntoServiceFactory<T>,
|
||||||
U: IntoNewService<T>,
|
|
||||||
{
|
{
|
||||||
ApplyNewService::new(service.into_new_service(), f)
|
ApplyNewService::new(service.into_factory(), f)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
/// `Apply` service combinator
|
/// `Apply` service combinator
|
||||||
pub struct Apply<T, F, In, Out>
|
#[pin_project]
|
||||||
|
struct Apply<T, F, R, In, Out, Err>
|
||||||
where
|
where
|
||||||
T: Service,
|
T: Service<Error = Err>,
|
||||||
{
|
{
|
||||||
|
#[pin]
|
||||||
service: T,
|
service: T,
|
||||||
f: F,
|
f: F,
|
||||||
r: PhantomData<(In, Out)>,
|
r: PhantomData<(In, Out, R)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out> Apply<T, F, In, Out>
|
impl<T, F, R, In, Out, Err> Apply<T, F, R, In, Out, Err>
|
||||||
where
|
where
|
||||||
T: Service,
|
T: Service<Error = Err>,
|
||||||
F: FnMut(In, &mut T) -> Out,
|
F: FnMut(In, &mut T) -> R,
|
||||||
Out: IntoFuture,
|
R: Future<Output = Result<Out, Err>>,
|
||||||
Out::Error: From<T::Error>,
|
|
||||||
{
|
{
|
||||||
/// Create new `Apply` combinator
|
/// Create new `Apply` combinator
|
||||||
pub(crate) fn new<I: IntoService<T>>(service: I, f: F) -> Self {
|
fn new(service: T, f: F) -> Self {
|
||||||
Self {
|
Self {
|
||||||
service: service.into_service(),
|
service,
|
||||||
f,
|
f,
|
||||||
r: PhantomData,
|
r: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out> Clone for Apply<T, F, In, Out>
|
impl<T, F, R, In, Out, Err> Service for Apply<T, F, R, In, Out, Err>
|
||||||
where
|
where
|
||||||
T: Service + Clone,
|
T: Service<Error = Err>,
|
||||||
F: Clone,
|
F: FnMut(In, &mut T) -> R,
|
||||||
{
|
R: Future<Output = Result<Out, Err>>,
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Apply {
|
|
||||||
service: self.service.clone(),
|
|
||||||
f: self.f.clone(),
|
|
||||||
r: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, F, In, Out> Service for Apply<T, F, In, Out>
|
|
||||||
where
|
|
||||||
T: Service,
|
|
||||||
F: FnMut(In, &mut T) -> Out,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: From<T::Error>,
|
|
||||||
{
|
{
|
||||||
type Request = In;
|
type Request = In;
|
||||||
type Response = Out::Item;
|
type Response = Out;
|
||||||
type Error = Out::Error;
|
type Error = Err;
|
||||||
type Future = Out::Future;
|
type Future = R;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.service.poll_ready().map_err(|e| e.into())
|
Poll::Ready(futures::ready!(self.service.poll_ready(ctx)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: In) -> Self::Future {
|
fn call(&mut self, req: In) -> Self::Future {
|
||||||
(self.f)(req, &mut self.service).into_future()
|
(self.f)(req, &mut self.service)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `ApplyNewService` new service combinator
|
/// `ApplyNewService` new service combinator
|
||||||
pub struct ApplyNewService<T, F, In, Out>
|
struct ApplyNewService<T, F, R, In, Out, Err>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: ServiceFactory<Error = Err>,
|
||||||
{
|
{
|
||||||
service: T,
|
service: T,
|
||||||
f: F,
|
f: F,
|
||||||
r: PhantomData<(In, Out)>,
|
r: PhantomData<(R, In, Out)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out> ApplyNewService<T, F, In, Out>
|
impl<T, F, R, In, Out, Err> ApplyNewService<T, F, R, In, Out, Err>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: ServiceFactory<Error = Err>,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
F: FnMut(In, &mut T::Service) -> R + Clone,
|
||||||
Out: IntoFuture,
|
R: Future<Output = Result<Out, Err>>,
|
||||||
Out::Error: From<T::Error>,
|
|
||||||
{
|
{
|
||||||
/// Create new `ApplyNewService` new service instance
|
/// Create new `ApplyNewService` new service instance
|
||||||
pub(crate) fn new<F1: IntoNewService<T>>(service: F1, f: F) -> Self {
|
fn new(service: T, f: F) -> Self {
|
||||||
Self {
|
Self {
|
||||||
f,
|
f,
|
||||||
service: service.into_new_service(),
|
service,
|
||||||
r: PhantomData,
|
r: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out> Clone for ApplyNewService<T, F, In, Out>
|
impl<T, F, R, In, Out, Err> ServiceFactory for ApplyNewService<T, F, R, In, Out, Err>
|
||||||
where
|
where
|
||||||
T: NewService + Clone,
|
T: ServiceFactory<Error = Err>,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
F: FnMut(In, &mut T::Service) -> R + Clone,
|
||||||
Out: IntoFuture,
|
R: Future<Output = Result<Out, Err>>,
|
||||||
{
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self {
|
|
||||||
service: self.service.clone(),
|
|
||||||
f: self.f.clone(),
|
|
||||||
r: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, F, In, Out> NewService for ApplyNewService<T, F, In, Out>
|
|
||||||
where
|
|
||||||
T: NewService,
|
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: From<T::Error>,
|
|
||||||
{
|
{
|
||||||
type Request = In;
|
type Request = In;
|
||||||
type Response = Out::Item;
|
type Response = Out;
|
||||||
type Error = Out::Error;
|
type Error = Err;
|
||||||
|
|
||||||
type Config = T::Config;
|
type Config = T::Config;
|
||||||
type Service = Apply<T::Service, F, In, Out>;
|
type Service = Apply<T::Service, F, R, In, Out, Err>;
|
||||||
type InitError = T::InitError;
|
type InitError = T::InitError;
|
||||||
type Future = ApplyNewServiceFuture<T, F, In, Out>;
|
type Future = ApplyNewServiceFuture<T, F, R, In, Out, Err>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &T::Config) -> Self::Future {
|
fn new_service(&self, cfg: &T::Config) -> Self::Future {
|
||||||
ApplyNewServiceFuture::new(self.service.new_service(cfg), self.f.clone())
|
ApplyNewServiceFuture::new(self.service.new_service(cfg), self.f.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ApplyNewServiceFuture<T, F, In, Out>
|
#[pin_project]
|
||||||
|
struct ApplyNewServiceFuture<T, F, R, In, Out, Err>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: ServiceFactory<Error = Err>,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
F: FnMut(In, &mut T::Service) -> R + Clone,
|
||||||
Out: IntoFuture,
|
R: Future<Output = Result<Out, Err>>,
|
||||||
{
|
{
|
||||||
|
#[pin]
|
||||||
fut: T::Future,
|
fut: T::Future,
|
||||||
f: Option<F>,
|
f: Option<F>,
|
||||||
r: PhantomData<(In, Out)>,
|
r: PhantomData<(In, Out)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out> ApplyNewServiceFuture<T, F, In, Out>
|
impl<T, F, R, In, Out, Err> ApplyNewServiceFuture<T, F, R, In, Out, Err>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: ServiceFactory<Error = Err>,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
F: FnMut(In, &mut T::Service) -> R + Clone,
|
||||||
Out: IntoFuture,
|
R: Future<Output = Result<Out, Err>>,
|
||||||
{
|
{
|
||||||
fn new(fut: T::Future, f: F) -> Self {
|
fn new(fut: T::Future, f: F) -> Self {
|
||||||
ApplyNewServiceFuture {
|
ApplyNewServiceFuture {
|
||||||
@ -180,43 +163,44 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out> Future for ApplyNewServiceFuture<T, F, In, Out>
|
impl<T, F, R, In, Out, Err> Future for ApplyNewServiceFuture<T, F, R, In, Out, Err>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: ServiceFactory<Error = Err>,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
F: FnMut(In, &mut T::Service) -> R + Clone,
|
||||||
Out: IntoFuture,
|
R: Future<Output = Result<Out, Err>>,
|
||||||
Out::Error: From<T::Error>,
|
|
||||||
{
|
{
|
||||||
type Item = Apply<T::Service, F, In, Out>;
|
type Output = Result<Apply<T::Service, F, R, In, Out, Err>, T::InitError>;
|
||||||
type Error = T::InitError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
if let Async::Ready(service) = self.fut.poll()? {
|
let this = self.project();
|
||||||
Ok(Async::Ready(Apply::new(service, self.f.take().unwrap())))
|
if let Poll::Ready(svc) = this.fut.poll(cx)? {
|
||||||
|
Poll::Ready(Ok(Apply::new(svc, this.f.take().unwrap())))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use futures::future::{ok, FutureResult};
|
use std::task::{Context, Poll};
|
||||||
use futures::{Async, Future, Poll};
|
|
||||||
|
use futures::future::{lazy, ok, Ready};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::{IntoService, NewService, Service, ServiceExt};
|
use crate::{pipeline, pipeline_factory, Service, ServiceFactory};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Srv;
|
struct Srv;
|
||||||
|
|
||||||
impl Service for Srv {
|
impl Service for Srv {
|
||||||
type Request = ();
|
type Request = ();
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<(), ()>;
|
type Future = Ready<Result<(), ()>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, _: ()) -> Self::Future {
|
fn call(&mut self, _: ()) -> Self::Future {
|
||||||
@ -224,34 +208,42 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_call() {
|
async fn test_call() {
|
||||||
let blank = |req| Ok(req);
|
let mut srv = pipeline(apply_fn(Srv, |req: &'static str, srv| {
|
||||||
|
let fut = srv.call(());
|
||||||
|
async move {
|
||||||
|
let res = fut.await.unwrap();
|
||||||
|
Ok((req, res))
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
let mut srv = blank
|
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||||
.into_service()
|
|
||||||
.apply_fn(Srv, |req: &'static str, srv| {
|
let res = srv.call("srv").await;
|
||||||
srv.call(()).map(move |res| (req, res))
|
|
||||||
});
|
|
||||||
assert!(srv.poll_ready().is_ok());
|
|
||||||
let res = srv.call("srv").poll();
|
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), Async::Ready(("srv", ())));
|
assert_eq!(res.unwrap(), (("srv", ())));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_new_service() {
|
async fn test_new_service() {
|
||||||
let new_srv = ApplyNewService::new(
|
let new_srv = pipeline_factory(apply_fn_factory(
|
||||||
|| Ok::<_, ()>(Srv),
|
|| ok::<_, ()>(Srv),
|
||||||
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
|
|req: &'static str, srv| {
|
||||||
);
|
let fut = srv.call(());
|
||||||
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
|
async move {
|
||||||
assert!(srv.poll_ready().is_ok());
|
let res = fut.await.unwrap();
|
||||||
let res = srv.call("srv").poll();
|
Ok((req, res))
|
||||||
assert!(res.is_ok());
|
|
||||||
assert_eq!(res.unwrap(), Async::Ready(("srv", ())));
|
|
||||||
} else {
|
|
||||||
panic!()
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
));
|
||||||
|
|
||||||
|
let mut srv = new_srv.new_service(&()).await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||||
|
|
||||||
|
let res = srv.call("srv").await;
|
||||||
|
assert!(res.is_ok());
|
||||||
|
assert_eq!(res.unwrap(), (("srv", ())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,43 +1,45 @@
|
|||||||
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use futures::future::Future;
|
use futures::ready;
|
||||||
use futures::{try_ready, Async, IntoFuture, Poll};
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use crate::cell::Cell;
|
use crate::cell::Cell;
|
||||||
use crate::{IntoService, NewService, Service};
|
use crate::{IntoService, Service, ServiceFactory};
|
||||||
|
|
||||||
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
|
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
|
||||||
pub fn apply_cfg<F, C, T, R, S>(
|
pub fn apply_cfg<F, C, T, R, S, E>(
|
||||||
srv: T,
|
srv: T,
|
||||||
f: F,
|
f: F,
|
||||||
) -> impl NewService<
|
) -> impl ServiceFactory<
|
||||||
Config = C,
|
Config = C,
|
||||||
Request = S::Request,
|
Request = S::Request,
|
||||||
Response = S::Response,
|
Response = S::Response,
|
||||||
Error = S::Error,
|
Error = S::Error,
|
||||||
Service = S,
|
Service = S,
|
||||||
InitError = R::Error,
|
InitError = E,
|
||||||
> + Clone
|
> + Clone
|
||||||
where
|
where
|
||||||
F: FnMut(&C, &mut T) -> R,
|
F: FnMut(&C, &mut T) -> R,
|
||||||
T: Service,
|
T: Service,
|
||||||
R: IntoFuture,
|
R: Future<Output = Result<S, E>>,
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
ApplyConfigService {
|
ApplyConfigService {
|
||||||
f: Cell::new(f),
|
f: Cell::new(f),
|
||||||
srv: Cell::new(srv.into_service()),
|
srv: Cell::new(srv),
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
|
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
|
||||||
/// Service get constructor from NewService.
|
/// Service get constructor from NewService.
|
||||||
pub fn new_apply_cfg<F, C, T, R, S>(
|
pub fn apply_cfg_factory<F, C, T, R, S>(
|
||||||
srv: T,
|
srv: T,
|
||||||
f: F,
|
f: F,
|
||||||
) -> impl NewService<
|
) -> impl ServiceFactory<
|
||||||
Config = C,
|
Config = C,
|
||||||
Request = S::Request,
|
Request = S::Request,
|
||||||
Response = S::Response,
|
Response = S::Response,
|
||||||
@ -48,10 +50,9 @@ pub fn new_apply_cfg<F, C, T, R, S>(
|
|||||||
where
|
where
|
||||||
C: Clone,
|
C: Clone,
|
||||||
F: FnMut(&C, &mut T::Service) -> R,
|
F: FnMut(&C, &mut T::Service) -> R,
|
||||||
T: NewService<Config = ()>,
|
T: ServiceFactory<Config = ()>,
|
||||||
T::InitError: From<T::Error>,
|
T::InitError: From<T::Error>,
|
||||||
R: IntoFuture<Error = T::InitError>,
|
R: Future<Output = Result<S, T::InitError>>,
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
ApplyConfigNewService {
|
ApplyConfigNewService {
|
||||||
@ -61,26 +62,26 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService\
|
||||||
struct ApplyConfigService<F, C, T, R, S>
|
#[pin_project]
|
||||||
|
struct ApplyConfigService<F, C, T, R, S, E>
|
||||||
where
|
where
|
||||||
F: FnMut(&C, &mut T) -> R,
|
F: FnMut(&C, &mut T) -> R,
|
||||||
T: Service,
|
T: Service,
|
||||||
R: IntoFuture,
|
R: Future<Output = Result<S, E>>,
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
f: Cell<F>,
|
f: Cell<F>,
|
||||||
|
#[pin]
|
||||||
srv: Cell<T>,
|
srv: Cell<T>,
|
||||||
_t: PhantomData<(C, R, S)>,
|
_t: PhantomData<(C, R, S)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, C, T, R, S> Clone for ApplyConfigService<F, C, T, R, S>
|
impl<F, C, T, R, S, E> Clone for ApplyConfigService<F, C, T, R, S, E>
|
||||||
where
|
where
|
||||||
F: FnMut(&C, &mut T) -> R,
|
F: FnMut(&C, &mut T) -> R,
|
||||||
T: Service,
|
T: Service,
|
||||||
R: IntoFuture,
|
R: Future<Output = Result<S, E>>,
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
@ -92,12 +93,11 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, C, T, R, S> NewService for ApplyConfigService<F, C, T, R, S>
|
impl<F, C, T, R, S, E> ServiceFactory for ApplyConfigService<F, C, T, R, S, E>
|
||||||
where
|
where
|
||||||
F: FnMut(&C, &mut T) -> R,
|
F: FnMut(&C, &mut T) -> R,
|
||||||
T: Service,
|
T: Service,
|
||||||
R: IntoFuture,
|
R: Future<Output = Result<S, E>>,
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
type Config = C;
|
type Config = C;
|
||||||
@ -106,39 +106,37 @@ where
|
|||||||
type Error = S::Error;
|
type Error = S::Error;
|
||||||
type Service = S;
|
type Service = S;
|
||||||
|
|
||||||
type InitError = R::Error;
|
type InitError = E;
|
||||||
type Future = FnNewServiceConfigFut<R, S>;
|
type Future = FnNewServiceConfigFut<R, S, E>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||||
FnNewServiceConfigFut {
|
FnNewServiceConfigFut {
|
||||||
fut: unsafe { (self.f.get_mut_unsafe())(cfg, self.srv.get_mut_unsafe()) }
|
fut: unsafe { (self.f.get_mut_unsafe())(cfg, self.srv.get_mut_unsafe()) },
|
||||||
.into_future(),
|
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FnNewServiceConfigFut<R, S>
|
#[pin_project]
|
||||||
|
struct FnNewServiceConfigFut<R, S, E>
|
||||||
where
|
where
|
||||||
R: IntoFuture,
|
R: Future<Output = Result<S, E>>,
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
fut: R::Future,
|
#[pin]
|
||||||
|
fut: R,
|
||||||
_t: PhantomData<(S,)>,
|
_t: PhantomData<(S,)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R, S> Future for FnNewServiceConfigFut<R, S>
|
impl<R, S, E> Future for FnNewServiceConfigFut<R, S, E>
|
||||||
where
|
where
|
||||||
R: IntoFuture,
|
R: Future<Output = Result<S, E>>,
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
type Item = S;
|
type Output = Result<S, E>;
|
||||||
type Error = R::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
Ok(Async::Ready(try_ready!(self.fut.poll()).into_service()))
|
Poll::Ready(Ok(ready!(self.project().fut.poll(cx))?.into_service()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,9 +145,8 @@ struct ApplyConfigNewService<F, C, T, R, S>
|
|||||||
where
|
where
|
||||||
C: Clone,
|
C: Clone,
|
||||||
F: FnMut(&C, &mut T::Service) -> R,
|
F: FnMut(&C, &mut T::Service) -> R,
|
||||||
T: NewService<Config = ()>,
|
T: ServiceFactory<Config = ()>,
|
||||||
R: IntoFuture<Error = T::InitError>,
|
R: Future<Output = Result<S, T::InitError>>,
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
f: Cell<F>,
|
f: Cell<F>,
|
||||||
@ -161,9 +158,8 @@ impl<F, C, T, R, S> Clone for ApplyConfigNewService<F, C, T, R, S>
|
|||||||
where
|
where
|
||||||
C: Clone,
|
C: Clone,
|
||||||
F: FnMut(&C, &mut T::Service) -> R,
|
F: FnMut(&C, &mut T::Service) -> R,
|
||||||
T: NewService<Config = ()>,
|
T: ServiceFactory<Config = ()>,
|
||||||
R: IntoFuture<Error = T::InitError>,
|
R: Future<Output = Result<S, T::InitError>>,
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
@ -175,14 +171,13 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, C, T, R, S> NewService for ApplyConfigNewService<F, C, T, R, S>
|
impl<F, C, T, R, S> ServiceFactory for ApplyConfigNewService<F, C, T, R, S>
|
||||||
where
|
where
|
||||||
C: Clone,
|
C: Clone,
|
||||||
F: FnMut(&C, &mut T::Service) -> R,
|
F: FnMut(&C, &mut T::Service) -> R,
|
||||||
T: NewService<Config = ()>,
|
T: ServiceFactory<Config = ()>,
|
||||||
T::InitError: From<T::Error>,
|
T::InitError: From<T::Error>,
|
||||||
R: IntoFuture<Error = T::InitError>,
|
R: Future<Output = Result<S, T::InitError>>,
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
type Config = C;
|
type Config = C;
|
||||||
@ -191,7 +186,7 @@ where
|
|||||||
type Error = S::Error;
|
type Error = S::Error;
|
||||||
type Service = S;
|
type Service = S;
|
||||||
|
|
||||||
type InitError = R::Error;
|
type InitError = T::InitError;
|
||||||
type Future = ApplyConfigNewServiceFut<F, C, T, R, S>;
|
type Future = ApplyConfigNewServiceFut<F, C, T, R, S>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||||
@ -206,21 +201,23 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pin_project]
|
||||||
struct ApplyConfigNewServiceFut<F, C, T, R, S>
|
struct ApplyConfigNewServiceFut<F, C, T, R, S>
|
||||||
where
|
where
|
||||||
C: Clone,
|
C: Clone,
|
||||||
F: FnMut(&C, &mut T::Service) -> R,
|
F: FnMut(&C, &mut T::Service) -> R,
|
||||||
T: NewService<Config = ()>,
|
T: ServiceFactory<Config = ()>,
|
||||||
T::InitError: From<T::Error>,
|
T::InitError: From<T::Error>,
|
||||||
R: IntoFuture<Error = T::InitError>,
|
R: Future<Output = Result<S, T::InitError>>,
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
cfg: C,
|
cfg: C,
|
||||||
f: Cell<F>,
|
f: Cell<F>,
|
||||||
srv: Option<T::Service>,
|
srv: Option<T::Service>,
|
||||||
|
#[pin]
|
||||||
srv_fut: Option<T::Future>,
|
srv_fut: Option<T::Future>,
|
||||||
fut: Option<R::Future>,
|
#[pin]
|
||||||
|
fut: Option<R>,
|
||||||
_t: PhantomData<(S,)>,
|
_t: PhantomData<(S,)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,39 +225,40 @@ impl<F, C, T, R, S> Future for ApplyConfigNewServiceFut<F, C, T, R, S>
|
|||||||
where
|
where
|
||||||
C: Clone,
|
C: Clone,
|
||||||
F: FnMut(&C, &mut T::Service) -> R,
|
F: FnMut(&C, &mut T::Service) -> R,
|
||||||
T: NewService<Config = ()>,
|
T: ServiceFactory<Config = ()>,
|
||||||
T::InitError: From<T::Error>,
|
T::InitError: From<T::Error>,
|
||||||
R: IntoFuture<Error = T::InitError>,
|
R: Future<Output = Result<S, T::InitError>>,
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
type Item = S;
|
type Output = Result<S, T::InitError>;
|
||||||
type Error = R::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
if let Some(ref mut fut) = self.srv_fut {
|
let mut this = self.project();
|
||||||
match fut.poll()? {
|
'poll: loop {
|
||||||
Async::NotReady => return Ok(Async::NotReady),
|
if let Some(fut) = this.srv_fut.as_mut().as_pin_mut() {
|
||||||
Async::Ready(srv) => {
|
match fut.poll(cx)? {
|
||||||
let _ = self.srv_fut.take();
|
Poll::Pending => return Poll::Pending,
|
||||||
self.srv = Some(srv);
|
Poll::Ready(srv) => {
|
||||||
return self.poll();
|
this.srv_fut.set(None);
|
||||||
|
*this.srv = Some(srv);
|
||||||
|
continue 'poll;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref mut fut) = self.fut {
|
if let Some(fut) = this.fut.as_mut().as_pin_mut() {
|
||||||
Ok(Async::Ready(try_ready!(fut.poll()).into_service()))
|
return Poll::Ready(Ok(ready!(fut.poll(cx))?.into_service()));
|
||||||
} else if let Some(ref mut srv) = self.srv {
|
} else if let Some(ref mut srv) = this.srv {
|
||||||
match srv.poll_ready()? {
|
match srv.poll_ready(cx)? {
|
||||||
Async::NotReady => Ok(Async::NotReady),
|
Poll::Ready(_) => {
|
||||||
Async::Ready(_) => {
|
this.fut.set(Some(this.f.get_mut()(&this.cfg, srv)));
|
||||||
self.fut = Some(self.f.get_mut()(&self.cfg, srv).into_future());
|
continue 'poll;
|
||||||
return self.poll();
|
|
||||||
}
|
}
|
||||||
|
Poll::Pending => return Poll::Pending,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
return Poll::Pending;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,84 +0,0 @@
|
|||||||
use std::marker::PhantomData;
|
|
||||||
|
|
||||||
use futures::future::{ok, FutureResult};
|
|
||||||
use futures::{Async, Poll};
|
|
||||||
|
|
||||||
use super::{NewService, Service};
|
|
||||||
|
|
||||||
/// Empty service
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Blank<R, E> {
|
|
||||||
_t: PhantomData<(R, E)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R, E> Blank<R, E> {
|
|
||||||
pub fn err<E1>(self) -> Blank<R, E1> {
|
|
||||||
Blank { _t: PhantomData }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R> Blank<R, ()> {
|
|
||||||
#[allow(clippy::new_ret_no_self)]
|
|
||||||
pub fn new<E>() -> Blank<R, E> {
|
|
||||||
Blank { _t: PhantomData }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R, E> Default for Blank<R, E> {
|
|
||||||
fn default() -> Blank<R, E> {
|
|
||||||
Blank { _t: PhantomData }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R, E> Service for Blank<R, E> {
|
|
||||||
type Request = R;
|
|
||||||
type Response = R;
|
|
||||||
type Error = E;
|
|
||||||
type Future = FutureResult<R, E>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: R) -> Self::Future {
|
|
||||||
ok(req)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Empty service factory
|
|
||||||
pub struct BlankNewService<R, E1, E2 = ()> {
|
|
||||||
_t: PhantomData<(R, E1, E2)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R, E1, E2> BlankNewService<R, E1, E2> {
|
|
||||||
pub fn new() -> BlankNewService<R, E1, E2> {
|
|
||||||
BlankNewService { _t: PhantomData }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R, E1> BlankNewService<R, E1, ()> {
|
|
||||||
pub fn new_unit() -> BlankNewService<R, E1, ()> {
|
|
||||||
BlankNewService { _t: PhantomData }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R, E1, E2> Default for BlankNewService<R, E1, E2> {
|
|
||||||
fn default() -> BlankNewService<R, E1, E2> {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R, E1, E2> NewService for BlankNewService<R, E1, E2> {
|
|
||||||
type Request = R;
|
|
||||||
type Response = R;
|
|
||||||
type Error = E1;
|
|
||||||
|
|
||||||
type Config = ();
|
|
||||||
type Service = Blank<R, E1>;
|
|
||||||
type InitError = E2;
|
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
|
||||||
|
|
||||||
fn new_service(&self, _: &()) -> Self::Future {
|
|
||||||
ok(Blank::default())
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +1,8 @@
|
|||||||
use futures::future::{err, ok, Either, FutureResult};
|
use std::task::{Context, Poll};
|
||||||
use futures::{Async, Future, IntoFuture, Poll};
|
|
||||||
|
|
||||||
use crate::{NewService, Service};
|
use futures::future::{FutureExt, LocalBoxFuture};
|
||||||
|
|
||||||
|
use crate::{Service, ServiceFactory};
|
||||||
|
|
||||||
pub type BoxedService<Req, Res, Err> = Box<
|
pub type BoxedService<Req, Res, Err> = Box<
|
||||||
dyn Service<
|
dyn Service<
|
||||||
@ -12,17 +13,16 @@ pub type BoxedService<Req, Res, Err> = Box<
|
|||||||
>,
|
>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
pub type BoxedServiceResponse<Res, Err> =
|
pub type BoxedServiceResponse<Res, Err> = LocalBoxFuture<'static, Result<Res, Err>>;
|
||||||
Either<FutureResult<Res, Err>, Box<dyn Future<Item = Res, Error = Err>>>;
|
|
||||||
|
|
||||||
pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>);
|
pub struct BoxedNewService<C, Req, Res, Err, InitErr>(Inner<C, Req, Res, Err, InitErr>);
|
||||||
|
|
||||||
/// Create boxed new service
|
/// Create boxed new service
|
||||||
pub fn new_service<T>(
|
pub fn factory<T>(
|
||||||
service: T,
|
factory: T,
|
||||||
) -> BoxedNewService<T::Config, T::Request, T::Response, T::Error, T::InitError>
|
) -> BoxedNewService<T::Config, T::Request, T::Response, T::Error, T::InitError>
|
||||||
where
|
where
|
||||||
T: NewService + 'static,
|
T: ServiceFactory + 'static,
|
||||||
T::Request: 'static,
|
T::Request: 'static,
|
||||||
T::Response: 'static,
|
T::Response: 'static,
|
||||||
T::Service: 'static,
|
T::Service: 'static,
|
||||||
@ -30,8 +30,8 @@ where
|
|||||||
T::Error: 'static,
|
T::Error: 'static,
|
||||||
T::InitError: 'static,
|
T::InitError: 'static,
|
||||||
{
|
{
|
||||||
BoxedNewService(Box::new(NewServiceWrapper {
|
BoxedNewService(Box::new(FactoryWrapper {
|
||||||
service,
|
factory,
|
||||||
_t: std::marker::PhantomData,
|
_t: std::marker::PhantomData,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@ -46,18 +46,18 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Inner<C, Req, Res, Err, InitErr> = Box<
|
type Inner<C, Req, Res, Err, InitErr> = Box<
|
||||||
dyn NewService<
|
dyn ServiceFactory<
|
||||||
Config = C,
|
Config = C,
|
||||||
Request = Req,
|
Request = Req,
|
||||||
Response = Res,
|
Response = Res,
|
||||||
Error = Err,
|
Error = Err,
|
||||||
InitError = InitErr,
|
InitError = InitErr,
|
||||||
Service = BoxedService<Req, Res, Err>,
|
Service = BoxedService<Req, Res, Err>,
|
||||||
Future = Box<dyn Future<Item = BoxedService<Req, Res, Err>, Error = InitErr>>,
|
Future = LocalBoxFuture<'static, Result<BoxedService<Req, Res, Err>, InitErr>>,
|
||||||
>,
|
>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
impl<C, Req, Res, Err, InitErr> NewService for BoxedNewService<C, Req, Res, Err, InitErr>
|
impl<C, Req, Res, Err, InitErr> ServiceFactory for BoxedNewService<C, Req, Res, Err, InitErr>
|
||||||
where
|
where
|
||||||
Req: 'static,
|
Req: 'static,
|
||||||
Res: 'static,
|
Res: 'static,
|
||||||
@ -70,25 +70,32 @@ where
|
|||||||
type InitError = InitErr;
|
type InitError = InitErr;
|
||||||
type Config = C;
|
type Config = C;
|
||||||
type Service = BoxedService<Req, Res, Err>;
|
type Service = BoxedService<Req, Res, Err>;
|
||||||
type Future = Box<dyn Future<Item = Self::Service, Error = Self::InitError>>;
|
|
||||||
|
type Future = LocalBoxFuture<'static, Result<Self::Service, InitErr>>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||||
self.0.new_service(cfg)
|
self.0.new_service(cfg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct NewServiceWrapper<C, T: NewService> {
|
struct FactoryWrapper<C, T: ServiceFactory> {
|
||||||
service: T,
|
factory: T,
|
||||||
_t: std::marker::PhantomData<C>,
|
_t: std::marker::PhantomData<C>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C, T, Req, Res, Err, InitErr> NewService for NewServiceWrapper<C, T>
|
impl<C, T, Req, Res, Err, InitErr> ServiceFactory for FactoryWrapper<C, T>
|
||||||
where
|
where
|
||||||
Req: 'static,
|
Req: 'static,
|
||||||
Res: 'static,
|
Res: 'static,
|
||||||
Err: 'static,
|
Err: 'static,
|
||||||
InitErr: 'static,
|
InitErr: 'static,
|
||||||
T: NewService<Config = C, Request = Req, Response = Res, Error = Err, InitError = InitErr>,
|
T: ServiceFactory<
|
||||||
|
Config = C,
|
||||||
|
Request = Req,
|
||||||
|
Response = Res,
|
||||||
|
Error = Err,
|
||||||
|
InitError = InitErr,
|
||||||
|
>,
|
||||||
T::Future: 'static,
|
T::Future: 'static,
|
||||||
T::Service: 'static,
|
T::Service: 'static,
|
||||||
<T::Service as Service>::Future: 'static,
|
<T::Service as Service>::Future: 'static,
|
||||||
@ -99,15 +106,13 @@ where
|
|||||||
type InitError = InitErr;
|
type InitError = InitErr;
|
||||||
type Config = C;
|
type Config = C;
|
||||||
type Service = BoxedService<Req, Res, Err>;
|
type Service = BoxedService<Req, Res, Err>;
|
||||||
type Future = Box<dyn Future<Item = Self::Service, Error = Self::InitError>>;
|
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||||
Box::new(
|
self.factory
|
||||||
self.service
|
|
||||||
.new_service(cfg)
|
.new_service(cfg)
|
||||||
.into_future()
|
.map(|res| res.map(ServiceWrapper::boxed))
|
||||||
.map(ServiceWrapper::boxed),
|
.boxed_local()
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,21 +136,13 @@ where
|
|||||||
type Request = Req;
|
type Request = Req;
|
||||||
type Response = Res;
|
type Response = Res;
|
||||||
type Error = Err;
|
type Error = Err;
|
||||||
type Future = Either<
|
type Future = LocalBoxFuture<'static, Result<Res, Err>>;
|
||||||
FutureResult<Self::Response, Self::Error>,
|
|
||||||
Box<dyn Future<Item = Self::Response, Error = Self::Error>>,
|
|
||||||
>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.0.poll_ready()
|
self.0.poll_ready(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||||
let mut fut = self.0.call(req);
|
self.0.call(req).boxed_local()
|
||||||
match fut.poll() {
|
|
||||||
Ok(Async::Ready(res)) => Either::A(ok(res)),
|
|
||||||
Err(e) => Either::A(err(e)),
|
|
||||||
Ok(Async::NotReady) => Either::B(Box::new(fut)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
//! Custom cell impl
|
//! Custom cell impl, internal use only
|
||||||
use std::{cell::UnsafeCell, fmt, rc::Rc};
|
use std::{cell::UnsafeCell, fmt, rc::Rc};
|
||||||
|
|
||||||
pub(crate) struct Cell<T> {
|
pub(crate) struct Cell<T> {
|
||||||
|
@ -1,248 +1,245 @@
|
|||||||
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use futures::future::{ok, Future, FutureResult};
|
use futures::future::{ok, Ready};
|
||||||
use futures::{try_ready, Async, IntoFuture, Poll};
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use crate::{IntoNewService, IntoService, NewService, Service};
|
use crate::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
||||||
|
|
||||||
/// Create `NewService` for function that can act as a Service
|
/// Create `ServiceFactory` for function that can act as a `Service`
|
||||||
pub fn service_fn<F, Req, Out, Cfg>(f: F) -> NewServiceFn<F, Req, Out, Cfg>
|
pub fn service_fn<F, Fut, Req, Res, Err, Cfg>(
|
||||||
|
f: F,
|
||||||
|
) -> impl ServiceFactory<Config = Cfg, Request = Req, Response = Res, Error = Err, InitError = ()>
|
||||||
|
+ Clone
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Out + Clone,
|
F: FnMut(Req) -> Fut + Clone,
|
||||||
Out: IntoFuture,
|
Fut: Future<Output = Result<Res, Err>>,
|
||||||
{
|
{
|
||||||
NewServiceFn::new(f)
|
NewServiceFn::new(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create `NewService` for function that can produce services
|
pub fn service_fn2<F, Fut, Req, Res, Err>(
|
||||||
pub fn new_service_fn<F, C, R, S, E>(f: F) -> FnNewServiceNoConfig<F, C, R, S, E>
|
f: F,
|
||||||
|
) -> impl Service<Request = Req, Response = Res, Error = Err>
|
||||||
|
where
|
||||||
|
F: FnMut(Req) -> Fut,
|
||||||
|
Fut: Future<Output = Result<Res, Err>>,
|
||||||
|
{
|
||||||
|
ServiceFn::new(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create `ServiceFactory` for function that can produce services
|
||||||
|
pub fn factory_fn<S, F, Cfg, Fut, Err>(
|
||||||
|
f: F,
|
||||||
|
) -> impl ServiceFactory<
|
||||||
|
Config = Cfg,
|
||||||
|
Service = S,
|
||||||
|
Request = S::Request,
|
||||||
|
Response = S::Response,
|
||||||
|
Error = S::Error,
|
||||||
|
InitError = Err,
|
||||||
|
Future = Fut,
|
||||||
|
>
|
||||||
where
|
where
|
||||||
F: Fn() -> R,
|
|
||||||
R: IntoFuture<Item = S, Error = E>,
|
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
|
F: Fn() -> Fut,
|
||||||
|
Fut: Future<Output = Result<S, Err>>,
|
||||||
{
|
{
|
||||||
FnNewServiceNoConfig::new(f)
|
FnNewServiceNoConfig::new(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create `NewService` for function that can produce services with configuration
|
/// Create `ServiceFactory` for function that can produce services with configuration
|
||||||
pub fn new_service_cfg<F, C, R, S, E>(f: F) -> FnNewServiceConfig<F, C, R, S, E>
|
pub fn factory_fn_cfg<F, Fut, Cfg, Srv, Err>(
|
||||||
|
f: F,
|
||||||
|
) -> impl ServiceFactory<
|
||||||
|
Config = Cfg,
|
||||||
|
Service = Srv,
|
||||||
|
Request = Srv::Request,
|
||||||
|
Response = Srv::Response,
|
||||||
|
Error = Srv::Error,
|
||||||
|
InitError = Err,
|
||||||
|
>
|
||||||
where
|
where
|
||||||
F: Fn(&C) -> R,
|
F: Fn(&Cfg) -> Fut,
|
||||||
R: IntoFuture<Error = E>,
|
Fut: Future<Output = Result<Srv, Err>>,
|
||||||
R::Item: IntoService<S>,
|
Srv: Service,
|
||||||
S: Service,
|
|
||||||
{
|
{
|
||||||
FnNewServiceConfig::new(f)
|
FnNewServiceConfig::new(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ServiceFn<F, Req, Out>
|
pub struct ServiceFn<F, Fut, Req, Res, Err>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Out,
|
F: FnMut(Req) -> Fut,
|
||||||
Out: IntoFuture,
|
Fut: Future<Output = Result<Res, Err>>,
|
||||||
{
|
{
|
||||||
f: F,
|
f: F,
|
||||||
_t: PhantomData<Req>,
|
_t: PhantomData<Req>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Out> ServiceFn<F, Req, Out>
|
impl<F, Fut, Req, Res, Err> ServiceFn<F, Fut, Req, Res, Err>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Out,
|
F: FnMut(Req) -> Fut,
|
||||||
Out: IntoFuture,
|
Fut: Future<Output = Result<Res, Err>>,
|
||||||
{
|
{
|
||||||
pub(crate) fn new(f: F) -> Self {
|
pub(crate) fn new(f: F) -> Self {
|
||||||
ServiceFn { f, _t: PhantomData }
|
ServiceFn { f, _t: PhantomData }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Out> Clone for ServiceFn<F, Req, Out>
|
impl<F, Fut, Req, Res, Err> Clone for ServiceFn<F, Fut, Req, Res, Err>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Out + Clone,
|
F: FnMut(Req) -> Fut + Clone,
|
||||||
Out: IntoFuture,
|
Fut: Future<Output = Result<Res, Err>>,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
ServiceFn::new(self.f.clone())
|
ServiceFn::new(self.f.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Out> Service for ServiceFn<F, Req, Out>
|
impl<F, Fut, Req, Res, Err> Service for ServiceFn<F, Fut, Req, Res, Err>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Out,
|
F: FnMut(Req) -> Fut,
|
||||||
Out: IntoFuture,
|
Fut: Future<Output = Result<Res, Err>>,
|
||||||
{
|
{
|
||||||
type Request = Req;
|
type Request = Req;
|
||||||
type Response = Out::Item;
|
type Response = Res;
|
||||||
type Error = Out::Error;
|
type Error = Err;
|
||||||
type Future = Out::Future;
|
type Future = Fut;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Req) -> Self::Future {
|
fn call(&mut self, req: Req) -> Self::Future {
|
||||||
(self.f)(req).into_future()
|
(self.f)(req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Out> IntoService<ServiceFn<F, Req, Out>> for F
|
impl<F, Fut, Req, Res, Err> IntoService<ServiceFn<F, Fut, Req, Res, Err>> for F
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Out,
|
F: FnMut(Req) -> Fut,
|
||||||
Out: IntoFuture,
|
Fut: Future<Output = Result<Res, Err>>,
|
||||||
{
|
{
|
||||||
fn into_service(self) -> ServiceFn<F, Req, Out> {
|
fn into_service(self) -> ServiceFn<F, Fut, Req, Res, Err> {
|
||||||
ServiceFn::new(self)
|
ServiceFn::new(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct NewServiceFn<F, Req, Out, Cfg>
|
struct NewServiceFn<F, Fut, Req, Res, Err, Cfg>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Out,
|
F: FnMut(Req) -> Fut,
|
||||||
Out: IntoFuture,
|
Fut: Future<Output = Result<Res, Err>>,
|
||||||
{
|
{
|
||||||
f: F,
|
f: F,
|
||||||
_t: PhantomData<(Req, Cfg)>,
|
_t: PhantomData<(Req, Cfg)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Out, Cfg> NewServiceFn<F, Req, Out, Cfg>
|
impl<F, Fut, Req, Res, Err, Cfg> NewServiceFn<F, Fut, Req, Res, Err, Cfg>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Out + Clone,
|
F: FnMut(Req) -> Fut + Clone,
|
||||||
Out: IntoFuture,
|
Fut: Future<Output = Result<Res, Err>>,
|
||||||
{
|
{
|
||||||
pub(crate) fn new(f: F) -> Self {
|
fn new(f: F) -> Self {
|
||||||
NewServiceFn { f, _t: PhantomData }
|
NewServiceFn { f, _t: PhantomData }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Out, Cfg> Clone for NewServiceFn<F, Req, Out, Cfg>
|
impl<F, Fut, Req, Res, Err, Cfg> Clone for NewServiceFn<F, Fut, Req, Res, Err, Cfg>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Out + Clone,
|
F: FnMut(Req) -> Fut + Clone,
|
||||||
Out: IntoFuture,
|
Fut: Future<Output = Result<Res, Err>>,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
NewServiceFn::new(self.f.clone())
|
NewServiceFn::new(self.f.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Out, Cfg> NewService for NewServiceFn<F, Req, Out, Cfg>
|
impl<F, Fut, Req, Res, Err, Cfg> ServiceFactory for NewServiceFn<F, Fut, Req, Res, Err, Cfg>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Out + Clone,
|
F: FnMut(Req) -> Fut + Clone,
|
||||||
Out: IntoFuture,
|
Fut: Future<Output = Result<Res, Err>>,
|
||||||
{
|
{
|
||||||
type Request = Req;
|
type Request = Req;
|
||||||
type Response = Out::Item;
|
type Response = Res;
|
||||||
type Error = Out::Error;
|
type Error = Err;
|
||||||
|
|
||||||
type Config = Cfg;
|
type Config = Cfg;
|
||||||
type Service = ServiceFn<F, Req, Out>;
|
type Service = ServiceFn<F, Fut, Req, Res, Err>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, _: &Cfg) -> Self::Future {
|
fn new_service(&self, _: &Cfg) -> Self::Future {
|
||||||
ok(ServiceFn::new(self.f.clone()))
|
ok(ServiceFn::new(self.f.clone()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Out, Cfg> IntoService<ServiceFn<F, Req, Out>> for NewServiceFn<F, Req, Out, Cfg>
|
|
||||||
where
|
|
||||||
F: FnMut(Req) -> Out + Clone,
|
|
||||||
Out: IntoFuture,
|
|
||||||
{
|
|
||||||
fn into_service(self) -> ServiceFn<F, Req, Out> {
|
|
||||||
ServiceFn::new(self.f.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F, Req, Out, Cfg> IntoNewService<NewServiceFn<F, Req, Out, Cfg>> for F
|
|
||||||
where
|
|
||||||
F: Fn(Req) -> Out + Clone,
|
|
||||||
Out: IntoFuture,
|
|
||||||
{
|
|
||||||
fn into_new_service(self) -> NewServiceFn<F, Req, Out, Cfg> {
|
|
||||||
NewServiceFn::new(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
||||||
pub struct FnNewServiceConfig<F, C, R, S, E>
|
struct FnNewServiceConfig<F, Fut, Cfg, Srv, Err>
|
||||||
where
|
where
|
||||||
F: Fn(&C) -> R,
|
F: Fn(&Cfg) -> Fut,
|
||||||
R: IntoFuture<Error = E>,
|
Fut: Future<Output = Result<Srv, Err>>,
|
||||||
R::Item: IntoService<S>,
|
Srv: Service,
|
||||||
S: Service,
|
|
||||||
{
|
{
|
||||||
f: F,
|
f: F,
|
||||||
_t: PhantomData<(C, R, S, E)>,
|
_t: PhantomData<(Fut, Cfg, Srv, Err)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, C, R, S, E> FnNewServiceConfig<F, C, R, S, E>
|
impl<F, Fut, Cfg, Srv, Err> FnNewServiceConfig<F, Fut, Cfg, Srv, Err>
|
||||||
where
|
where
|
||||||
F: Fn(&C) -> R,
|
F: Fn(&Cfg) -> Fut,
|
||||||
R: IntoFuture<Error = E>,
|
Fut: Future<Output = Result<Srv, Err>>,
|
||||||
R::Item: IntoService<S>,
|
Srv: Service,
|
||||||
S: Service,
|
|
||||||
{
|
{
|
||||||
pub fn new(f: F) -> Self {
|
pub fn new(f: F) -> Self {
|
||||||
FnNewServiceConfig { f, _t: PhantomData }
|
FnNewServiceConfig { f, _t: PhantomData }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, C, R, S, E> NewService for FnNewServiceConfig<F, C, R, S, E>
|
impl<F, Fut, Cfg, Srv, Err> ServiceFactory for FnNewServiceConfig<F, Fut, Cfg, Srv, Err>
|
||||||
where
|
where
|
||||||
F: Fn(&C) -> R,
|
F: Fn(&Cfg) -> Fut,
|
||||||
R: IntoFuture<Error = E>,
|
Fut: Future<Output = Result<Srv, Err>>,
|
||||||
R::Item: IntoService<S>,
|
Srv: Service,
|
||||||
S: Service,
|
|
||||||
{
|
{
|
||||||
type Request = S::Request;
|
type Request = Srv::Request;
|
||||||
type Response = S::Response;
|
type Response = Srv::Response;
|
||||||
type Error = S::Error;
|
type Error = Srv::Error;
|
||||||
|
|
||||||
type Config = C;
|
type Config = Cfg;
|
||||||
type Service = S;
|
type Service = Srv;
|
||||||
type InitError = E;
|
type InitError = Err;
|
||||||
type Future = FnNewServiceConfigFut<R, S, E>;
|
type Future = FnNewServiceConfigFut<Fut, Srv, Err>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
fn new_service(&self, cfg: &Cfg) -> Self::Future {
|
||||||
FnNewServiceConfigFut {
|
FnNewServiceConfigFut {
|
||||||
fut: (self.f)(cfg).into_future(),
|
fut: (self.f)(cfg),
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FnNewServiceConfigFut<R, S, E>
|
#[pin_project]
|
||||||
|
struct FnNewServiceConfigFut<R, S, E>
|
||||||
where
|
where
|
||||||
R: IntoFuture<Error = E>,
|
R: Future<Output = Result<S, E>>,
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
fut: R::Future,
|
#[pin]
|
||||||
|
fut: R,
|
||||||
_t: PhantomData<(S,)>,
|
_t: PhantomData<(S,)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R, S, E> Future for FnNewServiceConfigFut<R, S, E>
|
impl<R, S, E> Future for FnNewServiceConfigFut<R, S, E>
|
||||||
where
|
where
|
||||||
R: IntoFuture<Error = E>,
|
R: Future<Output = Result<S, E>>,
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
type Item = S;
|
type Output = Result<S, E>;
|
||||||
type Error = R::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
Ok(Async::Ready(try_ready!(self.fut.poll()).into_service()))
|
Poll::Ready(Ok(futures::ready!(self.project().fut.poll(cx))?))
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F, C, R, S, E> Clone for FnNewServiceConfig<F, C, R, S, E>
|
|
||||||
where
|
|
||||||
F: Fn(&C) -> R + Clone,
|
|
||||||
R: IntoFuture<Error = E>,
|
|
||||||
R::Item: IntoService<S>,
|
|
||||||
S: Service,
|
|
||||||
{
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self::new(self.f.clone())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,7 +247,7 @@ where
|
|||||||
pub struct FnNewServiceNoConfig<F, C, R, S, E>
|
pub struct FnNewServiceNoConfig<F, C, R, S, E>
|
||||||
where
|
where
|
||||||
F: Fn() -> R,
|
F: Fn() -> R,
|
||||||
R: IntoFuture<Item = S, Error = E>,
|
R: Future<Output = Result<S, E>>,
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
f: F,
|
f: F,
|
||||||
@ -260,18 +257,18 @@ where
|
|||||||
impl<F, C, R, S, E> FnNewServiceNoConfig<F, C, R, S, E>
|
impl<F, C, R, S, E> FnNewServiceNoConfig<F, C, R, S, E>
|
||||||
where
|
where
|
||||||
F: Fn() -> R,
|
F: Fn() -> R,
|
||||||
R: IntoFuture<Item = S, Error = E>,
|
R: Future<Output = Result<S, E>>,
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
pub fn new(f: F) -> Self {
|
fn new(f: F) -> Self {
|
||||||
FnNewServiceNoConfig { f, _t: PhantomData }
|
FnNewServiceNoConfig { f, _t: PhantomData }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, C, R, S, E> NewService for FnNewServiceNoConfig<F, C, R, S, E>
|
impl<F, C, R, S, E> ServiceFactory for FnNewServiceNoConfig<F, C, R, S, E>
|
||||||
where
|
where
|
||||||
F: Fn() -> R,
|
F: Fn() -> R,
|
||||||
R: IntoFuture<Item = S, Error = E>,
|
R: Future<Output = Result<S, E>>,
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
type Request = S::Request;
|
type Request = S::Request;
|
||||||
@ -280,17 +277,17 @@ where
|
|||||||
type Service = S;
|
type Service = S;
|
||||||
type Config = C;
|
type Config = C;
|
||||||
type InitError = E;
|
type InitError = E;
|
||||||
type Future = R::Future;
|
type Future = R;
|
||||||
|
|
||||||
fn new_service(&self, _: &C) -> Self::Future {
|
fn new_service(&self, _: &C) -> Self::Future {
|
||||||
(self.f)().into_future()
|
(self.f)()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, C, R, S, E> Clone for FnNewServiceNoConfig<F, C, R, S, E>
|
impl<F, C, R, S, E> Clone for FnNewServiceNoConfig<F, C, R, S, E>
|
||||||
where
|
where
|
||||||
F: Fn() -> R + Clone,
|
F: Fn() -> R + Clone,
|
||||||
R: IntoFuture<Item = S, Error = E>,
|
R: Future<Output = Result<S, E>>,
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
@ -298,13 +295,13 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, C, R, S, E> IntoNewService<FnNewServiceNoConfig<F, C, R, S, E>> for F
|
impl<F, C, R, S, E> IntoServiceFactory<FnNewServiceNoConfig<F, C, R, S, E>> for F
|
||||||
where
|
where
|
||||||
F: Fn() -> R,
|
F: Fn() -> R,
|
||||||
R: IntoFuture<Item = S, Error = E>,
|
R: Future<Output = Result<S, E>>,
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
fn into_new_service(self) -> FnNewServiceNoConfig<F, C, R, S, E> {
|
fn into_factory(self) -> FnNewServiceNoConfig<F, C, R, S, E> {
|
||||||
FnNewServiceNoConfig::new(self)
|
FnNewServiceNoConfig::new(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,80 +0,0 @@
|
|||||||
use std::marker::PhantomData;
|
|
||||||
|
|
||||||
use futures::future::{ok, FutureResult};
|
|
||||||
use futures::IntoFuture;
|
|
||||||
|
|
||||||
use crate::apply::Apply;
|
|
||||||
use crate::{IntoTransform, Service, Transform};
|
|
||||||
|
|
||||||
/// Use function as transform service
|
|
||||||
pub fn transform_fn<F, S, In, Out, Err>(
|
|
||||||
f: F,
|
|
||||||
) -> impl Transform<S, Request = In, Response = Out::Item, Error = Out::Error, InitError = Err>
|
|
||||||
where
|
|
||||||
S: Service,
|
|
||||||
F: FnMut(In, &mut S) -> Out + Clone,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: From<S::Error>,
|
|
||||||
{
|
|
||||||
FnTransform::new(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct FnTransform<F, S, In, Out, Err>
|
|
||||||
where
|
|
||||||
F: FnMut(In, &mut S) -> Out + Clone,
|
|
||||||
Out: IntoFuture,
|
|
||||||
{
|
|
||||||
f: F,
|
|
||||||
_t: PhantomData<(S, In, Out, Err)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F, S, In, Out, Err> FnTransform<F, S, In, Out, Err>
|
|
||||||
where
|
|
||||||
F: FnMut(In, &mut S) -> Out + Clone,
|
|
||||||
Out: IntoFuture,
|
|
||||||
{
|
|
||||||
pub fn new(f: F) -> Self {
|
|
||||||
FnTransform { f, _t: PhantomData }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F, S, In, Out, Err> Transform<S> for FnTransform<F, S, In, Out, Err>
|
|
||||||
where
|
|
||||||
S: Service,
|
|
||||||
F: FnMut(In, &mut S) -> Out + Clone,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: From<S::Error>,
|
|
||||||
{
|
|
||||||
type Request = In;
|
|
||||||
type Response = Out::Item;
|
|
||||||
type Error = Out::Error;
|
|
||||||
type Transform = Apply<S, F, In, Out>;
|
|
||||||
type InitError = Err;
|
|
||||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
|
||||||
|
|
||||||
fn new_transform(&self, service: S) -> Self::Future {
|
|
||||||
ok(Apply::new(service, self.f.clone()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F, S, In, Out, Err> IntoTransform<FnTransform<F, S, In, Out, Err>, S> for F
|
|
||||||
where
|
|
||||||
S: Service,
|
|
||||||
F: FnMut(In, &mut S) -> Out + Clone,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: From<S::Error>,
|
|
||||||
{
|
|
||||||
fn into_transform(self) -> FnTransform<F, S, In, Out, Err> {
|
|
||||||
FnTransform::new(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F, S, In, Out, Err> Clone for FnTransform<F, S, In, Out, Err>
|
|
||||||
where
|
|
||||||
F: FnMut(In, &mut S) -> Out + Clone,
|
|
||||||
Out: IntoFuture,
|
|
||||||
{
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self::new(self.f.clone())
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,220 +0,0 @@
|
|||||||
use std::marker::PhantomData;
|
|
||||||
|
|
||||||
use futures::{Async, Future, Poll};
|
|
||||||
|
|
||||||
use super::{NewService, Service};
|
|
||||||
|
|
||||||
/// Service for the `from_err` combinator, changing the error type of a service.
|
|
||||||
///
|
|
||||||
/// This is created by the `ServiceExt::from_err` method.
|
|
||||||
pub struct FromErr<A, E> {
|
|
||||||
service: A,
|
|
||||||
f: PhantomData<E>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, E> FromErr<A, E> {
|
|
||||||
pub(crate) fn new(service: A) -> Self
|
|
||||||
where
|
|
||||||
A: Service,
|
|
||||||
E: From<A::Error>,
|
|
||||||
{
|
|
||||||
FromErr {
|
|
||||||
service,
|
|
||||||
f: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, E> Clone for FromErr<A, E>
|
|
||||||
where
|
|
||||||
A: Clone,
|
|
||||||
{
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
FromErr {
|
|
||||||
service: self.service.clone(),
|
|
||||||
f: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, E> Service for FromErr<A, E>
|
|
||||||
where
|
|
||||||
A: Service,
|
|
||||||
E: From<A::Error>,
|
|
||||||
{
|
|
||||||
type Request = A::Request;
|
|
||||||
type Response = A::Response;
|
|
||||||
type Error = E;
|
|
||||||
type Future = FromErrFuture<A, E>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), E> {
|
|
||||||
self.service.poll_ready().map_err(E::from)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
|
||||||
FromErrFuture {
|
|
||||||
fut: self.service.call(req),
|
|
||||||
f: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct FromErrFuture<A: Service, E> {
|
|
||||||
fut: A::Future,
|
|
||||||
f: PhantomData<E>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, E> Future for FromErrFuture<A, E>
|
|
||||||
where
|
|
||||||
A: Service,
|
|
||||||
E: From<A::Error>,
|
|
||||||
{
|
|
||||||
type Item = A::Response;
|
|
||||||
type Error = E;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
self.fut.poll().map_err(E::from)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// NewService for the `from_err` combinator, changing the type of a new
|
|
||||||
/// service's error.
|
|
||||||
///
|
|
||||||
/// This is created by the `NewServiceExt::from_err` method.
|
|
||||||
pub struct FromErrNewService<A, E> {
|
|
||||||
a: A,
|
|
||||||
e: PhantomData<E>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, E> FromErrNewService<A, E> {
|
|
||||||
/// Create new `FromErr` new service instance
|
|
||||||
pub fn new(a: A) -> Self
|
|
||||||
where
|
|
||||||
A: NewService,
|
|
||||||
E: From<A::Error>,
|
|
||||||
{
|
|
||||||
Self { a, e: PhantomData }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, E> Clone for FromErrNewService<A, E>
|
|
||||||
where
|
|
||||||
A: Clone,
|
|
||||||
{
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self {
|
|
||||||
a: self.a.clone(),
|
|
||||||
e: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, E> NewService for FromErrNewService<A, E>
|
|
||||||
where
|
|
||||||
A: NewService,
|
|
||||||
E: From<A::Error>,
|
|
||||||
{
|
|
||||||
type Request = A::Request;
|
|
||||||
type Response = A::Response;
|
|
||||||
type Error = E;
|
|
||||||
|
|
||||||
type Config = A::Config;
|
|
||||||
type Service = FromErr<A::Service, E>;
|
|
||||||
type InitError = A::InitError;
|
|
||||||
type Future = FromErrNewServiceFuture<A, E>;
|
|
||||||
|
|
||||||
fn new_service(&self, cfg: &A::Config) -> Self::Future {
|
|
||||||
FromErrNewServiceFuture {
|
|
||||||
fut: self.a.new_service(cfg),
|
|
||||||
e: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct FromErrNewServiceFuture<A, E>
|
|
||||||
where
|
|
||||||
A: NewService,
|
|
||||||
E: From<A::Error>,
|
|
||||||
{
|
|
||||||
fut: A::Future,
|
|
||||||
e: PhantomData<E>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A, E> Future for FromErrNewServiceFuture<A, E>
|
|
||||||
where
|
|
||||||
A: NewService,
|
|
||||||
E: From<A::Error>,
|
|
||||||
{
|
|
||||||
type Item = FromErr<A::Service, E>;
|
|
||||||
type Error = A::InitError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
if let Async::Ready(service) = self.fut.poll()? {
|
|
||||||
Ok(Async::Ready(FromErr::new(service)))
|
|
||||||
} else {
|
|
||||||
Ok(Async::NotReady)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use futures::future::{err, FutureResult};
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
use crate::{IntoNewService, NewService, Service, ServiceExt};
|
|
||||||
|
|
||||||
struct Srv;
|
|
||||||
impl Service for Srv {
|
|
||||||
type Request = ();
|
|
||||||
type Response = ();
|
|
||||||
type Error = ();
|
|
||||||
type Future = FutureResult<(), ()>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
Err(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, _: ()) -> Self::Future {
|
|
||||||
err(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
|
||||||
struct Error;
|
|
||||||
|
|
||||||
impl From<()> for Error {
|
|
||||||
fn from(_: ()) -> Self {
|
|
||||||
Error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_poll_ready() {
|
|
||||||
let mut srv = Srv.from_err::<Error>();
|
|
||||||
let res = srv.poll_ready();
|
|
||||||
assert!(res.is_err());
|
|
||||||
assert_eq!(res.err().unwrap(), Error);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_call() {
|
|
||||||
let mut srv = Srv.from_err::<Error>();
|
|
||||||
let res = srv.call(()).poll();
|
|
||||||
assert!(res.is_err());
|
|
||||||
assert_eq!(res.err().unwrap(), Error);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_new_service() {
|
|
||||||
let blank = || Ok::<_, ()>(Srv);
|
|
||||||
let new_srv = blank.into_new_service().from_err::<Error>();
|
|
||||||
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
|
|
||||||
let res = srv.call(()).poll();
|
|
||||||
assert!(res.is_err());
|
|
||||||
assert_eq!(res.err().unwrap(), Error);
|
|
||||||
} else {
|
|
||||||
panic!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
204
actix-service/src/into.rs
Normal file
204
actix-service/src/into.rs
Normal file
@ -0,0 +1,204 @@
|
|||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
use crate::map::{Map, MapNewService};
|
||||||
|
use crate::map_err::{MapErr, MapErrNewService};
|
||||||
|
use crate::map_init_err::MapInitErr;
|
||||||
|
use crate::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
/// Convert object of type `U` to a service `T`
|
||||||
|
pub fn into_service<T, U>(service: U) -> ServiceMapper<T>
|
||||||
|
where
|
||||||
|
U: IntoService<T>,
|
||||||
|
T: Service,
|
||||||
|
{
|
||||||
|
ServiceMapper {
|
||||||
|
service: service.into_service(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_factory<T, F>(factory: F) -> ServiceFactoryMapper<T>
|
||||||
|
where
|
||||||
|
T: ServiceFactory,
|
||||||
|
F: IntoServiceFactory<T>,
|
||||||
|
{
|
||||||
|
ServiceFactoryMapper {
|
||||||
|
factory: factory.into_factory(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ServiceMapper<T> {
|
||||||
|
service: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ServiceFactoryMapper<T> {
|
||||||
|
factory: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Service> ServiceMapper<T> {
|
||||||
|
/// Map this service's output to a different type, returning a new service
|
||||||
|
/// of the resulting type.
|
||||||
|
///
|
||||||
|
/// This function is similar to the `Option::map` or `Iterator::map` where
|
||||||
|
/// it will change the type of the underlying service.
|
||||||
|
///
|
||||||
|
/// Note that this function consumes the receiving service and returns a
|
||||||
|
/// wrapped version of it, similar to the existing `map` methods in the
|
||||||
|
/// standard library.
|
||||||
|
pub fn map<F, R>(
|
||||||
|
self,
|
||||||
|
f: F,
|
||||||
|
) -> ServiceMapper<impl Service<Request = T::Request, Response = R, Error = T::Error>>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: FnMut(T::Response) -> R + Clone,
|
||||||
|
{
|
||||||
|
ServiceMapper {
|
||||||
|
service: Map::new(self.service, f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Map this service's error to a different error, returning a new service.
|
||||||
|
///
|
||||||
|
/// This function is similar to the `Result::map_err` where it will change
|
||||||
|
/// the error type of the underlying service. This is useful for example to
|
||||||
|
/// ensure that services have the same error type.
|
||||||
|
///
|
||||||
|
/// Note that this function consumes the receiving service and returns a
|
||||||
|
/// wrapped version of it.
|
||||||
|
pub fn map_err<F, E>(
|
||||||
|
self,
|
||||||
|
f: F,
|
||||||
|
) -> ServiceMapper<impl Service<Request = T::Request, Response = T::Response, Error = E>>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: Fn(T::Error) -> E + Clone,
|
||||||
|
{
|
||||||
|
ServiceMapper {
|
||||||
|
service: MapErr::new(self, f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Clone for ServiceMapper<T>
|
||||||
|
where
|
||||||
|
T: Clone,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
ServiceMapper {
|
||||||
|
service: self.service.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Service> Service for ServiceMapper<T> {
|
||||||
|
type Request = T::Request;
|
||||||
|
type Response = T::Response;
|
||||||
|
type Error = T::Error;
|
||||||
|
type Future = T::Future;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
|
self.service.poll_ready(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn call(&mut self, req: T::Request) -> Self::Future {
|
||||||
|
self.service.call(req)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ServiceFactory> ServiceFactoryMapper<T> {
|
||||||
|
/// Map this service's output to a different type, returning a new service
|
||||||
|
/// of the resulting type.
|
||||||
|
pub fn map<F, R>(
|
||||||
|
self,
|
||||||
|
f: F,
|
||||||
|
) -> ServiceFactoryMapper<
|
||||||
|
impl ServiceFactory<
|
||||||
|
Config = T::Config,
|
||||||
|
Request = T::Request,
|
||||||
|
Response = R,
|
||||||
|
Error = T::Error,
|
||||||
|
InitError = T::InitError,
|
||||||
|
>,
|
||||||
|
>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: FnMut(T::Response) -> R + Clone,
|
||||||
|
{
|
||||||
|
ServiceFactoryMapper {
|
||||||
|
factory: MapNewService::new(self.factory, f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Map this service's error to a different error, returning a new service.
|
||||||
|
pub fn map_err<F, E>(
|
||||||
|
self,
|
||||||
|
f: F,
|
||||||
|
) -> ServiceFactoryMapper<
|
||||||
|
impl ServiceFactory<
|
||||||
|
Config = T::Config,
|
||||||
|
Request = T::Request,
|
||||||
|
Response = T::Response,
|
||||||
|
Error = E,
|
||||||
|
InitError = T::InitError,
|
||||||
|
>,
|
||||||
|
>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: Fn(T::Error) -> E + Clone,
|
||||||
|
{
|
||||||
|
ServiceFactoryMapper {
|
||||||
|
factory: MapErrNewService::new(self.factory, f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Map this factory's init error to a different error, returning a new service.
|
||||||
|
pub fn map_init_err<F, E>(
|
||||||
|
self,
|
||||||
|
f: F,
|
||||||
|
) -> ServiceFactoryMapper<
|
||||||
|
impl ServiceFactory<
|
||||||
|
Config = T::Config,
|
||||||
|
Request = T::Request,
|
||||||
|
Response = T::Response,
|
||||||
|
Error = T::Error,
|
||||||
|
InitError = E,
|
||||||
|
>,
|
||||||
|
>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: Fn(T::InitError) -> E + Clone,
|
||||||
|
{
|
||||||
|
ServiceFactoryMapper {
|
||||||
|
factory: MapInitErr::new(self.factory, f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Clone for ServiceFactoryMapper<T>
|
||||||
|
where
|
||||||
|
T: Clone,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
ServiceFactoryMapper {
|
||||||
|
factory: self.factory.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ServiceFactory> ServiceFactory for ServiceFactoryMapper<T> {
|
||||||
|
type Config = T::Config;
|
||||||
|
type Request = T::Request;
|
||||||
|
type Response = T::Response;
|
||||||
|
type Error = T::Error;
|
||||||
|
type Service = T::Service;
|
||||||
|
type InitError = T::InitError;
|
||||||
|
type Future = T::Future;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn new_service(&self, cfg: &T::Config) -> Self::Future {
|
||||||
|
self.factory.new_service(cfg)
|
||||||
|
}
|
||||||
|
}
|
@ -1,43 +1,32 @@
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::future::Future;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::task::{self, Context, Poll};
|
||||||
use futures::{Future, IntoFuture, Poll};
|
|
||||||
|
|
||||||
mod and_then;
|
mod and_then;
|
||||||
mod and_then_apply;
|
|
||||||
mod and_then_apply_fn;
|
|
||||||
mod apply;
|
mod apply;
|
||||||
mod apply_cfg;
|
mod apply_cfg;
|
||||||
pub mod blank;
|
|
||||||
pub mod boxed;
|
pub mod boxed;
|
||||||
mod cell;
|
mod cell;
|
||||||
mod fn_service;
|
mod fn_service;
|
||||||
mod fn_transform;
|
mod into;
|
||||||
mod from_err;
|
|
||||||
mod map;
|
mod map;
|
||||||
mod map_config;
|
mod map_config;
|
||||||
mod map_err;
|
mod map_err;
|
||||||
mod map_init_err;
|
mod map_init_err;
|
||||||
|
mod pipeline;
|
||||||
mod then;
|
mod then;
|
||||||
mod transform;
|
mod transform;
|
||||||
mod transform_err;
|
mod transform_err;
|
||||||
|
|
||||||
pub use self::and_then::{AndThen, AndThenNewService};
|
pub use self::apply::{apply_fn, apply_fn_factory};
|
||||||
pub use self::apply::{apply_fn, new_apply_fn, Apply, ApplyNewService};
|
pub use self::apply_cfg::{apply_cfg, apply_cfg_factory};
|
||||||
pub use self::apply_cfg::{apply_cfg, new_apply_cfg};
|
pub use self::fn_service::{factory_fn, factory_fn_cfg, service_fn, service_fn2};
|
||||||
pub use self::fn_service::{new_service_cfg, new_service_fn, service_fn, ServiceFn};
|
pub use self::into::{into_factory, into_service, ServiceFactoryMapper, ServiceMapper};
|
||||||
pub use self::fn_transform::transform_fn;
|
pub use self::map_config::{map_config, unit_config, MappedConfig};
|
||||||
pub use self::from_err::{FromErr, FromErrNewService};
|
pub use self::pipeline::{pipeline, pipeline_factory, Pipeline, PipelineFactory};
|
||||||
pub use self::map::{Map, MapNewService};
|
pub use self::transform::{apply, Transform};
|
||||||
pub use self::map_config::{MapConfig, MappedConfig, UnitConfig};
|
|
||||||
pub use self::map_err::{MapErr, MapErrNewService};
|
|
||||||
pub use self::map_init_err::MapInitErr;
|
|
||||||
pub use self::then::{Then, ThenNewService};
|
|
||||||
pub use self::transform::{apply_transform, IntoTransform, Transform};
|
|
||||||
|
|
||||||
use self::and_then_apply::AndThenTransform;
|
|
||||||
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
|
|
||||||
|
|
||||||
/// An asynchronous function from `Request` to a `Response`.
|
/// An asynchronous function from `Request` to a `Response`.
|
||||||
pub trait Service {
|
pub trait Service {
|
||||||
@ -51,7 +40,7 @@ pub trait Service {
|
|||||||
type Error;
|
type Error;
|
||||||
|
|
||||||
/// The future response value.
|
/// The future response value.
|
||||||
type Future: Future<Item = Self::Response, Error = Self::Error>;
|
type Future: Future<Output = Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
/// Returns `Ready` when the service is able to process requests.
|
/// Returns `Ready` when the service is able to process requests.
|
||||||
///
|
///
|
||||||
@ -62,7 +51,7 @@ pub trait Service {
|
|||||||
/// This is a **best effort** implementation. False positives are permitted.
|
/// This is a **best effort** implementation. False positives are permitted.
|
||||||
/// It is permitted for the service to return `Ready` from a `poll_ready`
|
/// It is permitted for the service to return `Ready` from a `poll_ready`
|
||||||
/// call and the next invocation of `call` results in an error.
|
/// call and the next invocation of `call` results in an error.
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error>;
|
fn poll_ready(&mut self, ctx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>>;
|
||||||
|
|
||||||
/// Process the request and return the response asynchronously.
|
/// Process the request and return the response asynchronously.
|
||||||
///
|
///
|
||||||
@ -76,113 +65,16 @@ pub trait Service {
|
|||||||
fn call(&mut self, req: Self::Request) -> Self::Future;
|
fn call(&mut self, req: Self::Request) -> Self::Future;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An extension trait for `Service`s that provides a variety of convenient
|
|
||||||
/// adapters
|
|
||||||
pub trait ServiceExt: Service {
|
|
||||||
/// Apply function to specified service and use it as a next service in
|
|
||||||
/// chain.
|
|
||||||
fn apply_fn<F, B, B1, Out>(self, service: B1, f: F) -> AndThenApply<Self, B, F, Out>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
F: FnMut(Self::Response, &mut B) -> Out,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: Into<Self::Error>,
|
|
||||||
B: Service<Error = Self::Error>,
|
|
||||||
B1: IntoService<B>,
|
|
||||||
{
|
|
||||||
AndThenApply::new(self, service, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Call another service after call to this one has resolved successfully.
|
|
||||||
///
|
|
||||||
/// This function can be used to chain two services together and ensure that
|
|
||||||
/// the second service isn't called until call to the fist service have
|
|
||||||
/// finished. Result of the call to the first service is used as an
|
|
||||||
/// input parameter for the second service's call.
|
|
||||||
///
|
|
||||||
/// Note that this function consumes the receiving service and returns a
|
|
||||||
/// wrapped version of it.
|
|
||||||
fn and_then<F, B>(self, service: F) -> AndThen<Self, B>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
F: IntoService<B>,
|
|
||||||
B: Service<Request = Self::Response, Error = Self::Error>,
|
|
||||||
{
|
|
||||||
AndThen::new(self, service.into_service())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Map this service's error to any error implementing `From` for
|
|
||||||
/// this service`s `Error`.
|
|
||||||
///
|
|
||||||
/// Note that this function consumes the receiving service and returns a
|
|
||||||
/// wrapped version of it.
|
|
||||||
fn from_err<E>(self) -> FromErr<Self, E>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
E: From<Self::Error>,
|
|
||||||
{
|
|
||||||
FromErr::new(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Chain on a computation for when a call to the service finished,
|
|
||||||
/// passing the result of the call to the next service `B`.
|
|
||||||
///
|
|
||||||
/// Note that this function consumes the receiving service and returns a
|
|
||||||
/// wrapped version of it.
|
|
||||||
fn then<B>(self, service: B) -> Then<Self, B>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
B: Service<Request = Result<Self::Response, Self::Error>, Error = Self::Error>,
|
|
||||||
{
|
|
||||||
Then::new(self, service)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Map this service's output to a different type, returning a new service
|
|
||||||
/// of the resulting type.
|
|
||||||
///
|
|
||||||
/// This function is similar to the `Option::map` or `Iterator::map` where
|
|
||||||
/// it will change the type of the underlying service.
|
|
||||||
///
|
|
||||||
/// Note that this function consumes the receiving service and returns a
|
|
||||||
/// wrapped version of it, similar to the existing `map` methods in the
|
|
||||||
/// standard library.
|
|
||||||
fn map<F, R>(self, f: F) -> Map<Self, F, R>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
F: FnMut(Self::Response) -> R,
|
|
||||||
{
|
|
||||||
Map::new(self, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Map this service's error to a different error, returning a new service.
|
|
||||||
///
|
|
||||||
/// This function is similar to the `Result::map_err` where it will change
|
|
||||||
/// the error type of the underlying service. This is useful for example to
|
|
||||||
/// ensure that services have the same error type.
|
|
||||||
///
|
|
||||||
/// Note that this function consumes the receiving service and returns a
|
|
||||||
/// wrapped version of it.
|
|
||||||
fn map_err<F, E>(self, f: F) -> MapErr<Self, F, E>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
F: Fn(Self::Error) -> E,
|
|
||||||
{
|
|
||||||
MapErr::new(self, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: ?Sized> ServiceExt for T where T: Service {}
|
|
||||||
|
|
||||||
/// Creates new `Service` values.
|
/// Creates new `Service` values.
|
||||||
///
|
///
|
||||||
/// Acts as a service factory. This is useful for cases where new `Service`
|
/// Acts as a service factory. This is useful for cases where new `Service`
|
||||||
/// values must be produced. One case is a TCP server listener. The listener
|
/// values must be produced. One case is a TCP server listener. The listener
|
||||||
/// accepts new TCP streams, obtains a new `Service` value using the
|
/// accepts new TCP streams, obtains a new `Service` value using the
|
||||||
/// `NewService` trait, and uses that new `Service` value to process inbound
|
/// `ServiceFactory` trait, and uses that new `Service` value to process inbound
|
||||||
/// requests on that new TCP stream.
|
/// requests on that new TCP stream.
|
||||||
///
|
///
|
||||||
/// `Config` is a service factory configuration type.
|
/// `Config` is a service factory configuration type.
|
||||||
pub trait NewService {
|
pub trait ServiceFactory {
|
||||||
/// Requests handled by the service.
|
/// Requests handled by the service.
|
||||||
type Request;
|
type Request;
|
||||||
|
|
||||||
@ -206,132 +98,10 @@ pub trait NewService {
|
|||||||
type InitError;
|
type InitError;
|
||||||
|
|
||||||
/// The future of the `Service` instance.
|
/// The future of the `Service` instance.
|
||||||
type Future: Future<Item = Self::Service, Error = Self::InitError>;
|
type Future: Future<Output = Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
/// Create and return a new service value asynchronously.
|
/// Create and return a new service value asynchronously.
|
||||||
fn new_service(&self, cfg: &Self::Config) -> Self::Future;
|
fn new_service(&self, cfg: &Self::Config) -> Self::Future;
|
||||||
|
|
||||||
/// Apply transform service to specified service and use it as a next service in
|
|
||||||
/// chain.
|
|
||||||
fn apply<T, T1, B, B1>(self, transform: T1, service: B1) -> AndThenTransform<T, Self, B>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
T: Transform<B::Service, Request = Self::Response, InitError = Self::InitError>,
|
|
||||||
T::Error: From<Self::Error>,
|
|
||||||
T1: IntoTransform<T, B::Service>,
|
|
||||||
B: NewService<Config = Self::Config, InitError = Self::InitError>,
|
|
||||||
B1: IntoNewService<B>,
|
|
||||||
{
|
|
||||||
AndThenTransform::new(transform.into_transform(), self, service.into_new_service())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Apply function to specified service and use it as a next service in
|
|
||||||
/// chain.
|
|
||||||
fn apply_fn<B, I, F, Out>(self, service: I, f: F) -> AndThenApplyNewService<Self, B, F, Out>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
B: NewService<Config = Self::Config, Error = Self::Error, InitError = Self::InitError>,
|
|
||||||
I: IntoNewService<B>,
|
|
||||||
F: FnMut(Self::Response, &mut B::Service) -> Out,
|
|
||||||
Out: IntoFuture,
|
|
||||||
Out::Error: Into<Self::Error>,
|
|
||||||
{
|
|
||||||
AndThenApplyNewService::new(self, service, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Call another service after call to this one has resolved successfully.
|
|
||||||
fn and_then<F, B>(self, new_service: F) -> AndThenNewService<Self, B>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
F: IntoNewService<B>,
|
|
||||||
B: NewService<
|
|
||||||
Config = Self::Config,
|
|
||||||
Request = Self::Response,
|
|
||||||
Error = Self::Error,
|
|
||||||
InitError = Self::InitError,
|
|
||||||
>,
|
|
||||||
{
|
|
||||||
AndThenNewService::new(self, new_service)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `NewService` that create service to map this service's error
|
|
||||||
/// and new service's init error to any error
|
|
||||||
/// implementing `From` for this service`s `Error`.
|
|
||||||
///
|
|
||||||
/// Note that this function consumes the receiving new service and returns a
|
|
||||||
/// wrapped version of it.
|
|
||||||
fn from_err<E>(self) -> FromErrNewService<Self, E>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
E: From<Self::Error>,
|
|
||||||
{
|
|
||||||
FromErrNewService::new(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create `NewService` to chain on a computation for when a call to the
|
|
||||||
/// service finished, passing the result of the call to the next
|
|
||||||
/// service `B`.
|
|
||||||
///
|
|
||||||
/// Note that this function consumes the receiving future and returns a
|
|
||||||
/// wrapped version of it.
|
|
||||||
fn then<F, B>(self, new_service: F) -> ThenNewService<Self, B>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
F: IntoNewService<B>,
|
|
||||||
B: NewService<
|
|
||||||
Config = Self::Config,
|
|
||||||
Request = Result<Self::Response, Self::Error>,
|
|
||||||
Error = Self::Error,
|
|
||||||
InitError = Self::InitError,
|
|
||||||
>,
|
|
||||||
{
|
|
||||||
ThenNewService::new(self, new_service)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Map this service's output to a different type, returning a new service
|
|
||||||
/// of the resulting type.
|
|
||||||
fn map<F, R>(self, f: F) -> MapNewService<Self, F, R>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
F: FnMut(Self::Response) -> R,
|
|
||||||
{
|
|
||||||
MapNewService::new(self, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Map this service's error to a different error, returning a new service.
|
|
||||||
fn map_err<F, E>(self, f: F) -> MapErrNewService<Self, F, E>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
F: Fn(Self::Error) -> E + Clone,
|
|
||||||
{
|
|
||||||
MapErrNewService::new(self, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Map this factory's init error to a different error, returning a new service.
|
|
||||||
fn map_init_err<F, E>(self, f: F) -> MapInitErr<Self, F, E>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
F: Fn(Self::InitError) -> E,
|
|
||||||
{
|
|
||||||
MapInitErr::new(self, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Map config to a different error, returning a new service.
|
|
||||||
fn map_config<F, C>(self, f: F) -> MapConfig<Self, F, C>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
F: Fn(&C) -> MappedConfig<Self::Config>,
|
|
||||||
{
|
|
||||||
MapConfig::new(self, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Replace config with unit
|
|
||||||
fn unit_config<C>(self) -> UnitConfig<Self, C>
|
|
||||||
where
|
|
||||||
Self: NewService<Config = ()> + Sized,
|
|
||||||
{
|
|
||||||
UnitConfig::new(self)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, S> Service for &'a mut S
|
impl<'a, S> Service for &'a mut S
|
||||||
@ -343,8 +113,8 @@ where
|
|||||||
type Error = S::Error;
|
type Error = S::Error;
|
||||||
type Future = S::Future;
|
type Future = S::Future;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), S::Error> {
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
(**self).poll_ready()
|
(**self).poll_ready(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, request: Self::Request) -> S::Future {
|
fn call(&mut self, request: Self::Request) -> S::Future {
|
||||||
@ -361,8 +131,8 @@ where
|
|||||||
type Error = S::Error;
|
type Error = S::Error;
|
||||||
type Future = S::Future;
|
type Future = S::Future;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), S::Error> {
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), S::Error>> {
|
||||||
(**self).poll_ready()
|
(**self).poll_ready(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, request: Self::Request) -> S::Future {
|
fn call(&mut self, request: Self::Request) -> S::Future {
|
||||||
@ -379,18 +149,18 @@ where
|
|||||||
type Error = S::Error;
|
type Error = S::Error;
|
||||||
type Future = S::Future;
|
type Future = S::Future;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), S::Error> {
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.borrow_mut().poll_ready()
|
self.borrow_mut().poll_ready(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, request: Self::Request) -> S::Future {
|
fn call(&mut self, request: Self::Request) -> S::Future {
|
||||||
self.borrow_mut().call(request)
|
(&mut (**self).borrow_mut()).call(request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> NewService for Rc<S>
|
impl<S> ServiceFactory for Rc<S>
|
||||||
where
|
where
|
||||||
S: NewService,
|
S: ServiceFactory,
|
||||||
{
|
{
|
||||||
type Request = S::Request;
|
type Request = S::Request;
|
||||||
type Response = S::Response;
|
type Response = S::Response;
|
||||||
@ -405,9 +175,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> NewService for Arc<S>
|
impl<S> ServiceFactory for Arc<S>
|
||||||
where
|
where
|
||||||
S: NewService,
|
S: ServiceFactory,
|
||||||
{
|
{
|
||||||
type Request = S::Request;
|
type Request = S::Request;
|
||||||
type Response = S::Response;
|
type Response = S::Response;
|
||||||
@ -431,13 +201,13 @@ where
|
|||||||
fn into_service(self) -> T;
|
fn into_service(self) -> T;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait for types that can be converted to a `NewService`
|
/// Trait for types that can be converted to a `ServiceFactory`
|
||||||
pub trait IntoNewService<T>
|
pub trait IntoServiceFactory<T>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: ServiceFactory,
|
||||||
{
|
{
|
||||||
/// Convert to an `NewService`
|
/// Convert `Self` an `ServiceFactory`
|
||||||
fn into_new_service(self) -> T;
|
fn into_factory(self) -> T;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> IntoService<T> for T
|
impl<T> IntoService<T> for T
|
||||||
@ -449,11 +219,11 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> IntoNewService<T> for T
|
impl<T> IntoServiceFactory<T> for T
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: ServiceFactory,
|
||||||
{
|
{
|
||||||
fn into_new_service(self) -> T {
|
fn into_factory(self) -> T {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use futures::{Async, Future, Poll};
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use super::{NewService, Service};
|
use super::{Service, ServiceFactory};
|
||||||
|
|
||||||
/// Service for the `map` combinator, changing the type of a service's response.
|
/// Service for the `map` combinator, changing the type of a service's response.
|
||||||
///
|
///
|
||||||
/// This is created by the `ServiceExt::map` method.
|
/// This is created by the `ServiceExt::map` method.
|
||||||
pub struct Map<A, F, Response> {
|
pub(crate) struct Map<A, F, Response> {
|
||||||
service: A,
|
service: A,
|
||||||
f: F,
|
f: F,
|
||||||
_t: PhantomData<Response>,
|
_t: PhantomData<Response>,
|
||||||
@ -52,8 +55,8 @@ where
|
|||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
type Future = MapFuture<A, F, Response>;
|
type Future = MapFuture<A, F, Response>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.service.poll_ready()
|
self.service.poll_ready(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||||
@ -61,12 +64,14 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MapFuture<A, F, Response>
|
#[pin_project]
|
||||||
|
pub(crate) struct MapFuture<A, F, Response>
|
||||||
where
|
where
|
||||||
A: Service,
|
A: Service,
|
||||||
F: FnMut(A::Response) -> Response,
|
F: FnMut(A::Response) -> Response,
|
||||||
{
|
{
|
||||||
f: F,
|
f: F,
|
||||||
|
#[pin]
|
||||||
fut: A::Future,
|
fut: A::Future,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,19 +90,20 @@ where
|
|||||||
A: Service,
|
A: Service,
|
||||||
F: FnMut(A::Response) -> Response,
|
F: FnMut(A::Response) -> Response,
|
||||||
{
|
{
|
||||||
type Item = Response;
|
type Output = Result<Response, A::Error>;
|
||||||
type Error = A::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
match self.fut.poll()? {
|
let this = self.project();
|
||||||
Async::Ready(resp) => Ok(Async::Ready((self.f)(resp))),
|
match this.fut.poll(cx) {
|
||||||
Async::NotReady => Ok(Async::NotReady),
|
Poll::Ready(Ok(resp)) => Poll::Ready(Ok((this.f)(resp))),
|
||||||
|
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
|
||||||
|
Poll::Pending => Poll::Pending,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `MapNewService` new service combinator
|
/// `MapNewService` new service combinator
|
||||||
pub struct MapNewService<A, F, Res> {
|
pub(crate) struct MapNewService<A, F, Res> {
|
||||||
a: A,
|
a: A,
|
||||||
f: F,
|
f: F,
|
||||||
r: PhantomData<Res>,
|
r: PhantomData<Res>,
|
||||||
@ -107,7 +113,7 @@ impl<A, F, Res> MapNewService<A, F, Res> {
|
|||||||
/// Create new `Map` new service instance
|
/// Create new `Map` new service instance
|
||||||
pub fn new(a: A, f: F) -> Self
|
pub fn new(a: A, f: F) -> Self
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: FnMut(A::Response) -> Res,
|
F: FnMut(A::Response) -> Res,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
@ -132,9 +138,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, Res> NewService for MapNewService<A, F, Res>
|
impl<A, F, Res> ServiceFactory for MapNewService<A, F, Res>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: FnMut(A::Response) -> Res + Clone,
|
F: FnMut(A::Response) -> Res + Clone,
|
||||||
{
|
{
|
||||||
type Request = A::Request;
|
type Request = A::Request;
|
||||||
@ -151,18 +157,20 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MapNewServiceFuture<A, F, Res>
|
#[pin_project]
|
||||||
|
pub(crate) struct MapNewServiceFuture<A, F, Res>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: FnMut(A::Response) -> Res,
|
F: FnMut(A::Response) -> Res,
|
||||||
{
|
{
|
||||||
|
#[pin]
|
||||||
fut: A::Future,
|
fut: A::Future,
|
||||||
f: Option<F>,
|
f: Option<F>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, Res> MapNewServiceFuture<A, F, Res>
|
impl<A, F, Res> MapNewServiceFuture<A, F, Res>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: FnMut(A::Response) -> Res,
|
F: FnMut(A::Response) -> Res,
|
||||||
{
|
{
|
||||||
fn new(fut: A::Future, f: F) -> Self {
|
fn new(fut: A::Future, f: F) -> Self {
|
||||||
@ -172,37 +180,38 @@ where
|
|||||||
|
|
||||||
impl<A, F, Res> Future for MapNewServiceFuture<A, F, Res>
|
impl<A, F, Res> Future for MapNewServiceFuture<A, F, Res>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: FnMut(A::Response) -> Res,
|
F: FnMut(A::Response) -> Res,
|
||||||
{
|
{
|
||||||
type Item = Map<A::Service, F, Res>;
|
type Output = Result<Map<A::Service, F, Res>, A::InitError>;
|
||||||
type Error = A::InitError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
if let Async::Ready(service) = self.fut.poll()? {
|
let this = self.project();
|
||||||
Ok(Async::Ready(Map::new(service, self.f.take().unwrap())))
|
if let Poll::Ready(svc) = this.fut.poll(cx)? {
|
||||||
|
Poll::Ready(Ok(Map::new(svc, this.f.take().unwrap())))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use futures::future::{ok, FutureResult};
|
use futures::future::{lazy, ok, Ready};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::{IntoNewService, Service, ServiceExt};
|
use crate::{into_factory, into_service, Service};
|
||||||
|
|
||||||
struct Srv;
|
struct Srv;
|
||||||
|
|
||||||
impl Service for Srv {
|
impl Service for Srv {
|
||||||
type Request = ();
|
type Request = ();
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<(), ()>;
|
type Future = Ready<Result<(), ()>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, _: ()) -> Self::Future {
|
fn call(&mut self, _: ()) -> Self::Future {
|
||||||
@ -210,32 +219,27 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_poll_ready() {
|
async fn test_poll_ready() {
|
||||||
let mut srv = Srv.map(|_| "ok");
|
let mut srv = into_service(Srv).map(|_| "ok");
|
||||||
let res = srv.poll_ready();
|
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
||||||
assert!(res.is_ok());
|
assert_eq!(res, Poll::Ready(Ok(())));
|
||||||
assert_eq!(res.unwrap(), Async::Ready(()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_call() {
|
async fn test_call() {
|
||||||
let mut srv = Srv.map(|_| "ok");
|
let mut srv = into_service(Srv).map(|_| "ok");
|
||||||
let res = srv.call(()).poll();
|
let res = srv.call(()).await;
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), Async::Ready("ok"));
|
assert_eq!(res.unwrap(), "ok");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_new_service() {
|
async fn test_new_service() {
|
||||||
let blank = || Ok::<_, ()>(Srv);
|
let new_srv = into_factory(|| ok::<_, ()>(Srv)).map(|_| "ok");
|
||||||
let new_srv = blank.into_new_service().map(|_| "ok");
|
let mut srv = new_srv.new_service(&()).await.unwrap();
|
||||||
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
|
let res = srv.call(()).await;
|
||||||
let res = srv.call(()).poll();
|
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), Async::Ready("ok"));
|
assert_eq!(res.unwrap(), ("ok"));
|
||||||
} else {
|
|
||||||
panic!()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,48 @@
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use super::NewService;
|
use super::ServiceFactory;
|
||||||
|
|
||||||
pub enum MappedConfig<'a, T> {
|
pub enum MappedConfig<'a, T> {
|
||||||
Ref(&'a T),
|
Ref(&'a T),
|
||||||
Owned(T),
|
Owned(T),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adapt external config to a config for provided new service
|
||||||
|
pub fn map_config<T, F, C>(
|
||||||
|
factory: T,
|
||||||
|
f: F,
|
||||||
|
) -> impl ServiceFactory<
|
||||||
|
Config = C,
|
||||||
|
Request = T::Request,
|
||||||
|
Response = T::Response,
|
||||||
|
Error = T::Error,
|
||||||
|
InitError = T::InitError,
|
||||||
|
>
|
||||||
|
where
|
||||||
|
T: ServiceFactory,
|
||||||
|
F: Fn(&C) -> MappedConfig<T::Config>,
|
||||||
|
{
|
||||||
|
MapConfig::new(factory, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Replace config with unit
|
||||||
|
pub fn unit_config<T, C>(
|
||||||
|
new_service: T,
|
||||||
|
) -> impl ServiceFactory<
|
||||||
|
Config = C,
|
||||||
|
Request = T::Request,
|
||||||
|
Response = T::Response,
|
||||||
|
Error = T::Error,
|
||||||
|
InitError = T::InitError,
|
||||||
|
>
|
||||||
|
where
|
||||||
|
T: ServiceFactory<Config = ()>,
|
||||||
|
{
|
||||||
|
UnitConfig::new(new_service)
|
||||||
|
}
|
||||||
|
|
||||||
/// `MapInitErr` service combinator
|
/// `MapInitErr` service combinator
|
||||||
pub struct MapConfig<A, F, C> {
|
pub(crate) struct MapConfig<A, F, C> {
|
||||||
a: A,
|
a: A,
|
||||||
f: F,
|
f: F,
|
||||||
e: PhantomData<C>,
|
e: PhantomData<C>,
|
||||||
@ -18,7 +52,7 @@ impl<A, F, C> MapConfig<A, F, C> {
|
|||||||
/// Create new `MapConfig` combinator
|
/// Create new `MapConfig` combinator
|
||||||
pub fn new(a: A, f: F) -> Self
|
pub fn new(a: A, f: F) -> Self
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: Fn(&C) -> MappedConfig<A::Config>,
|
F: Fn(&C) -> MappedConfig<A::Config>,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
@ -43,9 +77,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, C> NewService for MapConfig<A, F, C>
|
impl<A, F, C> ServiceFactory for MapConfig<A, F, C>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: Fn(&C) -> MappedConfig<A::Config>,
|
F: Fn(&C) -> MappedConfig<A::Config>,
|
||||||
{
|
{
|
||||||
type Request = A::Request;
|
type Request = A::Request;
|
||||||
@ -66,17 +100,17 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// `MapInitErr` service combinator
|
/// `MapInitErr` service combinator
|
||||||
pub struct UnitConfig<A, C> {
|
pub(crate) struct UnitConfig<A, C> {
|
||||||
a: A,
|
a: A,
|
||||||
e: PhantomData<C>,
|
e: PhantomData<C>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, C> UnitConfig<A, C> {
|
impl<A, C> UnitConfig<A, C>
|
||||||
|
where
|
||||||
|
A: ServiceFactory<Config = ()>,
|
||||||
|
{
|
||||||
/// Create new `UnitConfig` combinator
|
/// Create new `UnitConfig` combinator
|
||||||
pub fn new(a: A) -> Self
|
pub(crate) fn new(a: A) -> Self {
|
||||||
where
|
|
||||||
A: NewService<Config = ()>,
|
|
||||||
{
|
|
||||||
Self { a, e: PhantomData }
|
Self { a, e: PhantomData }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -93,9 +127,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, C> NewService for UnitConfig<A, C>
|
impl<A, C> ServiceFactory for UnitConfig<A, C>
|
||||||
where
|
where
|
||||||
A: NewService<Config = ()>,
|
A: ServiceFactory<Config = ()>,
|
||||||
{
|
{
|
||||||
type Request = A::Request;
|
type Request = A::Request;
|
||||||
type Response = A::Response;
|
type Response = A::Response;
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use futures::{Async, Future, Poll};
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use super::{NewService, Service};
|
use super::{Service, ServiceFactory};
|
||||||
|
|
||||||
/// Service for the `map_err` combinator, changing the type of a service's
|
/// Service for the `map_err` combinator, changing the type of a service's
|
||||||
/// error.
|
/// error.
|
||||||
///
|
///
|
||||||
/// This is created by the `ServiceExt::map_err` method.
|
/// This is created by the `ServiceExt::map_err` method.
|
||||||
pub struct MapErr<A, F, E> {
|
pub(crate) struct MapErr<A, F, E> {
|
||||||
service: A,
|
service: A,
|
||||||
f: F,
|
f: F,
|
||||||
_t: PhantomData<E>,
|
_t: PhantomData<E>,
|
||||||
@ -53,8 +56,8 @@ where
|
|||||||
type Error = E;
|
type Error = E;
|
||||||
type Future = MapErrFuture<A, F, E>;
|
type Future = MapErrFuture<A, F, E>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.service.poll_ready().map_err(&self.f)
|
self.service.poll_ready(ctx).map_err(&self.f)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||||
@ -62,12 +65,14 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MapErrFuture<A, F, E>
|
#[pin_project]
|
||||||
|
pub(crate) struct MapErrFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: Service,
|
A: Service,
|
||||||
F: Fn(A::Error) -> E,
|
F: Fn(A::Error) -> E,
|
||||||
{
|
{
|
||||||
f: F,
|
f: F,
|
||||||
|
#[pin]
|
||||||
fut: A::Future,
|
fut: A::Future,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,21 +91,21 @@ where
|
|||||||
A: Service,
|
A: Service,
|
||||||
F: Fn(A::Error) -> E,
|
F: Fn(A::Error) -> E,
|
||||||
{
|
{
|
||||||
type Item = A::Response;
|
type Output = Result<A::Response, E>;
|
||||||
type Error = E;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
self.fut.poll().map_err(&self.f)
|
let this = self.project();
|
||||||
|
this.fut.poll(cx).map_err(this.f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// NewService for the `map_err` combinator, changing the type of a new
|
/// Factory for the `map_err` combinator, changing the type of a new
|
||||||
/// service's error.
|
/// service's error.
|
||||||
///
|
///
|
||||||
/// This is created by the `NewServiceExt::map_err` method.
|
/// This is created by the `NewServiceExt::map_err` method.
|
||||||
pub struct MapErrNewService<A, F, E>
|
pub(crate) struct MapErrNewService<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: Fn(A::Error) -> E + Clone,
|
F: Fn(A::Error) -> E + Clone,
|
||||||
{
|
{
|
||||||
a: A,
|
a: A,
|
||||||
@ -110,11 +115,11 @@ where
|
|||||||
|
|
||||||
impl<A, F, E> MapErrNewService<A, F, E>
|
impl<A, F, E> MapErrNewService<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: Fn(A::Error) -> E + Clone,
|
F: Fn(A::Error) -> E + Clone,
|
||||||
{
|
{
|
||||||
/// Create new `MapErr` new service instance
|
/// Create new `MapErr` new service instance
|
||||||
pub fn new(a: A, f: F) -> Self {
|
pub(crate) fn new(a: A, f: F) -> Self {
|
||||||
Self {
|
Self {
|
||||||
a,
|
a,
|
||||||
f,
|
f,
|
||||||
@ -125,7 +130,7 @@ where
|
|||||||
|
|
||||||
impl<A, F, E> Clone for MapErrNewService<A, F, E>
|
impl<A, F, E> Clone for MapErrNewService<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService + Clone,
|
A: ServiceFactory + Clone,
|
||||||
F: Fn(A::Error) -> E + Clone,
|
F: Fn(A::Error) -> E + Clone,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
@ -137,9 +142,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E> NewService for MapErrNewService<A, F, E>
|
impl<A, F, E> ServiceFactory for MapErrNewService<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: Fn(A::Error) -> E + Clone,
|
F: Fn(A::Error) -> E + Clone,
|
||||||
{
|
{
|
||||||
type Request = A::Request;
|
type Request = A::Request;
|
||||||
@ -156,18 +161,20 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MapErrNewServiceFuture<A, F, E>
|
#[pin_project]
|
||||||
|
pub(crate) struct MapErrNewServiceFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: Fn(A::Error) -> E,
|
F: Fn(A::Error) -> E,
|
||||||
{
|
{
|
||||||
|
#[pin]
|
||||||
fut: A::Future,
|
fut: A::Future,
|
||||||
f: F,
|
f: F,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E> MapErrNewServiceFuture<A, F, E>
|
impl<A, F, E> MapErrNewServiceFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: Fn(A::Error) -> E,
|
F: Fn(A::Error) -> E,
|
||||||
{
|
{
|
||||||
fn new(fut: A::Future, f: F) -> Self {
|
fn new(fut: A::Future, f: F) -> Self {
|
||||||
@ -177,27 +184,27 @@ where
|
|||||||
|
|
||||||
impl<A, F, E> Future for MapErrNewServiceFuture<A, F, E>
|
impl<A, F, E> Future for MapErrNewServiceFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: Fn(A::Error) -> E + Clone,
|
F: Fn(A::Error) -> E + Clone,
|
||||||
{
|
{
|
||||||
type Item = MapErr<A::Service, F, E>;
|
type Output = Result<MapErr<A::Service, F, E>, A::InitError>;
|
||||||
type Error = A::InitError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
if let Async::Ready(service) = self.fut.poll()? {
|
let this = self.project();
|
||||||
Ok(Async::Ready(MapErr::new(service, self.f.clone())))
|
if let Poll::Ready(svc) = this.fut.poll(cx)? {
|
||||||
|
Poll::Ready(Ok(MapErr::new(svc, this.f.clone())))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use futures::future::{err, FutureResult};
|
use futures::future::{err, lazy, ok, Ready};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::{IntoNewService, NewService, Service, ServiceExt};
|
use crate::{into_factory, into_service, Service};
|
||||||
|
|
||||||
struct Srv;
|
struct Srv;
|
||||||
|
|
||||||
@ -205,10 +212,10 @@ mod tests {
|
|||||||
type Request = ();
|
type Request = ();
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<(), ()>;
|
type Future = Ready<Result<(), ()>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
Err(())
|
Poll::Ready(Err(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, _: ()) -> Self::Future {
|
fn call(&mut self, _: ()) -> Self::Future {
|
||||||
@ -216,32 +223,27 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_poll_ready() {
|
async fn test_poll_ready() {
|
||||||
let mut srv = Srv.map_err(|_| "error");
|
let mut srv = into_service(Srv).map_err(|_| "error");
|
||||||
let res = srv.poll_ready();
|
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
||||||
|
assert_eq!(res, Poll::Ready(Err("error")));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_call() {
|
||||||
|
let mut srv = into_service(Srv).map_err(|_| "error");
|
||||||
|
let res = srv.call(()).await;
|
||||||
assert!(res.is_err());
|
assert!(res.is_err());
|
||||||
assert_eq!(res.err().unwrap(), "error");
|
assert_eq!(res.err().unwrap(), "error");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_call() {
|
async fn test_new_service() {
|
||||||
let mut srv = Srv.map_err(|_| "error");
|
let new_srv = into_factory(|| ok::<_, ()>(Srv)).map_err(|_| "error");
|
||||||
let res = srv.call(()).poll();
|
let mut srv = new_srv.new_service(&()).await.unwrap();
|
||||||
|
let res = srv.call(()).await;
|
||||||
assert!(res.is_err());
|
assert!(res.is_err());
|
||||||
assert_eq!(res.err().unwrap(), "error");
|
assert_eq!(res.err().unwrap(), "error");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_new_service() {
|
|
||||||
let blank = || Ok::<_, ()>(Srv);
|
|
||||||
let new_srv = blank.into_new_service().map_err(|_| "error");
|
|
||||||
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
|
|
||||||
let res = srv.call(()).poll();
|
|
||||||
assert!(res.is_err());
|
|
||||||
assert_eq!(res.err().unwrap(), "error");
|
|
||||||
} else {
|
|
||||||
panic!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,26 @@
|
|||||||
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use futures::{Future, Poll};
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use super::NewService;
|
use super::ServiceFactory;
|
||||||
|
|
||||||
/// `MapInitErr` service combinator
|
/// `MapInitErr` service combinator
|
||||||
pub struct MapInitErr<A, F, E> {
|
pub(crate) struct MapInitErr<A, F, E> {
|
||||||
a: A,
|
a: A,
|
||||||
f: F,
|
f: F,
|
||||||
e: PhantomData<E>,
|
e: PhantomData<E>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E> MapInitErr<A, F, E> {
|
impl<A, F, E> MapInitErr<A, F, E>
|
||||||
/// Create new `MapInitErr` combinator
|
where
|
||||||
pub fn new(a: A, f: F) -> Self
|
A: ServiceFactory,
|
||||||
where
|
|
||||||
A: NewService,
|
|
||||||
F: Fn(A::InitError) -> E,
|
F: Fn(A::InitError) -> E,
|
||||||
{
|
{
|
||||||
|
/// Create new `MapInitErr` combinator
|
||||||
|
pub(crate) fn new(a: A, f: F) -> Self {
|
||||||
Self {
|
Self {
|
||||||
a,
|
a,
|
||||||
f,
|
f,
|
||||||
@ -40,9 +43,9 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E> NewService for MapInitErr<A, F, E>
|
impl<A, F, E> ServiceFactory for MapInitErr<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: Fn(A::InitError) -> E + Clone,
|
F: Fn(A::InitError) -> E + Clone,
|
||||||
{
|
{
|
||||||
type Request = A::Request;
|
type Request = A::Request;
|
||||||
@ -58,19 +61,20 @@ where
|
|||||||
MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone())
|
MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[pin_project]
|
||||||
pub struct MapInitErrFuture<A, F, E>
|
pub(crate) struct MapInitErrFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: Fn(A::InitError) -> E,
|
F: Fn(A::InitError) -> E,
|
||||||
{
|
{
|
||||||
f: F,
|
f: F,
|
||||||
|
#[pin]
|
||||||
fut: A::Future,
|
fut: A::Future,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, F, E> MapInitErrFuture<A, F, E>
|
impl<A, F, E> MapInitErrFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: Fn(A::InitError) -> E,
|
F: Fn(A::InitError) -> E,
|
||||||
{
|
{
|
||||||
fn new(fut: A::Future, f: F) -> Self {
|
fn new(fut: A::Future, f: F) -> Self {
|
||||||
@ -80,13 +84,13 @@ where
|
|||||||
|
|
||||||
impl<A, F, E> Future for MapInitErrFuture<A, F, E>
|
impl<A, F, E> Future for MapInitErrFuture<A, F, E>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
F: Fn(A::InitError) -> E,
|
F: Fn(A::InitError) -> E,
|
||||||
{
|
{
|
||||||
type Item = A::Service;
|
type Output = Result<A::Service, E>;
|
||||||
type Error = E;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
self.fut.poll().map_err(&self.f)
|
let this = self.project();
|
||||||
|
this.fut.poll(cx).map_err(this.f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
196
actix-service/src/pipeline.rs
Normal file
196
actix-service/src/pipeline.rs
Normal file
@ -0,0 +1,196 @@
|
|||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
use crate::and_then::{AndThen, AndThenNewService};
|
||||||
|
use crate::then::{Then, ThenNewService};
|
||||||
|
use crate::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
||||||
|
|
||||||
|
pub fn pipeline<F, T>(service: F) -> Pipeline<T>
|
||||||
|
where
|
||||||
|
F: IntoService<T>,
|
||||||
|
T: Service,
|
||||||
|
{
|
||||||
|
Pipeline {
|
||||||
|
service: service.into_service(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pipeline_factory<T, F>(factory: F) -> PipelineFactory<T>
|
||||||
|
where
|
||||||
|
T: ServiceFactory,
|
||||||
|
F: IntoServiceFactory<T>,
|
||||||
|
{
|
||||||
|
PipelineFactory {
|
||||||
|
factory: factory.into_factory(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pipeline service
|
||||||
|
pub struct Pipeline<T> {
|
||||||
|
service: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Service> Pipeline<T> {
|
||||||
|
/// Call another service after call to this one has resolved successfully.
|
||||||
|
///
|
||||||
|
/// This function can be used to chain two services together and ensure that
|
||||||
|
/// the second service isn't called until call to the fist service have
|
||||||
|
/// finished. Result of the call to the first service is used as an
|
||||||
|
/// input parameter for the second service's call.
|
||||||
|
///
|
||||||
|
/// Note that this function consumes the receiving service and returns a
|
||||||
|
/// wrapped version of it.
|
||||||
|
pub fn and_then<F, U>(
|
||||||
|
self,
|
||||||
|
service: F,
|
||||||
|
) -> Pipeline<impl Service<Request = T::Request, Response = U::Response, Error = T::Error>>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: IntoService<U>,
|
||||||
|
U: Service<Request = T::Response, Error = T::Error>,
|
||||||
|
{
|
||||||
|
Pipeline {
|
||||||
|
service: AndThen::new(self.service, service.into_service()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Chain on a computation for when a call to the service finished,
|
||||||
|
/// passing the result of the call to the next service `U`.
|
||||||
|
///
|
||||||
|
/// Note that this function consumes the receiving pipeline and returns a
|
||||||
|
/// wrapped version of it.
|
||||||
|
pub fn then<F, U>(
|
||||||
|
self,
|
||||||
|
service: F,
|
||||||
|
) -> Pipeline<impl Service<Request = T::Request, Response = U::Response, Error = T::Error>>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: IntoService<U>,
|
||||||
|
U: Service<Request = Result<T::Response, T::Error>, Error = T::Error>,
|
||||||
|
{
|
||||||
|
Pipeline {
|
||||||
|
service: Then::new(self.service, service.into_service()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Clone for Pipeline<T>
|
||||||
|
where
|
||||||
|
T: Clone,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Pipeline {
|
||||||
|
service: self.service.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Service> Service for Pipeline<T> {
|
||||||
|
type Request = T::Request;
|
||||||
|
type Response = T::Response;
|
||||||
|
type Error = T::Error;
|
||||||
|
type Future = T::Future;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), T::Error>> {
|
||||||
|
self.service.poll_ready(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn call(&mut self, req: T::Request) -> Self::Future {
|
||||||
|
self.service.call(req)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pipeline constructor
|
||||||
|
pub struct PipelineFactory<T> {
|
||||||
|
factory: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ServiceFactory> PipelineFactory<T> {
|
||||||
|
/// Call another service after call to this one has resolved successfully.
|
||||||
|
pub fn and_then<F, U>(
|
||||||
|
self,
|
||||||
|
factory: F,
|
||||||
|
) -> PipelineFactory<
|
||||||
|
impl ServiceFactory<
|
||||||
|
Config = T::Config,
|
||||||
|
Request = T::Request,
|
||||||
|
Response = U::Response,
|
||||||
|
Error = T::Error,
|
||||||
|
InitError = T::InitError,
|
||||||
|
>,
|
||||||
|
>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: IntoServiceFactory<U>,
|
||||||
|
U: ServiceFactory<
|
||||||
|
Config = T::Config,
|
||||||
|
Request = T::Response,
|
||||||
|
Error = T::Error,
|
||||||
|
InitError = T::InitError,
|
||||||
|
>,
|
||||||
|
{
|
||||||
|
PipelineFactory {
|
||||||
|
factory: AndThenNewService::new(self.factory, factory.into_factory()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create `NewService` to chain on a computation for when a call to the
|
||||||
|
/// service finished, passing the result of the call to the next
|
||||||
|
/// service `U`.
|
||||||
|
///
|
||||||
|
/// Note that this function consumes the receiving pipeline and returns a
|
||||||
|
/// wrapped version of it.
|
||||||
|
pub fn then<F, U>(
|
||||||
|
self,
|
||||||
|
factory: F,
|
||||||
|
) -> PipelineFactory<
|
||||||
|
impl ServiceFactory<
|
||||||
|
Config = T::Config,
|
||||||
|
Request = T::Request,
|
||||||
|
Response = U::Response,
|
||||||
|
Error = T::Error,
|
||||||
|
InitError = T::InitError,
|
||||||
|
>,
|
||||||
|
>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: IntoServiceFactory<U>,
|
||||||
|
U: ServiceFactory<
|
||||||
|
Config = T::Config,
|
||||||
|
Request = Result<T::Response, T::Error>,
|
||||||
|
Error = T::Error,
|
||||||
|
InitError = T::InitError,
|
||||||
|
>,
|
||||||
|
{
|
||||||
|
PipelineFactory {
|
||||||
|
factory: ThenNewService::new(self.factory, factory.into_factory()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Clone for PipelineFactory<T>
|
||||||
|
where
|
||||||
|
T: Clone,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
PipelineFactory {
|
||||||
|
factory: self.factory.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ServiceFactory> ServiceFactory for PipelineFactory<T> {
|
||||||
|
type Config = T::Config;
|
||||||
|
type Request = T::Request;
|
||||||
|
type Response = T::Response;
|
||||||
|
type Error = T::Error;
|
||||||
|
type Service = T::Service;
|
||||||
|
type InitError = T::InitError;
|
||||||
|
type Future = T::Future;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn new_service(&self, cfg: &T::Config) -> Self::Future {
|
||||||
|
self.factory.new_service(cfg)
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,17 @@
|
|||||||
use futures::{Async, Future, Poll};
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use super::{IntoNewService, NewService, Service};
|
use pin_project::pin_project;
|
||||||
|
|
||||||
|
use super::{Service, ServiceFactory};
|
||||||
use crate::cell::Cell;
|
use crate::cell::Cell;
|
||||||
|
|
||||||
/// Service for the `then` combinator, chaining a computation onto the end of
|
/// Service for the `then` combinator, chaining a computation onto the end of
|
||||||
/// another service.
|
/// another service.
|
||||||
///
|
///
|
||||||
/// This is created by the `ServiceExt::then` method.
|
/// This is created by the `ServiceExt::then` method.
|
||||||
pub struct Then<A, B> {
|
pub(crate) struct Then<A, B> {
|
||||||
a: A,
|
a: A,
|
||||||
b: Cell<B>,
|
b: Cell<B>,
|
||||||
}
|
}
|
||||||
@ -45,12 +49,12 @@ where
|
|||||||
type Error = B::Error;
|
type Error = B::Error;
|
||||||
type Future = ThenFuture<A, B>;
|
type Future = ThenFuture<A, B>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
let not_ready = self.a.poll_ready()?.is_not_ready();
|
let not_ready = !self.a.poll_ready(ctx)?.is_ready();
|
||||||
if self.b.get_mut().poll_ready()?.is_not_ready() || not_ready {
|
if !self.b.get_mut().poll_ready(ctx)?.is_ready() || not_ready {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,13 +63,16 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ThenFuture<A, B>
|
#[pin_project]
|
||||||
|
pub(crate) struct ThenFuture<A, B>
|
||||||
where
|
where
|
||||||
A: Service,
|
A: Service,
|
||||||
B: Service<Request = Result<A::Response, A::Error>>,
|
B: Service<Request = Result<A::Response, A::Error>>,
|
||||||
{
|
{
|
||||||
b: Cell<B>,
|
b: Cell<B>,
|
||||||
|
#[pin]
|
||||||
fut_b: Option<B::Future>,
|
fut_b: Option<B::Future>,
|
||||||
|
#[pin]
|
||||||
fut_a: Option<A::Future>,
|
fut_a: Option<A::Future>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,60 +95,63 @@ where
|
|||||||
A: Service,
|
A: Service,
|
||||||
B: Service<Request = Result<A::Response, A::Error>>,
|
B: Service<Request = Result<A::Response, A::Error>>,
|
||||||
{
|
{
|
||||||
type Item = B::Response;
|
type Output = Result<B::Response, B::Error>;
|
||||||
type Error = B::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
if let Some(ref mut fut) = self.fut_b {
|
let mut this = self.project();
|
||||||
return fut.poll();
|
|
||||||
|
loop {
|
||||||
|
let mut fut_a = this.fut_a.as_mut();
|
||||||
|
let mut fut_b = this.fut_b.as_mut();
|
||||||
|
|
||||||
|
if let Some(fut) = fut_b.as_mut().as_pin_mut() {
|
||||||
|
return fut.poll(cx);
|
||||||
}
|
}
|
||||||
|
|
||||||
match self.fut_a.as_mut().expect("bug in actix-service").poll() {
|
match fut_a
|
||||||
Ok(Async::Ready(resp)) => {
|
.as_mut()
|
||||||
let _ = self.fut_a.take();
|
.as_pin_mut()
|
||||||
self.fut_b = Some(self.b.get_mut().call(Ok(resp)));
|
.expect("Bug in actix-service")
|
||||||
self.poll()
|
.poll(cx)
|
||||||
|
{
|
||||||
|
Poll::Ready(r) => {
|
||||||
|
fut_a.set(None);
|
||||||
|
let new_fut = this.b.get_mut().call(r);
|
||||||
|
fut_b.set(Some(new_fut));
|
||||||
}
|
}
|
||||||
Err(err) => {
|
|
||||||
let _ = self.fut_a.take();
|
Poll::Pending => return Poll::Pending,
|
||||||
self.fut_b = Some(self.b.get_mut().call(Err(err)));
|
|
||||||
self.poll()
|
|
||||||
}
|
}
|
||||||
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `ThenNewService` new service combinator
|
/// `.then()` service factory combinator
|
||||||
pub struct ThenNewService<A, B> {
|
pub(crate) struct ThenNewService<A, B> {
|
||||||
a: A,
|
a: A,
|
||||||
b: B,
|
b: B,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B> ThenNewService<A, B> {
|
impl<A, B> ThenNewService<A, B>
|
||||||
/// Create new `AndThen` combinator
|
where
|
||||||
pub fn new<F>(a: A, f: F) -> Self
|
A: ServiceFactory,
|
||||||
where
|
B: ServiceFactory<
|
||||||
A: NewService,
|
|
||||||
B: NewService<
|
|
||||||
Config = A::Config,
|
Config = A::Config,
|
||||||
Request = Result<A::Response, A::Error>,
|
Request = Result<A::Response, A::Error>,
|
||||||
Error = A::Error,
|
Error = A::Error,
|
||||||
InitError = A::InitError,
|
InitError = A::InitError,
|
||||||
>,
|
>,
|
||||||
F: IntoNewService<B>,
|
{
|
||||||
{
|
/// Create new `AndThen` combinator
|
||||||
Self {
|
pub fn new(a: A, b: B) -> Self {
|
||||||
a,
|
Self { a, b }
|
||||||
b: f.into_new_service(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B> NewService for ThenNewService<A, B>
|
impl<A, B> ServiceFactory for ThenNewService<A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
B: NewService<
|
B: ServiceFactory<
|
||||||
Config = A::Config,
|
Config = A::Config,
|
||||||
Request = Result<A::Response, A::Error>,
|
Request = Result<A::Response, A::Error>,
|
||||||
Error = A::Error,
|
Error = A::Error,
|
||||||
@ -175,17 +185,20 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ThenNewServiceFuture<A, B>
|
#[pin_project]
|
||||||
|
pub(crate) struct ThenNewServiceFuture<A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
B: NewService<
|
B: ServiceFactory<
|
||||||
Config = A::Config,
|
Config = A::Config,
|
||||||
Request = Result<A::Response, A::Error>,
|
Request = Result<A::Response, A::Error>,
|
||||||
Error = A::Error,
|
Error = A::Error,
|
||||||
InitError = A::InitError,
|
InitError = A::InitError,
|
||||||
>,
|
>,
|
||||||
{
|
{
|
||||||
|
#[pin]
|
||||||
fut_b: B::Future,
|
fut_b: B::Future,
|
||||||
|
#[pin]
|
||||||
fut_a: A::Future,
|
fut_a: A::Future,
|
||||||
a: Option<A::Service>,
|
a: Option<A::Service>,
|
||||||
b: Option<B::Service>,
|
b: Option<B::Service>,
|
||||||
@ -193,8 +206,8 @@ where
|
|||||||
|
|
||||||
impl<A, B> ThenNewServiceFuture<A, B>
|
impl<A, B> ThenNewServiceFuture<A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
B: NewService<
|
B: ServiceFactory<
|
||||||
Config = A::Config,
|
Config = A::Config,
|
||||||
Request = Result<A::Response, A::Error>,
|
Request = Result<A::Response, A::Error>,
|
||||||
Error = A::Error,
|
Error = A::Error,
|
||||||
@ -213,61 +226,61 @@ where
|
|||||||
|
|
||||||
impl<A, B> Future for ThenNewServiceFuture<A, B>
|
impl<A, B> Future for ThenNewServiceFuture<A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
B: NewService<
|
B: ServiceFactory<
|
||||||
Config = A::Config,
|
Config = A::Config,
|
||||||
Request = Result<A::Response, A::Error>,
|
Request = Result<A::Response, A::Error>,
|
||||||
Error = A::Error,
|
Error = A::Error,
|
||||||
InitError = A::InitError,
|
InitError = A::InitError,
|
||||||
>,
|
>,
|
||||||
{
|
{
|
||||||
type Item = Then<A::Service, B::Service>;
|
type Output = Result<Then<A::Service, B::Service>, A::InitError>;
|
||||||
type Error = A::InitError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
if self.a.is_none() {
|
let this = self.project();
|
||||||
if let Async::Ready(service) = self.fut_a.poll()? {
|
if this.a.is_none() {
|
||||||
self.a = Some(service);
|
if let Poll::Ready(service) = this.fut_a.poll(cx)? {
|
||||||
|
*this.a = Some(service);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if this.b.is_none() {
|
||||||
if self.b.is_none() {
|
if let Poll::Ready(service) = this.fut_b.poll(cx)? {
|
||||||
if let Async::Ready(service) = self.fut_b.poll()? {
|
*this.b = Some(service);
|
||||||
self.b = Some(service);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if this.a.is_some() && this.b.is_some() {
|
||||||
if self.a.is_some() && self.b.is_some() {
|
Poll::Ready(Ok(Then::new(
|
||||||
Ok(Async::Ready(Then::new(
|
this.a.take().unwrap(),
|
||||||
self.a.take().unwrap(),
|
this.b.take().unwrap(),
|
||||||
self.b.take().unwrap(),
|
|
||||||
)))
|
)))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use futures::future::{err, ok, FutureResult};
|
|
||||||
use futures::{Async, Future, Poll};
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use crate::{IntoNewService, NewService, Service, ServiceExt};
|
use futures::future::{err, lazy, ok, ready, Ready};
|
||||||
|
|
||||||
|
use crate::{pipeline, pipeline_factory, Service, ServiceFactory};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Srv1(Rc<Cell<usize>>);
|
struct Srv1(Rc<Cell<usize>>);
|
||||||
|
|
||||||
impl Service for Srv1 {
|
impl Service for Srv1 {
|
||||||
type Request = Result<&'static str, &'static str>;
|
type Request = Result<&'static str, &'static str>;
|
||||||
type Response = &'static str;
|
type Response = &'static str;
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<Self::Response, Self::Error>;
|
type Future = Ready<Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.0.set(self.0.get() + 1);
|
self.0.set(self.0.get() + 1);
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Result<&'static str, &'static str>) -> Self::Future {
|
fn call(&mut self, req: Result<&'static str, &'static str>) -> Self::Future {
|
||||||
@ -284,11 +297,11 @@ mod tests {
|
|||||||
type Request = Result<&'static str, ()>;
|
type Request = Result<&'static str, ()>;
|
||||||
type Response = (&'static str, &'static str);
|
type Response = (&'static str, &'static str);
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = FutureResult<Self::Response, ()>;
|
type Future = Ready<Result<Self::Response, ()>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.0.set(self.0.get() + 1);
|
self.0.set(self.0.get() + 1);
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Err(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Result<&'static str, ()>) -> Self::Future {
|
fn call(&mut self, req: Result<&'static str, ()>) -> Self::Future {
|
||||||
@ -299,46 +312,42 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_poll_ready() {
|
async fn test_poll_ready() {
|
||||||
let cnt = Rc::new(Cell::new(0));
|
let cnt = Rc::new(Cell::new(0));
|
||||||
let mut srv = Srv1(cnt.clone()).then(Srv2(cnt.clone()));
|
let mut srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt.clone()));
|
||||||
let res = srv.poll_ready();
|
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
||||||
assert!(res.is_ok());
|
assert_eq!(res, Poll::Ready(Err(())));
|
||||||
assert_eq!(res.unwrap(), Async::Ready(()));
|
|
||||||
assert_eq!(cnt.get(), 2);
|
assert_eq!(cnt.get(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_call() {
|
async fn test_call() {
|
||||||
let cnt = Rc::new(Cell::new(0));
|
let cnt = Rc::new(Cell::new(0));
|
||||||
let mut srv = Srv1(cnt.clone()).then(Srv2(cnt)).clone();
|
let mut srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt));
|
||||||
|
|
||||||
let res = srv.call(Ok("srv1")).poll();
|
let res = srv.call(Ok("srv1")).await;
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), Async::Ready(("srv1", "ok")));
|
assert_eq!(res.unwrap(), (("srv1", "ok")));
|
||||||
|
|
||||||
let res = srv.call(Err("srv")).poll();
|
let res = srv.call(Err("srv")).await;
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), Async::Ready(("srv2", "err")));
|
assert_eq!(res.unwrap(), (("srv2", "err")));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_new_service() {
|
async fn test_factory() {
|
||||||
let cnt = Rc::new(Cell::new(0));
|
let cnt = Rc::new(Cell::new(0));
|
||||||
let cnt2 = cnt.clone();
|
let cnt2 = cnt.clone();
|
||||||
let blank = move || Ok::<_, ()>(Srv1(cnt2.clone()));
|
let blank = move || ready(Ok::<_, ()>(Srv1(cnt2.clone())));
|
||||||
let new_srv = blank.into_new_service().then(move || Ok(Srv2(cnt.clone())));
|
let factory = pipeline_factory(blank).then(move || ready(Ok(Srv2(cnt.clone()))));
|
||||||
if let Async::Ready(mut srv) = new_srv.clone().new_service(&()).poll().unwrap() {
|
let mut srv = factory.new_service(&()).await.unwrap();
|
||||||
let res = srv.call(Ok("srv1")).poll();
|
let res = srv.call(Ok("srv1")).await;
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), Async::Ready(("srv1", "ok")));
|
assert_eq!(res.unwrap(), (("srv1", "ok")));
|
||||||
|
|
||||||
let res = srv.call(Err("srv")).poll();
|
let res = srv.call(Err("srv")).await;
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), Async::Ready(("srv2", "err")));
|
assert_eq!(res.unwrap(), (("srv2", "err")));
|
||||||
} else {
|
|
||||||
panic!()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use futures::{Async, Future, IntoFuture, Poll};
|
use crate::transform_err::TransformMapInitErr;
|
||||||
|
use crate::{IntoServiceFactory, Service, ServiceFactory};
|
||||||
|
|
||||||
use crate::transform_err::{TransformFromErr, TransformMapInitErr};
|
use pin_project::pin_project;
|
||||||
use crate::{IntoNewService, NewService, Service};
|
|
||||||
|
|
||||||
/// The `Transform` trait defines the interface of a Service factory. `Transform`
|
/// The `Transform` trait defines the interface of a Service factory. `Transform`
|
||||||
/// is often implemented for middleware, defining how to construct a
|
/// is often implemented for middleware, defining how to construct a
|
||||||
@ -32,7 +35,7 @@ pub trait Transform<S> {
|
|||||||
type InitError;
|
type InitError;
|
||||||
|
|
||||||
/// The future response value.
|
/// The future response value.
|
||||||
type Future: Future<Item = Self::Transform, Error = Self::InitError>;
|
type Future: Future<Output = Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
/// Creates and returns a new Service component, asynchronously
|
/// Creates and returns a new Service component, asynchronously
|
||||||
fn new_transform(&self, service: S) -> Self::Future;
|
fn new_transform(&self, service: S) -> Self::Future;
|
||||||
@ -46,32 +49,6 @@ pub trait Transform<S> {
|
|||||||
{
|
{
|
||||||
TransformMapInitErr::new(self, f)
|
TransformMapInitErr::new(self, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Map this service's init error to any error implementing `From` for
|
|
||||||
/// this service`s `Error`.
|
|
||||||
///
|
|
||||||
/// Note that this function consumes the receiving transform and returns a
|
|
||||||
/// wrapped version of it.
|
|
||||||
fn from_err<E>(self) -> TransformFromErr<Self, S, E>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
E: From<Self::InitError>,
|
|
||||||
{
|
|
||||||
TransformFromErr::new(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
// /// Map this service's init error to service's init error
|
|
||||||
// /// if it is implementing `Into` to this service`s `InitError`.
|
|
||||||
// ///
|
|
||||||
// /// Note that this function consumes the receiving transform and returns a
|
|
||||||
// /// wrapped version of it.
|
|
||||||
// fn into_err<E>(self) -> TransformIntoErr<Self, S>
|
|
||||||
// where
|
|
||||||
// Self: Sized,
|
|
||||||
// Self::InitError: From<Self::InitError>,
|
|
||||||
// {
|
|
||||||
// TransformFromErr::new(self)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, S> Transform<S> for Rc<T>
|
impl<T, S> Transform<S> for Rc<T>
|
||||||
@ -106,31 +83,13 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait for types that can be converted to a *transform service*
|
/// Apply transform to a service. Function returns
|
||||||
pub trait IntoTransform<T, S>
|
|
||||||
where
|
|
||||||
T: Transform<S>,
|
|
||||||
{
|
|
||||||
/// Convert to a `TransformService`
|
|
||||||
fn into_transform(self) -> T;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, S> IntoTransform<T, S> for T
|
|
||||||
where
|
|
||||||
T: Transform<S>,
|
|
||||||
{
|
|
||||||
fn into_transform(self) -> T {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Apply transform to service factory. Function returns
|
|
||||||
/// services factory that in initialization creates
|
/// services factory that in initialization creates
|
||||||
/// service and applies transform to this service.
|
/// service and applies transform to this service.
|
||||||
pub fn apply_transform<T, S, F, U>(
|
pub fn apply<T, S, U>(
|
||||||
t: F,
|
t: T,
|
||||||
service: U,
|
service: U,
|
||||||
) -> impl NewService<
|
) -> impl ServiceFactory<
|
||||||
Config = S::Config,
|
Config = S::Config,
|
||||||
Request = T::Request,
|
Request = T::Request,
|
||||||
Response = T::Response,
|
Response = T::Response,
|
||||||
@ -139,30 +98,29 @@ pub fn apply_transform<T, S, F, U>(
|
|||||||
InitError = S::InitError,
|
InitError = S::InitError,
|
||||||
> + Clone
|
> + Clone
|
||||||
where
|
where
|
||||||
S: NewService,
|
S: ServiceFactory,
|
||||||
T: Transform<S::Service, InitError = S::InitError>,
|
T: Transform<S::Service, InitError = S::InitError>,
|
||||||
F: IntoTransform<T, S::Service>,
|
U: IntoServiceFactory<S>,
|
||||||
U: IntoNewService<S>,
|
|
||||||
{
|
{
|
||||||
ApplyTransform::new(t.into_transform(), service.into_new_service())
|
ApplyTransform::new(t, service.into_factory())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `Apply` transform to new service
|
/// `Apply` transform to new service
|
||||||
pub struct ApplyTransform<T, S> {
|
struct ApplyTransform<T, S> {
|
||||||
s: Rc<S>,
|
s: Rc<S>,
|
||||||
t: Rc<T>,
|
t: Rc<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, S> ApplyTransform<T, S>
|
impl<T, S> ApplyTransform<T, S>
|
||||||
where
|
where
|
||||||
S: NewService,
|
S: ServiceFactory,
|
||||||
T: Transform<S::Service, InitError = S::InitError>,
|
T: Transform<S::Service, InitError = S::InitError>,
|
||||||
{
|
{
|
||||||
/// Create new `ApplyTransform` new service instance
|
/// Create new `ApplyTransform` new service instance
|
||||||
pub fn new<F: IntoTransform<T, S::Service>>(t: F, service: S) -> Self {
|
fn new(t: T, service: S) -> Self {
|
||||||
Self {
|
Self {
|
||||||
s: Rc::new(service),
|
s: Rc::new(service),
|
||||||
t: Rc::new(t.into_transform()),
|
t: Rc::new(t),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -176,9 +134,9 @@ impl<T, S> Clone for ApplyTransform<T, S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, S> NewService for ApplyTransform<T, S>
|
impl<T, S> ServiceFactory for ApplyTransform<T, S>
|
||||||
where
|
where
|
||||||
S: NewService,
|
S: ServiceFactory,
|
||||||
T: Transform<S::Service, InitError = S::InitError>,
|
T: Transform<S::Service, InitError = S::InitError>,
|
||||||
{
|
{
|
||||||
type Request = T::Request;
|
type Request = T::Request;
|
||||||
@ -193,40 +151,45 @@ where
|
|||||||
fn new_service(&self, cfg: &S::Config) -> Self::Future {
|
fn new_service(&self, cfg: &S::Config) -> Self::Future {
|
||||||
ApplyTransformFuture {
|
ApplyTransformFuture {
|
||||||
t_cell: self.t.clone(),
|
t_cell: self.t.clone(),
|
||||||
fut_a: self.s.new_service(cfg).into_future(),
|
fut_a: self.s.new_service(cfg),
|
||||||
fut_t: None,
|
fut_t: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ApplyTransformFuture<T, S>
|
#[pin_project]
|
||||||
|
struct ApplyTransformFuture<T, S>
|
||||||
where
|
where
|
||||||
S: NewService,
|
S: ServiceFactory,
|
||||||
T: Transform<S::Service, InitError = S::InitError>,
|
T: Transform<S::Service, InitError = S::InitError>,
|
||||||
{
|
{
|
||||||
|
#[pin]
|
||||||
fut_a: S::Future,
|
fut_a: S::Future,
|
||||||
fut_t: Option<<T::Future as IntoFuture>::Future>,
|
#[pin]
|
||||||
|
fut_t: Option<T::Future>,
|
||||||
t_cell: Rc<T>,
|
t_cell: Rc<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, S> Future for ApplyTransformFuture<T, S>
|
impl<T, S> Future for ApplyTransformFuture<T, S>
|
||||||
where
|
where
|
||||||
S: NewService,
|
S: ServiceFactory,
|
||||||
T: Transform<S::Service, InitError = S::InitError>,
|
T: Transform<S::Service, InitError = S::InitError>,
|
||||||
{
|
{
|
||||||
type Item = T::Transform;
|
type Output = Result<T::Transform, T::InitError>;
|
||||||
type Error = T::InitError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
if self.fut_t.is_none() {
|
let mut this = self.project();
|
||||||
if let Async::Ready(service) = self.fut_a.poll()? {
|
|
||||||
self.fut_t = Some(self.t_cell.new_transform(service).into_future());
|
if this.fut_t.as_mut().as_pin_mut().is_none() {
|
||||||
|
if let Poll::Ready(service) = this.fut_a.poll(cx)? {
|
||||||
|
this.fut_t.set(Some(this.t_cell.new_transform(service)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(ref mut fut) = self.fut_t {
|
|
||||||
fut.poll()
|
if let Some(fut) = this.fut_t.as_mut().as_pin_mut() {
|
||||||
|
fut.poll(cx)
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use futures::{Future, Poll};
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use super::Transform;
|
use super::Transform;
|
||||||
|
|
||||||
@ -63,12 +66,13 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[pin_project]
|
||||||
pub struct TransformMapInitErrFuture<T, S, F, E>
|
pub struct TransformMapInitErrFuture<T, S, F, E>
|
||||||
where
|
where
|
||||||
T: Transform<S>,
|
T: Transform<S>,
|
||||||
F: Fn(T::InitError) -> E,
|
F: Fn(T::InitError) -> E,
|
||||||
{
|
{
|
||||||
|
#[pin]
|
||||||
fut: T::Future,
|
fut: T::Future,
|
||||||
f: F,
|
f: F,
|
||||||
}
|
}
|
||||||
@ -78,85 +82,10 @@ where
|
|||||||
T: Transform<S>,
|
T: Transform<S>,
|
||||||
F: Fn(T::InitError) -> E + Clone,
|
F: Fn(T::InitError) -> E + Clone,
|
||||||
{
|
{
|
||||||
type Item = T::Transform;
|
type Output = Result<T::Transform, E>;
|
||||||
type Error = E;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
self.fut.poll().map_err(&self.f)
|
let this = self.project();
|
||||||
}
|
this.fut.poll(cx).map_err(this.f)
|
||||||
}
|
|
||||||
|
|
||||||
/// Transform for the `from_err` combinator, changing the type of a new
|
|
||||||
/// transform's init error.
|
|
||||||
///
|
|
||||||
/// This is created by the `Transform::from_err` method.
|
|
||||||
pub struct TransformFromErr<T, S, E> {
|
|
||||||
t: T,
|
|
||||||
e: PhantomData<(S, E)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, S, E> TransformFromErr<T, S, E>
|
|
||||||
where
|
|
||||||
T: Transform<S>,
|
|
||||||
E: From<T::InitError>,
|
|
||||||
{
|
|
||||||
/// Create new `TransformFromErr` new transform instance
|
|
||||||
pub fn new(t: T) -> Self {
|
|
||||||
Self { t, e: PhantomData }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, S, E> Clone for TransformFromErr<T, S, E>
|
|
||||||
where
|
|
||||||
T: Clone,
|
|
||||||
{
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self {
|
|
||||||
t: self.t.clone(),
|
|
||||||
e: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, S, E> Transform<S> for TransformFromErr<T, S, E>
|
|
||||||
where
|
|
||||||
T: Transform<S>,
|
|
||||||
E: From<T::InitError>,
|
|
||||||
{
|
|
||||||
type Request = T::Request;
|
|
||||||
type Response = T::Response;
|
|
||||||
type Error = T::Error;
|
|
||||||
type Transform = T::Transform;
|
|
||||||
|
|
||||||
type InitError = E;
|
|
||||||
type Future = TransformFromErrFuture<T, S, E>;
|
|
||||||
|
|
||||||
fn new_transform(&self, service: S) -> Self::Future {
|
|
||||||
TransformFromErrFuture {
|
|
||||||
fut: self.t.new_transform(service),
|
|
||||||
_t: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct TransformFromErrFuture<T, S, E>
|
|
||||||
where
|
|
||||||
T: Transform<S>,
|
|
||||||
E: From<T::InitError>,
|
|
||||||
{
|
|
||||||
fut: T::Future,
|
|
||||||
_t: PhantomData<E>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T, S, E> Future for TransformFromErrFuture<T, S, E>
|
|
||||||
where
|
|
||||||
T: Transform<S>,
|
|
||||||
E: From<T::InitError>,
|
|
||||||
{
|
|
||||||
type Item = T::Transform;
|
|
||||||
type Error = E;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
self.fut.poll().map_err(E::from)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-test-server"
|
name = "actix-test-server"
|
||||||
version = "0.2.3"
|
version = "0.2.2"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix test server"
|
description = "Actix test server"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -11,11 +11,27 @@ categories = ["network-programming", "asynchronous"]
|
|||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
workspace = ".."
|
||||||
|
|
||||||
|
[package.metadata.docs.rs]
|
||||||
|
features = ["ssl", "tls", "rust-tls"]
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "actix_test_server"
|
name = "actix_test_server"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = []
|
||||||
|
|
||||||
|
# tls
|
||||||
|
tls = ["native-tls", "actix-server/tls"]
|
||||||
|
|
||||||
|
# openssl
|
||||||
|
ssl = ["openssl", "actix-server/ssl"]
|
||||||
|
|
||||||
|
# rustls
|
||||||
|
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "0.2.1"
|
actix-rt = "0.2.1"
|
||||||
actix-server = "0.5.0"
|
actix-server = "0.5.0"
|
||||||
@ -24,9 +40,21 @@ actix-testing = "0.1.0"
|
|||||||
|
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
net2 = "0.2"
|
net2 = "0.2"
|
||||||
futures = "0.1"
|
futures = { package = "futures-preview", version = "0.3.0-alpha.18"}
|
||||||
tokio-tcp = "0.1"
|
tokio-io = "0.2.0-alpha.4"
|
||||||
tokio-reactor = "0.1"
|
tokio-net = "0.2.0-alpha.4"
|
||||||
|
|
||||||
|
# native-tls
|
||||||
|
native-tls = { version="0.2", optional = true }
|
||||||
|
|
||||||
|
# openssl
|
||||||
|
openssl = { version="0.10", optional = true }
|
||||||
|
|
||||||
|
#rustls
|
||||||
|
rustls = { version = "^0.16", optional = true }
|
||||||
|
tokio-rustls = { version = "^0.12.0-alpha.2", optional = true }
|
||||||
|
webpki = { version = "0.21", optional = true }
|
||||||
|
webpki-roots = { version = "0.17", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-service = "0.4.0"
|
actix-service = "0.4.0"
|
||||||
|
@ -1,2 +1,149 @@
|
|||||||
//! Various helpers for Actix applications to use during testing.
|
//! Various helpers for Actix applications to use during testing.
|
||||||
pub use actix_testing::*;
|
use std::sync::mpsc;
|
||||||
|
use std::{net, thread};
|
||||||
|
|
||||||
|
use actix_rt::{Runtime, System};
|
||||||
|
use actix_server::{Server, StreamServiceFactory};
|
||||||
|
pub use actix_server_config::{Io, ServerConfig};
|
||||||
|
|
||||||
|
use futures::future::{lazy, Future, IntoFuture};
|
||||||
|
use net2::TcpBuilder;
|
||||||
|
use tokio_reactor::Handle;
|
||||||
|
use tokio_tcp::TcpStream;
|
||||||
|
|
||||||
|
/// The `TestServer` type.
|
||||||
|
///
|
||||||
|
/// `TestServer` is very simple test server that simplify process of writing
|
||||||
|
/// integration tests for actix-net applications.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use actix_service::{service_fn, IntoNewService};
|
||||||
|
/// use actix_test_server::TestServer;
|
||||||
|
///
|
||||||
|
/// fn main() {
|
||||||
|
/// let srv = TestServer::with(|| service_fn(
|
||||||
|
/// |sock| {
|
||||||
|
/// println!("New connection: {:?}", sock);
|
||||||
|
/// Ok::<_, ()>(())
|
||||||
|
/// }
|
||||||
|
/// ));
|
||||||
|
///
|
||||||
|
/// println!("SOCKET: {:?}", srv.connect());
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub struct TestServer;
|
||||||
|
|
||||||
|
/// Test server runstime
|
||||||
|
pub struct TestServerRuntime {
|
||||||
|
addr: net::SocketAddr,
|
||||||
|
host: String,
|
||||||
|
port: u16,
|
||||||
|
rt: Runtime,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TestServer {
|
||||||
|
/// Start new test server with application factory
|
||||||
|
pub fn with<F: StreamServiceFactory>(factory: F) -> TestServerRuntime {
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
|
// run server in separate thread
|
||||||
|
thread::spawn(move || {
|
||||||
|
let sys = System::new("actix-test-server");
|
||||||
|
let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||||
|
let local_addr = tcp.local_addr().unwrap();
|
||||||
|
|
||||||
|
Server::build()
|
||||||
|
.listen("test", tcp, factory)?
|
||||||
|
.workers(1)
|
||||||
|
.disable_signals()
|
||||||
|
.start();
|
||||||
|
|
||||||
|
tx.send((System::current(), local_addr)).unwrap();
|
||||||
|
sys.run()
|
||||||
|
});
|
||||||
|
|
||||||
|
let (system, addr) = rx.recv().unwrap();
|
||||||
|
System::set_current(system);
|
||||||
|
|
||||||
|
let rt = Runtime::new().unwrap();
|
||||||
|
let host = format!("{}", addr.ip());
|
||||||
|
let port = addr.port();
|
||||||
|
|
||||||
|
TestServerRuntime {
|
||||||
|
addr,
|
||||||
|
rt,
|
||||||
|
host,
|
||||||
|
port,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get firat available unused local address
|
||||||
|
pub fn unused_addr() -> net::SocketAddr {
|
||||||
|
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
||||||
|
let socket = TcpBuilder::new_v4().unwrap();
|
||||||
|
socket.bind(&addr).unwrap();
|
||||||
|
socket.reuse_address(true).unwrap();
|
||||||
|
let tcp = socket.to_tcp_listener().unwrap();
|
||||||
|
tcp.local_addr().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TestServerRuntime {
|
||||||
|
/// Execute future on current runtime
|
||||||
|
pub fn block_on<F, I, E>(&mut self, fut: F) -> Result<I, E>
|
||||||
|
where
|
||||||
|
F: Future<Item = I, Error = E>,
|
||||||
|
{
|
||||||
|
self.rt.block_on(fut)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the provided function, with runtime enabled.
|
||||||
|
pub fn run_on<F, R>(&mut self, f: F) -> Result<R::Item, R::Error>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> R,
|
||||||
|
R: IntoFuture,
|
||||||
|
{
|
||||||
|
self.rt.block_on(lazy(|| f().into_future()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Spawn future to the current runtime
|
||||||
|
pub fn spawn<F>(&mut self, fut: F)
|
||||||
|
where
|
||||||
|
F: Future<Item = (), Error = ()> + 'static,
|
||||||
|
{
|
||||||
|
self.rt.spawn(fut);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test server host
|
||||||
|
pub fn host(&self) -> &str {
|
||||||
|
&self.host
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test server port
|
||||||
|
pub fn port(&self) -> u16 {
|
||||||
|
self.port
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get test server address
|
||||||
|
pub fn addr(&self) -> net::SocketAddr {
|
||||||
|
self.addr
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stop http server
|
||||||
|
fn stop(&mut self) {
|
||||||
|
System::current().stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Connect to server, return tokio TcpStream
|
||||||
|
pub fn connect(&self) -> std::io::Result<TcpStream> {
|
||||||
|
TcpStream::from_std(net::TcpStream::connect(self.addr)?, &Handle::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for TestServerRuntime {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-testing"
|
name = "actix-testing"
|
||||||
version = "0.2.0"
|
version = "0.3.0-alpha.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix testing utils"
|
description = "Actix testing utils"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -17,13 +17,13 @@ name = "actix_testing"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "0.2.5"
|
actix-rt = "1.0.0-alpha.1"
|
||||||
actix-server = "0.7.0"
|
actix-server = "0.8.0-alpha.1"
|
||||||
actix-server-config = "0.2.0"
|
actix-server-config = "0.3.0-alpha.1"
|
||||||
actix-service = "0.4.2"
|
actix-service = "1.0.0-alpha.1"
|
||||||
|
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
net2 = "0.2"
|
net2 = "0.2"
|
||||||
futures = "0.1"
|
futures = "0.3.1"
|
||||||
tokio-tcp = "0.1"
|
tokio = "0.2.0-alpha.6"
|
||||||
tokio-reactor = "0.1"
|
tokio-net = { version = "0.2.0-alpha.6" }
|
||||||
|
@ -3,12 +3,12 @@ use std::sync::mpsc;
|
|||||||
use std::{net, thread};
|
use std::{net, thread};
|
||||||
|
|
||||||
use actix_rt::System;
|
use actix_rt::System;
|
||||||
use actix_server::{Server, ServerBuilder, StreamServiceFactory};
|
use actix_server::{Server, ServerBuilder, ServiceFactory};
|
||||||
pub use actix_server_config::{Io, ServerConfig};
|
pub use actix_server_config::{Io, ServerConfig};
|
||||||
|
|
||||||
use net2::TcpBuilder;
|
use net2::TcpBuilder;
|
||||||
use tokio_reactor::Handle;
|
use tokio_net::driver::Handle;
|
||||||
use tokio_tcp::TcpStream;
|
use tokio_net::tcp::TcpStream;
|
||||||
|
|
||||||
mod rt;
|
mod rt;
|
||||||
pub use self::rt::*;
|
pub use self::rt::*;
|
||||||
@ -21,12 +21,12 @@ pub use self::rt::*;
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use actix_service::{service_fn, IntoNewService};
|
/// use actix_service::{service_fn};
|
||||||
/// use actix_testing::TestServer;
|
/// use actix_testing::TestServer;
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let srv = TestServer::with(|| service_fn(
|
/// let srv = TestServer::with(|| service_fn(
|
||||||
/// |sock| {
|
/// |sock| async move {
|
||||||
/// println!("New connection: {:?}", sock);
|
/// println!("New connection: {:?}", sock);
|
||||||
/// Ok::<_, ()>(())
|
/// Ok::<_, ()>(())
|
||||||
/// }
|
/// }
|
||||||
@ -75,7 +75,7 @@ impl TestServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Start new test server with application factory
|
/// Start new test server with application factory
|
||||||
pub fn with<F: StreamServiceFactory<TcpStream>>(factory: F) -> TestServerRuntime {
|
pub fn with<F: ServiceFactory<TcpStream>>(factory: F) -> TestServerRuntime {
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
// run server in separate thread
|
// run server in separate thread
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
//! Various helpers for Actix applications to use during testing.
|
//! Various helpers for Actix applications to use during testing.
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::future::Future;
|
||||||
|
|
||||||
use actix_rt::{System, SystemRunner};
|
use actix_rt::{System, SystemRunner};
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use futures::future::{lazy, Future, IntoFuture};
|
use futures::future::{lazy, FutureExt};
|
||||||
|
// use futures_util::future::FutureExt;
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
static RT: RefCell<Inner> = {
|
static RT: RefCell<Inner> = {
|
||||||
@ -35,11 +37,11 @@ impl Drop for Inner {
|
|||||||
///
|
///
|
||||||
/// Note that this function is intended to be used only for testing purpose.
|
/// Note that this function is intended to be used only for testing purpose.
|
||||||
/// This function panics on nested call.
|
/// This function panics on nested call.
|
||||||
pub fn block_on<F>(f: F) -> Result<F::Item, F::Error>
|
pub fn block_on<F>(f: F) -> F::Output
|
||||||
where
|
where
|
||||||
F: IntoFuture,
|
F: Future,
|
||||||
{
|
{
|
||||||
RT.with(move |rt| rt.borrow_mut().get_mut().block_on(f.into_future()))
|
RT.with(move |rt| rt.borrow_mut().get_mut().block_on(f))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runs the provided function, blocking the current thread until the result
|
/// Runs the provided function, blocking the current thread until the result
|
||||||
@ -52,21 +54,21 @@ where
|
|||||||
///
|
///
|
||||||
/// Note that this function is intended to be used only for testing purpose.
|
/// Note that this function is intended to be used only for testing purpose.
|
||||||
/// This function panics on nested call.
|
/// This function panics on nested call.
|
||||||
pub fn block_fn<F, R>(f: F) -> Result<R::Item, R::Error>
|
pub fn block_fn<F, R>(f: F) -> F::Output
|
||||||
where
|
where
|
||||||
F: FnOnce() -> R,
|
F: FnOnce() -> R,
|
||||||
R: IntoFuture,
|
R: Future,
|
||||||
{
|
{
|
||||||
RT.with(move |rt| rt.borrow_mut().get_mut().block_on(lazy(f)))
|
RT.with(move |rt| rt.borrow_mut().get_mut().block_on(lazy(|_| f())))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Spawn future to the current test runtime.
|
/// Spawn future to the current test runtime.
|
||||||
pub fn spawn<F>(fut: F)
|
pub fn spawn<F>(fut: F)
|
||||||
where
|
where
|
||||||
F: Future<Item = (), Error = ()> + 'static,
|
F: Future + 'static,
|
||||||
{
|
{
|
||||||
run_on(move || {
|
run_on(move || {
|
||||||
actix_rt::spawn(fut);
|
actix_rt::spawn(fut.map(|_| ()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,12 +80,7 @@ pub fn run_on<F, R>(f: F) -> R
|
|||||||
where
|
where
|
||||||
F: FnOnce() -> R,
|
F: FnOnce() -> R,
|
||||||
{
|
{
|
||||||
RT.with(move |rt| {
|
RT.with(move |rt| rt.borrow_mut().get_mut().block_on(lazy(|_| f())))
|
||||||
rt.borrow_mut()
|
|
||||||
.get_mut()
|
|
||||||
.block_on(lazy(|| Ok::<_, ()>(f())))
|
|
||||||
})
|
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calls service and waits for response future completion.
|
/// Calls service and waits for response future completion.
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.2.0] - 2019-??-??
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Migrate to `std::future`
|
||||||
|
|
||||||
## [0.1.2] - 2019-08-05
|
## [0.1.2] - 2019-08-05
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-threadpool"
|
name = "actix-threadpool"
|
||||||
version = "0.1.2"
|
version = "0.2.0"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix thread pool for sync code"
|
description = "Actix thread pool for sync code"
|
||||||
keywords = ["actix", "network", "framework", "async", "futures"]
|
keywords = ["actix", "network", "framework", "async", "futures"]
|
||||||
@ -19,7 +19,7 @@ path = "src/lib.rs"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
derive_more = "0.15"
|
derive_more = "0.15"
|
||||||
futures = "0.1.25"
|
futures = "0.3.1"
|
||||||
parking_lot = "0.9"
|
parking_lot = "0.9"
|
||||||
lazy_static = "1.2"
|
lazy_static = "1.2"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
@ -1,33 +1,34 @@
|
|||||||
//! Thread pool for blocking operations
|
//! Thread pool for blocking operations
|
||||||
|
|
||||||
use std::fmt;
|
use std::{
|
||||||
|
future::Future,
|
||||||
|
pin::Pin,
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
|
||||||
use derive_more::Display;
|
use derive_more::Display;
|
||||||
use futures::sync::oneshot;
|
use futures::channel::oneshot;
|
||||||
use futures::{Async, Future, Poll};
|
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use threadpool::ThreadPool;
|
use threadpool::ThreadPool;
|
||||||
|
|
||||||
/// Env variable for default cpu pool size
|
/// Env variable for default cpu pool size.
|
||||||
const ENV_CPU_POOL_VAR: &str = "ACTIX_THREADPOOL";
|
const ENV_CPU_POOL_VAR: &str = "ACTIX_THREADPOOL";
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
pub(crate) static ref DEFAULT_POOL: Mutex<ThreadPool> = {
|
pub(crate) static ref DEFAULT_POOL: Mutex<ThreadPool> = {
|
||||||
let default = match std::env::var(ENV_CPU_POOL_VAR) {
|
let num = std::env::var(ENV_CPU_POOL_VAR)
|
||||||
Ok(val) => {
|
.map_err(|_| ())
|
||||||
if let Ok(val) = val.parse() {
|
.and_then(|val| {
|
||||||
val
|
val.parse().map_err(|_| log::warn!(
|
||||||
} else {
|
"Can not parse {} value, using default",
|
||||||
log::error!("Can not parse ACTIX_THREADPOOL value");
|
ENV_CPU_POOL_VAR,
|
||||||
num_cpus::get() * 5
|
))
|
||||||
}
|
})
|
||||||
}
|
.unwrap_or_else(|_| num_cpus::get() * 5);
|
||||||
Err(_) => num_cpus::get() * 5,
|
|
||||||
};
|
|
||||||
Mutex::new(
|
Mutex::new(
|
||||||
threadpool::Builder::new()
|
threadpool::Builder::new()
|
||||||
.thread_name("actix-web".to_owned())
|
.thread_name("actix-web".to_owned())
|
||||||
.num_threads(default)
|
.num_threads(num)
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
@ -39,22 +40,17 @@ thread_local! {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Blocking operation execution error
|
/// Error of blocking operation execution being cancelled.
|
||||||
#[derive(Debug, Display)]
|
#[derive(Clone, Copy, Debug, Display)]
|
||||||
pub enum BlockingError<E: fmt::Debug> {
|
#[display(fmt = "Thread pool is gone")]
|
||||||
#[display(fmt = "{:?}", _0)]
|
pub struct Cancelled;
|
||||||
Error(E),
|
|
||||||
#[display(fmt = "Thread pool is gone")]
|
|
||||||
Canceled,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Execute blocking function on a thread pool, returns future that resolves
|
/// Execute blocking function on a thread pool, returns future that resolves
|
||||||
/// to result of the function execution.
|
/// to result of the function execution.
|
||||||
pub fn run<F, I, E>(f: F) -> CpuFuture<I, E>
|
pub fn run<F, I>(f: F) -> CpuFuture<I>
|
||||||
where
|
where
|
||||||
F: FnOnce() -> Result<I, E> + Send + 'static,
|
F: FnOnce() -> I + Send + 'static,
|
||||||
I: Send + 'static,
|
I: Send + 'static,
|
||||||
E: Send + fmt::Debug + 'static,
|
|
||||||
{
|
{
|
||||||
let (tx, rx) = oneshot::channel();
|
let (tx, rx) = oneshot::channel();
|
||||||
POOL.with(|pool| {
|
POOL.with(|pool| {
|
||||||
@ -70,19 +66,16 @@ where
|
|||||||
|
|
||||||
/// Blocking operation completion future. It resolves with results
|
/// Blocking operation completion future. It resolves with results
|
||||||
/// of blocking function execution.
|
/// of blocking function execution.
|
||||||
pub struct CpuFuture<I, E> {
|
pub struct CpuFuture<I> {
|
||||||
rx: oneshot::Receiver<Result<I, E>>,
|
rx: oneshot::Receiver<I>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I, E: fmt::Debug> Future for CpuFuture<I, E> {
|
impl<I> Future for CpuFuture<I> {
|
||||||
type Item = I;
|
type Output = Result<I, Cancelled>;
|
||||||
type Error = BlockingError<E>;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let res = futures::try_ready!(self.rx.poll().map_err(|_| BlockingError::Canceled));
|
let rx = Pin::new(&mut Pin::get_mut(self).rx);
|
||||||
match res {
|
let res = futures::ready!(rx.poll(cx));
|
||||||
Ok(val) => Ok(Async::Ready(val)),
|
Poll::Ready(res.map_err(|_| Cancelled))
|
||||||
Err(err) => Err(BlockingError::Error(err)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-utils"
|
name = "actix-utils"
|
||||||
version = "0.4.7"
|
version = "0.5.0-alpha1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix utils - various actix net related services"
|
description = "Actix utils - various actix net related services"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -18,14 +18,15 @@ name = "actix_utils"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-service = "0.4.1"
|
actix-service = "1.0.0-alpha.1"
|
||||||
actix-codec = "0.1.2"
|
actix-codec = "0.2.0-alpha.1"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
either = "1.5.2"
|
either = "1.5.2"
|
||||||
futures = "0.1.25"
|
futures = "0.3.1"
|
||||||
tokio-timer = "0.2.8"
|
pin-project = "0.4.5"
|
||||||
tokio-current-thread = "0.1.4"
|
tokio-timer = "0.3.0-alpha.6"
|
||||||
|
tokio-executor = { version="=0.2.0-alpha.6", features=["current-thread"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "0.2.2"
|
actix-rt = "1.0.0-alpha.1"
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::task;
|
||||||
|
|
||||||
use futures::task::AtomicTask;
|
use crate::task::LocalWaker;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
/// Simple counter with ability to notify task on reaching specific number
|
/// Simple counter with ability to notify task on reaching specific number
|
||||||
@ -12,7 +13,7 @@ pub struct Counter(Rc<CounterInner>);
|
|||||||
struct CounterInner {
|
struct CounterInner {
|
||||||
count: Cell<usize>,
|
count: Cell<usize>,
|
||||||
capacity: usize,
|
capacity: usize,
|
||||||
task: AtomicTask,
|
task: LocalWaker,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Counter {
|
impl Counter {
|
||||||
@ -21,7 +22,7 @@ impl Counter {
|
|||||||
Counter(Rc::new(CounterInner {
|
Counter(Rc::new(CounterInner {
|
||||||
capacity,
|
capacity,
|
||||||
count: Cell::new(0),
|
count: Cell::new(0),
|
||||||
task: AtomicTask::new(),
|
task: LocalWaker::new(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,8 +33,8 @@ impl Counter {
|
|||||||
|
|
||||||
/// Check if counter is not at capacity. If counter at capacity
|
/// Check if counter is not at capacity. If counter at capacity
|
||||||
/// it registers notification for current task.
|
/// it registers notification for current task.
|
||||||
pub fn available(&self) -> bool {
|
pub fn available(&self, cx: &mut task::Context) -> bool {
|
||||||
self.0.available()
|
self.0.available(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get total number of acquired counts
|
/// Get total number of acquired counts
|
||||||
@ -66,15 +67,15 @@ impl CounterInner {
|
|||||||
let num = self.count.get();
|
let num = self.count.get();
|
||||||
self.count.set(num - 1);
|
self.count.set(num - 1);
|
||||||
if num == self.capacity {
|
if num == self.capacity {
|
||||||
self.task.notify();
|
self.task.wake();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn available(&self) -> bool {
|
fn available(&self, cx: &mut task::Context) -> bool {
|
||||||
if self.count.get() < self.capacity {
|
if self.count.get() < self.capacity {
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
self.task.register();
|
self.task.register(cx.waker());
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
//! Contains `Either` service and related types and functions.
|
//! Contains `Either` service and related types and functions.
|
||||||
use actix_service::{IntoNewService, NewService, Service};
|
use std::pin::Pin;
|
||||||
use futures::{future, try_ready, Async, Future, IntoFuture, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
use actix_service::{Service, ServiceFactory};
|
||||||
|
use futures::{future, ready, Future};
|
||||||
|
use pin_project::pin_project;
|
||||||
|
|
||||||
/// Combine two different service types into a single type.
|
/// Combine two different service types into a single type.
|
||||||
///
|
///
|
||||||
@ -31,21 +35,21 @@ where
|
|||||||
type Error = A::Error;
|
type Error = A::Error;
|
||||||
type Future = future::Either<A::Future, B::Future>;
|
type Future = future::Either<A::Future, B::Future>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
let left = self.left.poll_ready()?;
|
let left = self.left.poll_ready(cx)?;
|
||||||
let right = self.right.poll_ready()?;
|
let right = self.right.poll_ready(cx)?;
|
||||||
|
|
||||||
if left.is_ready() && right.is_ready() {
|
if left.is_ready() && right.is_ready() {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: either::Either<A::Request, B::Request>) -> Self::Future {
|
fn call(&mut self, req: either::Either<A::Request, B::Request>) -> Self::Future {
|
||||||
match req {
|
match req {
|
||||||
either::Either::Left(req) => future::Either::A(self.left.call(req)),
|
either::Either::Left(req) => future::Either::Left(self.left.call(req)),
|
||||||
either::Either::Right(req) => future::Either::B(self.right.call(req)),
|
either::Either::Right(req) => future::Either::Right(self.right.call(req)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -57,29 +61,24 @@ pub struct Either<A, B> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B> Either<A, B> {
|
impl<A, B> Either<A, B> {
|
||||||
pub fn new<F1, F2>(srv_a: F1, srv_b: F2) -> Either<A, B>
|
pub fn new(left: A, right: B) -> Either<A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
B: NewService<
|
B: ServiceFactory<
|
||||||
Config = A::Config,
|
Config = A::Config,
|
||||||
Response = A::Response,
|
Response = A::Response,
|
||||||
Error = A::Error,
|
Error = A::Error,
|
||||||
InitError = A::InitError,
|
InitError = A::InitError,
|
||||||
>,
|
>,
|
||||||
F1: IntoNewService<A>,
|
|
||||||
F2: IntoNewService<B>,
|
|
||||||
{
|
{
|
||||||
Either {
|
Either { left, right }
|
||||||
left: srv_a.into_new_service(),
|
|
||||||
right: srv_b.into_new_service(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B> NewService for Either<A, B>
|
impl<A, B> ServiceFactory for Either<A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
B: NewService<
|
B: ServiceFactory<
|
||||||
Config = A::Config,
|
Config = A::Config,
|
||||||
Response = A::Response,
|
Response = A::Response,
|
||||||
Error = A::Error,
|
Error = A::Error,
|
||||||
@ -113,37 +112,41 @@ impl<A: Clone, B: Clone> Clone for Either<A, B> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pin_project]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub struct EitherNewService<A: NewService, B: NewService> {
|
pub struct EitherNewService<A: ServiceFactory, B: ServiceFactory> {
|
||||||
left: Option<A::Service>,
|
left: Option<A::Service>,
|
||||||
right: Option<B::Service>,
|
right: Option<B::Service>,
|
||||||
left_fut: <A::Future as IntoFuture>::Future,
|
#[pin]
|
||||||
right_fut: <B::Future as IntoFuture>::Future,
|
left_fut: A::Future,
|
||||||
|
#[pin]
|
||||||
|
right_fut: B::Future,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B> Future for EitherNewService<A, B>
|
impl<A, B> Future for EitherNewService<A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: ServiceFactory,
|
||||||
B: NewService<Response = A::Response, Error = A::Error, InitError = A::InitError>,
|
B: ServiceFactory<Response = A::Response, Error = A::Error, InitError = A::InitError>,
|
||||||
{
|
{
|
||||||
type Item = EitherService<A::Service, B::Service>;
|
type Output = Result<EitherService<A::Service, B::Service>, A::InitError>;
|
||||||
type Error = A::InitError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||||
if self.left.is_none() {
|
let this = self.project();
|
||||||
self.left = Some(try_ready!(self.left_fut.poll()));
|
|
||||||
|
if this.left.is_none() {
|
||||||
|
*this.left = Some(ready!(this.left_fut.poll(cx))?);
|
||||||
}
|
}
|
||||||
if self.right.is_none() {
|
if this.right.is_none() {
|
||||||
self.right = Some(try_ready!(self.right_fut.poll()));
|
*this.right = Some(ready!(this.right_fut.poll(cx))?);
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.left.is_some() && self.right.is_some() {
|
if this.left.is_some() && this.right.is_some() {
|
||||||
Ok(Async::Ready(EitherService {
|
Poll::Ready(Ok(EitherService {
|
||||||
left: self.left.take().unwrap(),
|
left: this.left.take().unwrap(),
|
||||||
right: self.right.take().unwrap(),
|
right: this.right.take().unwrap(),
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,20 @@
|
|||||||
//! Framed dispatcher service and related utilities
|
//! Framed dispatcher service and related utilities
|
||||||
|
#![allow(type_alias_bounds)]
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
use std::{fmt, mem};
|
use std::{fmt, mem};
|
||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
||||||
use actix_service::{IntoService, Service};
|
use actix_service::{IntoService, Service};
|
||||||
use futures::task::AtomicTask;
|
use futures::future::{ready, FutureExt};
|
||||||
use futures::unsync::mpsc;
|
use futures::{Future, Sink, Stream};
|
||||||
use futures::{Async, Future, Poll, Sink, Stream};
|
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use crate::cell::Cell;
|
use crate::cell::Cell;
|
||||||
|
use crate::mpsc;
|
||||||
|
use crate::task::LocalWaker;
|
||||||
|
|
||||||
type Request<U> = <U as Decoder>::Item;
|
type Request<U> = <U as Decoder>::Item;
|
||||||
type Response<U> = <U as Encoder>::Item;
|
type Response<U> = <U as Encoder>::Item;
|
||||||
@ -68,22 +73,26 @@ pub enum FramedMessage<T> {
|
|||||||
Close,
|
Close,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Rx<U> = Option<mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>>;
|
||||||
|
type Inner<S: Service, U> = Cell<FramedTransportInner<<U as Encoder>::Item, S::Error>>;
|
||||||
|
|
||||||
/// FramedTransport - is a future that reads frames from Framed object
|
/// FramedTransport - is a future that reads frames from Framed object
|
||||||
/// and pass then to the service.
|
/// and pass then to the service.
|
||||||
|
#[pin_project]
|
||||||
pub struct FramedTransport<S, T, U>
|
pub struct FramedTransport<S, T, U>
|
||||||
where
|
where
|
||||||
S: Service<Request = Request<U>, Response = Response<U>>,
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
U: Encoder + Decoder,
|
U: Encoder + Decoder + Unpin,
|
||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
service: S,
|
service: S,
|
||||||
state: TransportState<S, U>,
|
state: TransportState<S, U>,
|
||||||
framed: Framed<T, U>,
|
framed: Framed<T, U>,
|
||||||
rx: Option<mpsc::UnboundedReceiver<FramedMessage<<U as Encoder>::Item>>>,
|
rx: Option<mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>>,
|
||||||
inner: Cell<FramedTransportInner<<U as Encoder>::Item, S::Error>>,
|
inner: Cell<FramedTransportInner<<U as Encoder>::Item, S::Error>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,7 +106,7 @@ enum TransportState<S: Service, U: Encoder + Decoder> {
|
|||||||
|
|
||||||
struct FramedTransportInner<I, E> {
|
struct FramedTransportInner<I, E> {
|
||||||
buf: VecDeque<Result<I, E>>,
|
buf: VecDeque<Result<I, E>>,
|
||||||
task: AtomicTask,
|
task: LocalWaker,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, T, U> FramedTransport<S, T, U>
|
impl<S, T, U> FramedTransport<S, T, U>
|
||||||
@ -105,130 +114,8 @@ where
|
|||||||
S: Service<Request = Request<U>, Response = Response<U>>,
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
U: Decoder + Encoder,
|
U: Decoder + Encoder + Unpin,
|
||||||
<U as Encoder>::Item: 'static,
|
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
|
||||||
{
|
|
||||||
fn poll_read(&mut self) -> bool {
|
|
||||||
loop {
|
|
||||||
match self.service.poll_ready() {
|
|
||||||
Ok(Async::Ready(_)) => {
|
|
||||||
let item = match self.framed.poll() {
|
|
||||||
Ok(Async::Ready(Some(el))) => el,
|
|
||||||
Err(err) => {
|
|
||||||
self.state =
|
|
||||||
TransportState::FramedError(FramedTransportError::Decoder(err));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
Ok(Async::NotReady) => return false,
|
|
||||||
Ok(Async::Ready(None)) => {
|
|
||||||
self.state = TransportState::Stopping;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut cell = self.inner.clone();
|
|
||||||
tokio_current_thread::spawn(self.service.call(item).then(move |item| {
|
|
||||||
let inner = cell.get_mut();
|
|
||||||
inner.buf.push_back(item);
|
|
||||||
inner.task.notify();
|
|
||||||
Ok(())
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
Ok(Async::NotReady) => return false,
|
|
||||||
Err(err) => {
|
|
||||||
self.state = TransportState::Error(FramedTransportError::Service(err));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// write to framed object
|
|
||||||
fn poll_write(&mut self) -> bool {
|
|
||||||
let inner = self.inner.get_mut();
|
|
||||||
let mut rx_done = self.rx.is_none();
|
|
||||||
let mut buf_empty = inner.buf.is_empty();
|
|
||||||
loop {
|
|
||||||
while !self.framed.is_write_buf_full() {
|
|
||||||
if !buf_empty {
|
|
||||||
match inner.buf.pop_front().unwrap() {
|
|
||||||
Ok(msg) => {
|
|
||||||
if let Err(err) = self.framed.force_send(msg) {
|
|
||||||
self.state = TransportState::FramedError(
|
|
||||||
FramedTransportError::Encoder(err),
|
|
||||||
);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
buf_empty = inner.buf.is_empty();
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
self.state =
|
|
||||||
TransportState::Error(FramedTransportError::Service(err));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !rx_done && self.rx.is_some() {
|
|
||||||
match self.rx.as_mut().unwrap().poll() {
|
|
||||||
Ok(Async::Ready(Some(FramedMessage::Message(msg)))) => {
|
|
||||||
if let Err(err) = self.framed.force_send(msg) {
|
|
||||||
self.state = TransportState::FramedError(
|
|
||||||
FramedTransportError::Encoder(err),
|
|
||||||
);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(Async::Ready(Some(FramedMessage::Close))) => {
|
|
||||||
self.state = TransportState::FlushAndStop;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
Ok(Async::Ready(None)) => {
|
|
||||||
rx_done = true;
|
|
||||||
let _ = self.rx.take();
|
|
||||||
}
|
|
||||||
Ok(Async::NotReady) => rx_done = true,
|
|
||||||
Err(_e) => {
|
|
||||||
rx_done = true;
|
|
||||||
let _ = self.rx.take();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if rx_done && buf_empty {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !self.framed.is_write_buf_empty() {
|
|
||||||
match self.framed.poll_complete() {
|
|
||||||
Ok(Async::NotReady) => break,
|
|
||||||
Err(err) => {
|
|
||||||
debug!("Error sending data: {:?}", err);
|
|
||||||
self.state =
|
|
||||||
TransportState::FramedError(FramedTransportError::Encoder(err));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
Ok(Async::Ready(_)) => (),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S, T, U> FramedTransport<S, T, U>
|
|
||||||
where
|
|
||||||
S: Service<Request = Request<U>, Response = Response<U>>,
|
|
||||||
S::Error: 'static,
|
|
||||||
S::Future: 'static,
|
|
||||||
T: AsyncRead + AsyncWrite,
|
|
||||||
U: Decoder + Encoder,
|
|
||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
@ -240,7 +127,7 @@ where
|
|||||||
state: TransportState::Processing,
|
state: TransportState::Processing,
|
||||||
inner: Cell::new(FramedTransportInner {
|
inner: Cell::new(FramedTransportInner {
|
||||||
buf: VecDeque::new(),
|
buf: VecDeque::new(),
|
||||||
task: AtomicTask::new(),
|
task: LocalWaker::new(),
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -248,7 +135,7 @@ where
|
|||||||
/// Get Sender
|
/// Get Sender
|
||||||
pub fn set_receiver(
|
pub fn set_receiver(
|
||||||
mut self,
|
mut self,
|
||||||
rx: mpsc::UnboundedReceiver<FramedMessage<<U as Encoder>::Item>>,
|
rx: mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.rx = Some(rx);
|
self.rx = Some(rx);
|
||||||
self
|
self
|
||||||
@ -283,51 +170,215 @@ where
|
|||||||
S: Service<Request = Request<U>, Response = Response<U>>,
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
U: Decoder + Encoder,
|
U: Decoder + Encoder + Unpin,
|
||||||
<U as Encoder>::Item: 'static,
|
<U as Encoder>::Item: 'static,
|
||||||
<U as Encoder>::Error: std::fmt::Debug,
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
{
|
{
|
||||||
type Item = ();
|
type Output = Result<(), FramedTransportError<S::Error, U>>;
|
||||||
type Error = FramedTransportError<S::Error, U>;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||||
self.inner.get_ref().task.register();
|
self.inner.get_ref().task.register(cx.waker());
|
||||||
|
|
||||||
match mem::replace(&mut self.state, TransportState::Processing) {
|
let this = self.project();
|
||||||
|
poll(
|
||||||
|
cx,
|
||||||
|
this.service,
|
||||||
|
this.state,
|
||||||
|
this.framed,
|
||||||
|
this.rx,
|
||||||
|
this.inner,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll<S, T, U>(
|
||||||
|
cx: &mut Context,
|
||||||
|
srv: &mut S,
|
||||||
|
state: &mut TransportState<S, U>,
|
||||||
|
framed: &mut Framed<T, U>,
|
||||||
|
rx: &mut Rx<U>,
|
||||||
|
inner: &mut Inner<S, U>,
|
||||||
|
) -> Poll<Result<(), FramedTransportError<S::Error, U>>>
|
||||||
|
where
|
||||||
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
||||||
|
S::Error: 'static,
|
||||||
|
S::Future: 'static,
|
||||||
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
|
U: Decoder + Encoder + Unpin,
|
||||||
|
<U as Encoder>::Item: 'static,
|
||||||
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
match mem::replace(state, TransportState::Processing) {
|
||||||
TransportState::Processing => {
|
TransportState::Processing => {
|
||||||
if self.poll_read() || self.poll_write() {
|
if poll_read(cx, srv, state, framed, inner)
|
||||||
self.poll()
|
|| poll_write(cx, state, framed, rx, inner)
|
||||||
|
{
|
||||||
|
poll(cx, srv, state, framed, rx, inner)
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TransportState::Error(err) => {
|
TransportState::Error(err) => {
|
||||||
if self.framed.is_write_buf_empty()
|
let is_empty = framed.is_write_buf_empty();
|
||||||
|| (self.poll_write() || self.framed.is_write_buf_empty())
|
if is_empty || (poll_write(cx, state, framed, rx, inner) || is_empty) {
|
||||||
{
|
Poll::Ready(Err(err))
|
||||||
Err(err)
|
|
||||||
} else {
|
} else {
|
||||||
self.state = TransportState::Error(err);
|
*state = TransportState::Error(err);
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TransportState::FlushAndStop => {
|
TransportState::FlushAndStop => {
|
||||||
if !self.framed.is_write_buf_empty() {
|
if !framed.is_write_buf_empty() {
|
||||||
match self.framed.poll_complete() {
|
match Pin::new(framed).poll_flush(cx) {
|
||||||
Err(err) => {
|
Poll::Ready(Err(err)) => {
|
||||||
debug!("Error sending data: {:?}", err);
|
debug!("Error sending data: {:?}", err);
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
Ok(Async::NotReady) => Ok(Async::NotReady),
|
Poll::Pending => Poll::Pending,
|
||||||
Ok(Async::Ready(_)) => Ok(Async::Ready(())),
|
Poll::Ready(Ok(_)) => Poll::Ready(Ok(())),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TransportState::FramedError(err) => Err(err),
|
TransportState::FramedError(err) => Poll::Ready(Err(err)),
|
||||||
TransportState::Stopping => Ok(Async::Ready(())),
|
TransportState::Stopping => Poll::Ready(Ok(())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_read<S, T, U>(
|
||||||
|
cx: &mut Context,
|
||||||
|
srv: &mut S,
|
||||||
|
state: &mut TransportState<S, U>,
|
||||||
|
framed: &mut Framed<T, U>,
|
||||||
|
inner: &mut Inner<S, U>,
|
||||||
|
) -> bool
|
||||||
|
where
|
||||||
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
||||||
|
S::Error: 'static,
|
||||||
|
S::Future: 'static,
|
||||||
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
|
U: Decoder + Encoder + Unpin,
|
||||||
|
<U as Encoder>::Item: 'static,
|
||||||
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
loop {
|
||||||
|
match srv.poll_ready(cx) {
|
||||||
|
Poll::Ready(Ok(_)) => {
|
||||||
|
let item = match framed.next_item(cx) {
|
||||||
|
Poll::Ready(Some(Ok(el))) => el,
|
||||||
|
Poll::Ready(Some(Err(err))) => {
|
||||||
|
*state =
|
||||||
|
TransportState::FramedError(FramedTransportError::Decoder(err));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Poll::Pending => return false,
|
||||||
|
Poll::Ready(None) => {
|
||||||
|
*state = TransportState::Stopping;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut cell = inner.clone();
|
||||||
|
let fut = srv.call(item).then(move |item| {
|
||||||
|
let inner = cell.get_mut();
|
||||||
|
inner.buf.push_back(item);
|
||||||
|
inner.task.wake();
|
||||||
|
ready(())
|
||||||
|
});
|
||||||
|
tokio_executor::current_thread::spawn(fut);
|
||||||
|
}
|
||||||
|
Poll::Pending => return false,
|
||||||
|
Poll::Ready(Err(err)) => {
|
||||||
|
*state = TransportState::Error(FramedTransportError::Service(err));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// write to framed object
|
||||||
|
fn poll_write<S, T, U>(
|
||||||
|
cx: &mut Context,
|
||||||
|
state: &mut TransportState<S, U>,
|
||||||
|
framed: &mut Framed<T, U>,
|
||||||
|
rx: &mut Rx<U>,
|
||||||
|
inner: &mut Inner<S, U>,
|
||||||
|
) -> bool
|
||||||
|
where
|
||||||
|
S: Service<Request = Request<U>, Response = Response<U>>,
|
||||||
|
S::Error: 'static,
|
||||||
|
S::Future: 'static,
|
||||||
|
T: AsyncRead + AsyncWrite + Unpin,
|
||||||
|
U: Decoder + Encoder + Unpin,
|
||||||
|
<U as Encoder>::Item: 'static,
|
||||||
|
<U as Encoder>::Error: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
// let this = self.project();
|
||||||
|
|
||||||
|
let inner = inner.get_mut();
|
||||||
|
let mut rx_done = rx.is_none();
|
||||||
|
let mut buf_empty = inner.buf.is_empty();
|
||||||
|
loop {
|
||||||
|
while !framed.is_write_buf_full() {
|
||||||
|
if !buf_empty {
|
||||||
|
match inner.buf.pop_front().unwrap() {
|
||||||
|
Ok(msg) => {
|
||||||
|
if let Err(err) = framed.force_send(msg) {
|
||||||
|
*state =
|
||||||
|
TransportState::FramedError(FramedTransportError::Encoder(err));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
buf_empty = inner.buf.is_empty();
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
*state = TransportState::Error(FramedTransportError::Service(err));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !rx_done && rx.is_some() {
|
||||||
|
match Pin::new(rx.as_mut().unwrap()).poll_next(cx) {
|
||||||
|
Poll::Ready(Some(FramedMessage::Message(msg))) => {
|
||||||
|
if let Err(err) = framed.force_send(msg) {
|
||||||
|
*state =
|
||||||
|
TransportState::FramedError(FramedTransportError::Encoder(err));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Poll::Ready(Some(FramedMessage::Close)) => {
|
||||||
|
*state = TransportState::FlushAndStop;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Poll::Ready(None) => {
|
||||||
|
rx_done = true;
|
||||||
|
let _ = rx.take();
|
||||||
|
}
|
||||||
|
Poll::Pending => rx_done = true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if rx_done && buf_empty {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !framed.is_write_buf_empty() {
|
||||||
|
match framed.flush(cx) {
|
||||||
|
Poll::Pending => break,
|
||||||
|
Poll::Ready(Err(err)) => {
|
||||||
|
debug!("Error sending data: {:?}", err);
|
||||||
|
*state = TransportState::FramedError(FramedTransportError::Encoder(err));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Poll::Ready(Ok(_)) => (),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_service::{IntoService, Service, Transform};
|
use actix_service::{IntoService, Service, Transform};
|
||||||
use futures::future::{ok, FutureResult};
|
use futures::future::{ok, Ready};
|
||||||
use futures::{Async, Future, Poll};
|
use pin_project::pin_project;
|
||||||
|
|
||||||
use super::counter::{Counter, CounterGuard};
|
use super::counter::{Counter, CounterGuard};
|
||||||
|
|
||||||
@ -32,7 +35,7 @@ impl<S: Service> Transform<S> for InFlight {
|
|||||||
type Error = S::Error;
|
type Error = S::Error;
|
||||||
type InitError = Infallible;
|
type InitError = Infallible;
|
||||||
type Transform = InFlightService<S>;
|
type Transform = InFlightService<S>;
|
||||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
fn new_transform(&self, service: S) -> Self::Future {
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
ok(InFlightService::new(self.max_inflight, service))
|
ok(InFlightService::new(self.max_inflight, service))
|
||||||
@ -68,14 +71,14 @@ where
|
|||||||
type Error = T::Error;
|
type Error = T::Error;
|
||||||
type Future = InFlightServiceResponse<T>;
|
type Future = InFlightServiceResponse<T>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
if let Async::NotReady = self.service.poll_ready()? {
|
if let Poll::Pending = self.service.poll_ready(cx)? {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
} else if !self.count.available() {
|
} else if !self.count.available(cx) {
|
||||||
log::trace!("InFlight limit exceeded");
|
log::trace!("InFlight limit exceeded");
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,31 +90,31 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pin_project]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub struct InFlightServiceResponse<T: Service> {
|
pub struct InFlightServiceResponse<T: Service> {
|
||||||
|
#[pin]
|
||||||
fut: T::Future,
|
fut: T::Future,
|
||||||
_guard: CounterGuard,
|
_guard: CounterGuard,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Service> Future for InFlightServiceResponse<T> {
|
impl<T: Service> Future for InFlightServiceResponse<T> {
|
||||||
type Item = T::Response;
|
type Output = Result<T::Response, T::Error>;
|
||||||
type Error = T::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||||
self.fut.poll()
|
self.project().fut.poll(cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use futures::future::lazy;
|
|
||||||
use futures::{Async, Poll};
|
|
||||||
|
|
||||||
|
use std::task::{Context, Poll};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use actix_service::blank::{Blank, BlankNewService};
|
use actix_service::{apply, factory_fn, Service, ServiceFactory};
|
||||||
use actix_service::{NewService, Service, ServiceExt};
|
use futures::future::{lazy, ok, FutureExt, LocalBoxFuture};
|
||||||
|
|
||||||
struct SleepService(Duration);
|
struct SleepService(Duration);
|
||||||
|
|
||||||
@ -119,57 +122,49 @@ mod tests {
|
|||||||
type Request = ();
|
type Request = ();
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = Box<dyn Future<Item = (), Error = ()>>;
|
type Future = LocalBoxFuture<'static, Result<(), ()>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, _: ()) -> Self::Future {
|
fn call(&mut self, _: ()) -> Self::Future {
|
||||||
Box::new(tokio_timer::sleep(self.0).map_err(|_| ()))
|
tokio_timer::delay_for(self.0)
|
||||||
|
.then(|_| ok::<_, ()>(()))
|
||||||
|
.boxed_local()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_transform() {
|
fn test_transform() {
|
||||||
let wait_time = Duration::from_millis(50);
|
let wait_time = Duration::from_millis(50);
|
||||||
let _ = actix_rt::System::new("test").block_on(lazy(|| {
|
let _ = actix_rt::System::new("test").block_on(async {
|
||||||
let mut srv =
|
let mut srv = InFlightService::new(1, SleepService(wait_time));
|
||||||
Blank::new().and_then(InFlightService::new(1, SleepService(wait_time)));
|
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||||
assert_eq!(srv.poll_ready(), Ok(Async::Ready(())));
|
|
||||||
|
|
||||||
let mut res = srv.call(());
|
let res = srv.call(());
|
||||||
let _ = res.poll();
|
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Pending);
|
||||||
assert_eq!(srv.poll_ready(), Ok(Async::NotReady));
|
|
||||||
|
|
||||||
drop(res);
|
let _ = res.await;
|
||||||
assert_eq!(srv.poll_ready(), Ok(Async::Ready(())));
|
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||||
|
});
|
||||||
Ok::<_, ()>(())
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_newtransform() {
|
fn test_newtransform() {
|
||||||
let wait_time = Duration::from_millis(50);
|
let wait_time = Duration::from_millis(50);
|
||||||
let _ = actix_rt::System::new("test").block_on(lazy(|| {
|
|
||||||
let srv =
|
|
||||||
BlankNewService::new().apply(InFlight::new(1), || Ok(SleepService(wait_time)));
|
|
||||||
|
|
||||||
if let Async::Ready(mut srv) = srv.new_service(&()).poll().unwrap() {
|
actix_rt::System::new("test").block_on(async {
|
||||||
assert_eq!(srv.poll_ready(), Ok(Async::Ready(())));
|
let srv = apply(InFlight::new(1), factory_fn(|| ok(SleepService(wait_time))));
|
||||||
|
|
||||||
let mut res = srv.call(());
|
let mut srv = srv.new_service(&()).await.unwrap();
|
||||||
let _ = res.poll();
|
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||||
assert_eq!(srv.poll_ready(), Ok(Async::NotReady));
|
|
||||||
|
|
||||||
drop(res);
|
let res = srv.call(());
|
||||||
assert_eq!(srv.poll_ready(), Ok(Async::Ready(())));
|
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Pending);
|
||||||
} else {
|
|
||||||
panic!()
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok::<_, ()>(())
|
let _ = res.await;
|
||||||
}));
|
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use actix_service::{NewService, Service};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::future::{ok, FutureResult};
|
use futures::future::{ok, Ready};
|
||||||
use futures::{Async, Future, Poll};
|
use tokio_timer::{delay, Delay};
|
||||||
use tokio_timer::Delay;
|
|
||||||
|
|
||||||
use super::time::{LowResTime, LowResTimeService};
|
use super::time::{LowResTime, LowResTimeService};
|
||||||
|
|
||||||
@ -44,7 +46,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R, E, F> NewService for KeepAlive<R, E, F>
|
impl<R, E, F> ServiceFactory for KeepAlive<R, E, F>
|
||||||
where
|
where
|
||||||
F: Fn() -> E + Clone,
|
F: Fn() -> E + Clone,
|
||||||
{
|
{
|
||||||
@ -54,7 +56,7 @@ where
|
|||||||
type InitError = Infallible;
|
type InitError = Infallible;
|
||||||
type Config = ();
|
type Config = ();
|
||||||
type Service = KeepAliveService<R, E, F>;
|
type Service = KeepAliveService<R, E, F>;
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, _: &()) -> Self::Future {
|
fn new_service(&self, _: &()) -> Self::Future {
|
||||||
ok(KeepAliveService::new(
|
ok(KeepAliveService::new(
|
||||||
@ -85,7 +87,7 @@ where
|
|||||||
ka,
|
ka,
|
||||||
time,
|
time,
|
||||||
expire,
|
expire,
|
||||||
delay: Delay::new(expire),
|
delay: delay(expire),
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -98,22 +100,21 @@ where
|
|||||||
type Request = R;
|
type Request = R;
|
||||||
type Response = R;
|
type Response = R;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
type Future = FutureResult<R, E>;
|
type Future = Ready<Result<R, E>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
match self.delay.poll() {
|
match Pin::new(&mut self.delay).poll(cx) {
|
||||||
Ok(Async::Ready(_)) => {
|
Poll::Ready(_) => {
|
||||||
let now = self.time.now();
|
let now = self.time.now();
|
||||||
if self.expire <= now {
|
if self.expire <= now {
|
||||||
Err((self.f)())
|
Poll::Ready(Err((self.f)()))
|
||||||
} else {
|
} else {
|
||||||
self.delay.reset(self.expire);
|
self.delay.reset(self.expire);
|
||||||
let _ = self.delay.poll();
|
let _ = Pin::new(&mut self.delay).poll(cx);
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Async::NotReady) => Ok(Async::Ready(())),
|
Poll::Pending => Poll::Ready(Ok(())),
|
||||||
Err(_e) => panic!(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,9 @@ pub mod either;
|
|||||||
pub mod framed;
|
pub mod framed;
|
||||||
pub mod inflight;
|
pub mod inflight;
|
||||||
pub mod keepalive;
|
pub mod keepalive;
|
||||||
|
pub mod mpsc;
|
||||||
|
pub mod oneshot;
|
||||||
pub mod order;
|
pub mod order;
|
||||||
pub mod stream;
|
pub mod task;
|
||||||
pub mod time;
|
pub mod time;
|
||||||
pub mod timeout;
|
pub mod timeout;
|
||||||
|
203
actix-utils/src/mpsc.rs
Normal file
203
actix-utils/src/mpsc.rs
Normal file
@ -0,0 +1,203 @@
|
|||||||
|
//! A multi-producer, single-consumer, futures-aware, FIFO queue with back
|
||||||
|
//! pressure, for use communicating between tasks on the same thread.
|
||||||
|
//!
|
||||||
|
//! These queues are the same as those in `futures::sync`, except they're not
|
||||||
|
//! intended to be sent across threads.
|
||||||
|
|
||||||
|
use std::any::Any;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::rc::{Rc, Weak};
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
use std::{fmt, mem};
|
||||||
|
|
||||||
|
use futures::{Sink, Stream};
|
||||||
|
|
||||||
|
use crate::task::LocalWaker;
|
||||||
|
|
||||||
|
/// Creates a unbounded in-memory channel with buffered storage.
|
||||||
|
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
|
||||||
|
let shared = Rc::new(RefCell::new(Shared {
|
||||||
|
buffer: VecDeque::new(),
|
||||||
|
blocked_recv: LocalWaker::new(),
|
||||||
|
}));
|
||||||
|
let sender = Sender {
|
||||||
|
shared: Rc::downgrade(&shared),
|
||||||
|
};
|
||||||
|
let receiver = Receiver {
|
||||||
|
state: State::Open(shared),
|
||||||
|
};
|
||||||
|
(sender, receiver)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Shared<T> {
|
||||||
|
buffer: VecDeque<T>,
|
||||||
|
blocked_recv: LocalWaker,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The transmission end of a channel.
|
||||||
|
///
|
||||||
|
/// This is created by the `channel` function.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Sender<T> {
|
||||||
|
shared: Weak<RefCell<Shared<T>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Sender<T> {
|
||||||
|
/// Sends the provided message along this channel.
|
||||||
|
pub fn send(&self, item: T) -> Result<(), SendError<T>> {
|
||||||
|
let shared = match self.shared.upgrade() {
|
||||||
|
Some(shared) => shared,
|
||||||
|
None => return Err(SendError(item)), // receiver was dropped
|
||||||
|
};
|
||||||
|
let mut shared = shared.borrow_mut();
|
||||||
|
|
||||||
|
shared.buffer.push_back(item);
|
||||||
|
shared.blocked_recv.wake();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Clone for Sender<T> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Sender {
|
||||||
|
shared: self.shared.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Sink<T> for Sender<T> {
|
||||||
|
type Error = SendError<T>;
|
||||||
|
|
||||||
|
fn poll_ready(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
|
Poll::Ready(Ok(()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start_send(self: Pin<&mut Self>, item: T) -> Result<(), SendError<T>> {
|
||||||
|
self.send(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_flush(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), SendError<T>>> {
|
||||||
|
Poll::Ready(Ok(()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn poll_close(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
|
Poll::Ready(Ok(()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Drop for Sender<T> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let shared = match self.shared.upgrade() {
|
||||||
|
Some(shared) => shared,
|
||||||
|
None => return,
|
||||||
|
};
|
||||||
|
// The number of existing `Weak` indicates if we are possibly the last
|
||||||
|
// `Sender`. If we are the last, we possibly must notify a blocked
|
||||||
|
// `Receiver`. `self.shared` is always one of the `Weak` to this shared
|
||||||
|
// data. Therefore the smallest possible Rc::weak_count(&shared) is 1.
|
||||||
|
if Rc::weak_count(&shared) == 1 {
|
||||||
|
// Wake up receiver as its stream has ended
|
||||||
|
shared.borrow_mut().blocked_recv.wake();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The receiving end of a channel which implements the `Stream` trait.
|
||||||
|
///
|
||||||
|
/// This is created by the `channel` function.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Receiver<T> {
|
||||||
|
state: State<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Unpin for Receiver<T> {}
|
||||||
|
|
||||||
|
/// Possible states of a receiver. We're either Open (can receive more messages)
|
||||||
|
/// or we're closed with a list of messages we have left to receive.
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum State<T> {
|
||||||
|
Open(Rc<RefCell<Shared<T>>>),
|
||||||
|
Closed(VecDeque<T>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Receiver<T> {
|
||||||
|
/// Closes the receiving half
|
||||||
|
///
|
||||||
|
/// This prevents any further messages from being sent on the channel while
|
||||||
|
/// still enabling the receiver to drain messages that are buffered.
|
||||||
|
pub fn close(&mut self) {
|
||||||
|
let items = match self.state {
|
||||||
|
State::Open(ref state) => {
|
||||||
|
let mut state = state.borrow_mut();
|
||||||
|
let items = mem::replace(&mut state.buffer, VecDeque::new());
|
||||||
|
items
|
||||||
|
}
|
||||||
|
State::Closed(_) => return,
|
||||||
|
};
|
||||||
|
self.state = State::Closed(items);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Stream for Receiver<T> {
|
||||||
|
type Item = T;
|
||||||
|
|
||||||
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
||||||
|
let me = match self.state {
|
||||||
|
State::Open(ref mut me) => me,
|
||||||
|
State::Closed(ref mut items) => return Poll::Ready(items.pop_front()),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(shared) = Rc::get_mut(me) {
|
||||||
|
// All senders have been dropped, so drain the buffer and end the
|
||||||
|
// stream.
|
||||||
|
return Poll::Ready(shared.borrow_mut().buffer.pop_front());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut shared = me.borrow_mut();
|
||||||
|
if let Some(msg) = shared.buffer.pop_front() {
|
||||||
|
Poll::Ready(Some(msg))
|
||||||
|
} else {
|
||||||
|
shared.blocked_recv.register(cx.waker());
|
||||||
|
Poll::Pending
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Drop for Receiver<T> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Error type for sending, used when the receiving end of a channel is
|
||||||
|
/// dropped
|
||||||
|
pub struct SendError<T>(T);
|
||||||
|
|
||||||
|
impl<T> fmt::Debug for SendError<T> {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
fmt.debug_tuple("SendError").field(&"...").finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> fmt::Display for SendError<T> {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(fmt, "send failed because receiver is gone")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Any> Error for SendError<T> {
|
||||||
|
fn description(&self) -> &str {
|
||||||
|
"send failed because receiver is gone"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> SendError<T> {
|
||||||
|
/// Returns the message that was attempted to be sent but failed.
|
||||||
|
pub fn into_inner(self) -> T {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
}
|
209
actix-utils/src/oneshot.rs
Normal file
209
actix-utils/src/oneshot.rs
Normal file
@ -0,0 +1,209 @@
|
|||||||
|
//! A one-shot, futures-aware channel
|
||||||
|
//!
|
||||||
|
//! This channel is similar to that in `sync::oneshot` but cannot be sent across
|
||||||
|
//! threads.
|
||||||
|
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::rc::{Rc, Weak};
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
pub use futures::channel::oneshot::Canceled;
|
||||||
|
|
||||||
|
use crate::task::LocalWaker;
|
||||||
|
|
||||||
|
/// Creates a new futures-aware, one-shot channel.
|
||||||
|
///
|
||||||
|
/// This function is the same as `sync::oneshot::channel` except that the
|
||||||
|
/// returned values cannot be sent across threads.
|
||||||
|
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
|
||||||
|
let inner = Rc::new(RefCell::new(Inner {
|
||||||
|
value: None,
|
||||||
|
tx_task: LocalWaker::new(),
|
||||||
|
rx_task: LocalWaker::new(),
|
||||||
|
}));
|
||||||
|
let tx = Sender {
|
||||||
|
inner: Rc::downgrade(&inner),
|
||||||
|
};
|
||||||
|
let rx = Receiver {
|
||||||
|
state: State::Open(inner),
|
||||||
|
};
|
||||||
|
(tx, rx)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents the completion half of a oneshot through which the result of a
|
||||||
|
/// computation is signaled.
|
||||||
|
///
|
||||||
|
/// This is created by the `unsync::oneshot::channel` function and is equivalent
|
||||||
|
/// in functionality to `sync::oneshot::Sender` except that it cannot be sent
|
||||||
|
/// across threads.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Sender<T> {
|
||||||
|
inner: Weak<RefCell<Inner<T>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A future representing the completion of a computation happening elsewhere in
|
||||||
|
/// memory.
|
||||||
|
///
|
||||||
|
/// This is created by the `unsync::oneshot::channel` function and is equivalent
|
||||||
|
/// in functionality to `sync::oneshot::Receiver` except that it cannot be sent
|
||||||
|
/// across threads.
|
||||||
|
#[derive(Debug)]
|
||||||
|
#[must_use = "futures do nothing unless polled"]
|
||||||
|
pub struct Receiver<T> {
|
||||||
|
state: State<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// The channels do not ever project Pin to the inner T
|
||||||
|
impl<T> Unpin for Receiver<T> {}
|
||||||
|
impl<T> Unpin for Sender<T> {}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum State<T> {
|
||||||
|
Open(Rc<RefCell<Inner<T>>>),
|
||||||
|
Closed(Option<T>),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Inner<T> {
|
||||||
|
value: Option<T>,
|
||||||
|
tx_task: LocalWaker,
|
||||||
|
rx_task: LocalWaker,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Sender<T> {
|
||||||
|
/// Completes this oneshot with a successful result.
|
||||||
|
///
|
||||||
|
/// This function will consume `self` and indicate to the other end, the
|
||||||
|
/// `Receiver`, that the error provided is the result of the computation this
|
||||||
|
/// represents.
|
||||||
|
///
|
||||||
|
/// If the value is successfully enqueued for the remote end to receive,
|
||||||
|
/// then `Ok(())` is returned. If the receiving end was deallocated before
|
||||||
|
/// this function was called, however, then `Err` is returned with the value
|
||||||
|
/// provided.
|
||||||
|
pub fn send(self, val: T) -> Result<(), T> {
|
||||||
|
if let Some(inner) = self.inner.upgrade() {
|
||||||
|
inner.borrow_mut().value = Some(val);
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Polls this `Sender` half to detect whether the `Receiver` this has
|
||||||
|
/// paired with has gone away.
|
||||||
|
///
|
||||||
|
/// This function can be used to learn about when the `Receiver` (consumer)
|
||||||
|
/// half has gone away and nothing will be able to receive a message sent
|
||||||
|
/// from `complete`.
|
||||||
|
///
|
||||||
|
/// Like `Future::poll`, this function will panic if it's not called from
|
||||||
|
/// within the context of a task. In other words, this should only ever be
|
||||||
|
/// called from inside another future.
|
||||||
|
///
|
||||||
|
/// If `Ready` is returned then it means that the `Receiver` has disappeared
|
||||||
|
/// and the result this `Sender` would otherwise produce should no longer
|
||||||
|
/// be produced.
|
||||||
|
///
|
||||||
|
/// If `NotReady` is returned then the `Receiver` is still alive and may be
|
||||||
|
/// able to receive a message if sent. The current task, however, is
|
||||||
|
/// scheduled to receive a notification if the corresponding `Receiver` goes
|
||||||
|
/// away.
|
||||||
|
pub fn poll_canceled(&mut self, cx: &mut Context) -> Poll<()> {
|
||||||
|
match self.inner.upgrade() {
|
||||||
|
Some(inner) => {
|
||||||
|
inner.borrow_mut().tx_task.register(cx.waker());
|
||||||
|
Poll::Pending
|
||||||
|
}
|
||||||
|
None => Poll::Ready(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Tests to see whether this `Sender`'s corresponding `Receiver`
|
||||||
|
/// has gone away.
|
||||||
|
///
|
||||||
|
/// This function can be used to learn about when the `Receiver` (consumer)
|
||||||
|
/// half has gone away and nothing will be able to receive a message sent
|
||||||
|
/// from `send`.
|
||||||
|
///
|
||||||
|
/// Note that this function is intended to *not* be used in the context of a
|
||||||
|
/// future. If you're implementing a future you probably want to call the
|
||||||
|
/// `poll_cancel` function which will block the current task if the
|
||||||
|
/// cancellation hasn't happened yet. This can be useful when working on a
|
||||||
|
/// non-futures related thread, though, which would otherwise panic if
|
||||||
|
/// `poll_cancel` were called.
|
||||||
|
pub fn is_canceled(&self) -> bool {
|
||||||
|
!self.inner.upgrade().is_some()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Drop for Sender<T> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let inner = match self.inner.upgrade() {
|
||||||
|
Some(inner) => inner,
|
||||||
|
None => return,
|
||||||
|
};
|
||||||
|
inner.borrow().rx_task.wake();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Receiver<T> {
|
||||||
|
/// Gracefully close this receiver, preventing sending any future messages.
|
||||||
|
///
|
||||||
|
/// Any `send` operation which happens after this method returns is
|
||||||
|
/// guaranteed to fail. Once this method is called the normal `poll` method
|
||||||
|
/// can be used to determine whether a message was actually sent or not. If
|
||||||
|
/// `Canceled` is returned from `poll` then no message was sent.
|
||||||
|
pub fn close(&mut self) {
|
||||||
|
match self.state {
|
||||||
|
State::Open(ref inner) => {
|
||||||
|
let mut inner = inner.borrow_mut();
|
||||||
|
inner.tx_task.wake();
|
||||||
|
let value = inner.value.take();
|
||||||
|
drop(inner);
|
||||||
|
|
||||||
|
self.state = State::Closed(value);
|
||||||
|
}
|
||||||
|
State::Closed(_) => return,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Future for Receiver<T> {
|
||||||
|
type Output = Result<T, Canceled>;
|
||||||
|
|
||||||
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
let this = self.get_mut();
|
||||||
|
|
||||||
|
let inner = match this.state {
|
||||||
|
State::Open(ref mut inner) => inner,
|
||||||
|
State::Closed(ref mut item) => match item.take() {
|
||||||
|
Some(item) => return Poll::Ready(Ok(item.into())),
|
||||||
|
None => return Poll::Ready(Err(Canceled)),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// If we've got a value, then skip the logic below as we're done.
|
||||||
|
if let Some(val) = inner.borrow_mut().value.take() {
|
||||||
|
return Poll::Ready(Ok(val));
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we can get mutable access, then the sender has gone away. We
|
||||||
|
// didn't see a value above, so we're canceled. Otherwise we park
|
||||||
|
// our task and wait for a value to come in.
|
||||||
|
if Rc::get_mut(inner).is_some() {
|
||||||
|
Poll::Ready(Err(Canceled))
|
||||||
|
} else {
|
||||||
|
inner.borrow().rx_task.register(cx.waker());
|
||||||
|
Poll::Pending
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Drop for Receiver<T> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.close();
|
||||||
|
}
|
||||||
|
}
|
@ -1,14 +1,17 @@
|
|||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_service::{IntoService, Service, Transform};
|
use actix_service::{IntoService, Service, Transform};
|
||||||
use futures::future::{ok, FutureResult};
|
use futures::future::{ok, ready, FutureExt, Ready};
|
||||||
use futures::task::AtomicTask;
|
|
||||||
use futures::unsync::oneshot;
|
use crate::oneshot;
|
||||||
use futures::{Async, Future, Poll};
|
use crate::task::LocalWaker;
|
||||||
|
|
||||||
struct Record<I, E> {
|
struct Record<I, E> {
|
||||||
rx: oneshot::Receiver<Result<I, E>>,
|
rx: oneshot::Receiver<Result<I, E>>,
|
||||||
@ -93,7 +96,7 @@ where
|
|||||||
type Error = InOrderError<S::Error>;
|
type Error = InOrderError<S::Error>;
|
||||||
type InitError = Infallible;
|
type InitError = Infallible;
|
||||||
type Transform = InOrderService<S>;
|
type Transform = InOrderService<S>;
|
||||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
fn new_transform(&self, service: S) -> Self::Future {
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
ok(InOrderService::new(service))
|
ok(InOrderService::new(service))
|
||||||
@ -102,7 +105,7 @@ where
|
|||||||
|
|
||||||
pub struct InOrderService<S: Service> {
|
pub struct InOrderService<S: Service> {
|
||||||
service: S,
|
service: S,
|
||||||
task: Rc<AtomicTask>,
|
task: Rc<LocalWaker>,
|
||||||
acks: VecDeque<Record<S::Response, S::Error>>,
|
acks: VecDeque<Record<S::Response, S::Error>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +123,7 @@ where
|
|||||||
Self {
|
Self {
|
||||||
service: service.into_service(),
|
service: service.into_service(),
|
||||||
acks: VecDeque::new(),
|
acks: VecDeque::new(),
|
||||||
task: Rc::new(AtomicTask::new()),
|
task: Rc::new(LocalWaker::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -137,28 +140,30 @@ where
|
|||||||
type Error = InOrderError<S::Error>;
|
type Error = InOrderError<S::Error>;
|
||||||
type Future = InOrderServiceResponse<S>;
|
type Future = InOrderServiceResponse<S>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
// poll_ready could be called from different task
|
// poll_ready could be called from different task
|
||||||
self.task.register();
|
self.task.register(cx.waker());
|
||||||
|
|
||||||
// check acks
|
// check acks
|
||||||
while !self.acks.is_empty() {
|
while !self.acks.is_empty() {
|
||||||
let rec = self.acks.front_mut().unwrap();
|
let rec = self.acks.front_mut().unwrap();
|
||||||
match rec.rx.poll() {
|
match Pin::new(&mut rec.rx).poll(cx) {
|
||||||
Ok(Async::Ready(res)) => {
|
Poll::Ready(Ok(res)) => {
|
||||||
let rec = self.acks.pop_front().unwrap();
|
let rec = self.acks.pop_front().unwrap();
|
||||||
let _ = rec.tx.send(res);
|
let _ = rec.tx.send(res);
|
||||||
}
|
}
|
||||||
Ok(Async::NotReady) => break,
|
Poll::Pending => break,
|
||||||
Err(oneshot::Canceled) => return Err(InOrderError::Disconnected),
|
Poll::Ready(Err(oneshot::Canceled)) => {
|
||||||
|
return Poll::Ready(Err(InOrderError::Disconnected))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check nested service
|
// check nested service
|
||||||
if let Async::NotReady = self.service.poll_ready().map_err(InOrderError::Service)? {
|
if let Poll::Pending = self.service.poll_ready(cx).map_err(InOrderError::Service)? {
|
||||||
Ok(Async::NotReady)
|
Poll::Pending
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,10 +173,10 @@ where
|
|||||||
self.acks.push_back(Record { rx: rx1, tx: tx2 });
|
self.acks.push_back(Record { rx: rx1, tx: tx2 });
|
||||||
|
|
||||||
let task = self.task.clone();
|
let task = self.task.clone();
|
||||||
tokio_current_thread::spawn(self.service.call(request).then(move |res| {
|
tokio_executor::current_thread::spawn(self.service.call(request).then(move |res| {
|
||||||
task.notify();
|
task.wake();
|
||||||
let _ = tx1.send(res);
|
let _ = tx1.send(res);
|
||||||
Ok(())
|
ready(())
|
||||||
}));
|
}));
|
||||||
|
|
||||||
InOrderServiceResponse { rx: rx2 }
|
InOrderServiceResponse { rx: rx2 }
|
||||||
@ -184,29 +189,28 @@ pub struct InOrderServiceResponse<S: Service> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Service> Future for InOrderServiceResponse<S> {
|
impl<S: Service> Future for InOrderServiceResponse<S> {
|
||||||
type Item = S::Response;
|
type Output = Result<S::Response, InOrderError<S::Error>>;
|
||||||
type Error = InOrderError<S::Error>;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||||
match self.rx.poll() {
|
match Pin::new(&mut self.rx).poll(cx) {
|
||||||
Ok(Async::NotReady) => Ok(Async::NotReady),
|
Poll::Pending => Poll::Pending,
|
||||||
Ok(Async::Ready(Ok(res))) => Ok(Async::Ready(res)),
|
Poll::Ready(Ok(Ok(res))) => Poll::Ready(Ok(res)),
|
||||||
Ok(Async::Ready(Err(e))) => Err(e.into()),
|
Poll::Ready(Ok(Err(e))) => Poll::Ready(Err(e.into())),
|
||||||
Err(oneshot::Canceled) => Err(InOrderError::Disconnected),
|
Poll::Ready(Err(_)) => Poll::Ready(Err(InOrderError::Disconnected)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use futures::future::{lazy, Future};
|
|
||||||
use futures::{stream::futures_unordered, sync::oneshot, Async, Poll, Stream};
|
|
||||||
|
|
||||||
|
use std::task::{Context, Poll};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use actix_service::blank::Blank;
|
use actix_service::Service;
|
||||||
use actix_service::{Service, ServiceExt};
|
use futures::channel::oneshot;
|
||||||
|
use futures::future::{lazy, LocalBoxFuture};
|
||||||
|
|
||||||
struct Srv;
|
struct Srv;
|
||||||
|
|
||||||
@ -214,28 +218,14 @@ mod tests {
|
|||||||
type Request = oneshot::Receiver<usize>;
|
type Request = oneshot::Receiver<usize>;
|
||||||
type Response = usize;
|
type Response = usize;
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = Box<dyn Future<Item = usize, Error = ()>>;
|
type Future = LocalBoxFuture<'static, Result<usize, ()>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: oneshot::Receiver<usize>) -> Self::Future {
|
fn call(&mut self, req: oneshot::Receiver<usize>) -> Self::Future {
|
||||||
Box::new(req.map_err(|_| ()))
|
req.map(|res| res.map_err(|_| ())).boxed_local()
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct SrvPoll<S: Service> {
|
|
||||||
s: S,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: Service> Future for SrvPoll<S> {
|
|
||||||
type Item = ();
|
|
||||||
type Error = ();
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<(), ()> {
|
|
||||||
let _ = self.s.poll_ready();
|
|
||||||
Ok(Async::NotReady)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -251,23 +241,26 @@ mod tests {
|
|||||||
let rx2 = rx2;
|
let rx2 = rx2;
|
||||||
let rx3 = rx3;
|
let rx3 = rx3;
|
||||||
let tx_stop = tx_stop;
|
let tx_stop = tx_stop;
|
||||||
let _ = actix_rt::System::new("test").block_on(lazy(move || {
|
let _ = actix_rt::System::new("test").block_on(async {
|
||||||
let mut srv = Blank::new().and_then(InOrderService::new(Srv));
|
let mut srv = InOrderService::new(Srv);
|
||||||
|
|
||||||
let res1 = srv.call(rx1);
|
let res1 = srv.call(rx1);
|
||||||
let res2 = srv.call(rx2);
|
let res2 = srv.call(rx2);
|
||||||
let res3 = srv.call(rx3);
|
let res3 = srv.call(rx3);
|
||||||
tokio_current_thread::spawn(SrvPoll { s: srv });
|
|
||||||
|
|
||||||
futures_unordered(vec![res1, res2, res3])
|
let _ = lazy(|cx| srv.poll_ready(cx)).await;
|
||||||
.collect()
|
|
||||||
.and_then(move |res: Vec<_>| {
|
// dispatcher do this
|
||||||
assert_eq!(res, vec![1, 2, 3]);
|
tokio_timer::delay_for(Duration::from_millis(100)).await;
|
||||||
|
let _ = lazy(|cx| srv.poll_ready(cx)).await;
|
||||||
|
|
||||||
|
assert_eq!(res1.await.unwrap(), 1);
|
||||||
|
assert_eq!(res2.await.unwrap(), 2);
|
||||||
|
assert_eq!(res3.await.unwrap(), 3);
|
||||||
|
|
||||||
let _ = tx_stop.send(());
|
let _ = tx_stop.send(());
|
||||||
actix_rt::System::current().stop();
|
actix_rt::System::current().stop();
|
||||||
Ok(())
|
});
|
||||||
})
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let _ = tx3.send(3);
|
let _ = tx3.send(3);
|
||||||
@ -275,7 +268,7 @@ mod tests {
|
|||||||
let _ = tx2.send(2);
|
let _ = tx2.send(2);
|
||||||
let _ = tx1.send(1);
|
let _ = tx1.send(1);
|
||||||
|
|
||||||
let _ = rx_stop.wait();
|
let _ = actix_rt::System::new("test").block_on(rx_stop);
|
||||||
let _ = h.join();
|
let _ = h.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,151 +0,0 @@
|
|||||||
use std::marker::PhantomData;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use actix_service::{IntoService, NewService, Service};
|
|
||||||
use futures::unsync::mpsc;
|
|
||||||
use futures::{Async, Future, Poll, Stream};
|
|
||||||
|
|
||||||
type Request<T> = Result<<T as IntoStream>::Item, <T as IntoStream>::Error>;
|
|
||||||
|
|
||||||
pub trait IntoStream {
|
|
||||||
type Item;
|
|
||||||
type Error;
|
|
||||||
type Stream: Stream<Item = Self::Item, Error = Self::Error>;
|
|
||||||
|
|
||||||
fn into_stream(self) -> Self::Stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> IntoStream for T
|
|
||||||
where
|
|
||||||
T: Stream,
|
|
||||||
{
|
|
||||||
type Item = T::Item;
|
|
||||||
type Error = T::Error;
|
|
||||||
type Stream = T;
|
|
||||||
|
|
||||||
fn into_stream(self) -> Self::Stream {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct StreamService<S, T: NewService, E> {
|
|
||||||
factory: Rc<T>,
|
|
||||||
config: T::Config,
|
|
||||||
_t: PhantomData<(S, E)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S, T, E> Service for StreamService<S, T, E>
|
|
||||||
where
|
|
||||||
S: IntoStream + 'static,
|
|
||||||
T: NewService<Request = Request<S>, Response = (), Error = E, InitError = E>,
|
|
||||||
T::Future: 'static,
|
|
||||||
T::Service: 'static,
|
|
||||||
<T::Service as Service>::Future: 'static,
|
|
||||||
{
|
|
||||||
type Request = S;
|
|
||||||
type Response = ();
|
|
||||||
type Error = E;
|
|
||||||
type Future = Box<dyn Future<Item = (), Error = E>>;
|
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call(&mut self, req: S) -> Self::Future {
|
|
||||||
Box::new(
|
|
||||||
self.factory
|
|
||||||
.new_service(&self.config)
|
|
||||||
.and_then(move |srv| StreamDispatcher::new(req, srv)),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct StreamDispatcher<S, T>
|
|
||||||
where
|
|
||||||
S: IntoStream + 'static,
|
|
||||||
T: Service<Request = Request<S>, Response = ()> + 'static,
|
|
||||||
T::Future: 'static,
|
|
||||||
{
|
|
||||||
stream: S,
|
|
||||||
service: T,
|
|
||||||
err_rx: mpsc::UnboundedReceiver<T::Error>,
|
|
||||||
err_tx: mpsc::UnboundedSender<T::Error>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S, T> StreamDispatcher<S, T>
|
|
||||||
where
|
|
||||||
S: Stream,
|
|
||||||
T: Service<Request = Request<S>, Response = ()>,
|
|
||||||
T::Future: 'static,
|
|
||||||
{
|
|
||||||
pub fn new<F1, F2>(stream: F1, service: F2) -> Self
|
|
||||||
where
|
|
||||||
F1: IntoStream<Stream = S, Item = S::Item, Error = S::Error>,
|
|
||||||
F2: IntoService<T>,
|
|
||||||
{
|
|
||||||
let (err_tx, err_rx) = mpsc::unbounded();
|
|
||||||
StreamDispatcher {
|
|
||||||
err_rx,
|
|
||||||
err_tx,
|
|
||||||
stream: stream.into_stream(),
|
|
||||||
service: service.into_service(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S, T> Future for StreamDispatcher<S, T>
|
|
||||||
where
|
|
||||||
S: Stream,
|
|
||||||
T: Service<Request = Request<S>, Response = ()>,
|
|
||||||
T::Future: 'static,
|
|
||||||
{
|
|
||||||
type Item = ();
|
|
||||||
type Error = T::Error;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
if let Ok(Async::Ready(Some(e))) = self.err_rx.poll() {
|
|
||||||
return Err(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
loop {
|
|
||||||
match self.service.poll_ready()? {
|
|
||||||
Async::Ready(_) => match self.stream.poll() {
|
|
||||||
Ok(Async::Ready(Some(item))) => {
|
|
||||||
tokio_current_thread::spawn(StreamDispatcherService {
|
|
||||||
fut: self.service.call(Ok(item)),
|
|
||||||
stop: self.err_tx.clone(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Err(err) => tokio_current_thread::spawn(StreamDispatcherService {
|
|
||||||
fut: self.service.call(Err(err)),
|
|
||||||
stop: self.err_tx.clone(),
|
|
||||||
}),
|
|
||||||
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
|
||||||
Ok(Async::Ready(None)) => return Ok(Async::Ready(())),
|
|
||||||
},
|
|
||||||
Async::NotReady => return Ok(Async::NotReady),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct StreamDispatcherService<F: Future> {
|
|
||||||
fut: F,
|
|
||||||
stop: mpsc::UnboundedSender<F::Error>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F: Future> Future for StreamDispatcherService<F> {
|
|
||||||
type Item = ();
|
|
||||||
type Error = ();
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
match self.fut.poll() {
|
|
||||||
Ok(Async::Ready(_)) => Ok(Async::Ready(())),
|
|
||||||
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
||||||
Err(e) => {
|
|
||||||
let _ = self.stop.unbounded_send(e);
|
|
||||||
Ok(Async::Ready(()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
69
actix-utils/src/task.rs
Normal file
69
actix-utils/src/task.rs
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
use std::cell::UnsafeCell;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::task::Waker;
|
||||||
|
use std::{fmt, rc};
|
||||||
|
|
||||||
|
/// A synchronization primitive for task wakeup.
|
||||||
|
///
|
||||||
|
/// Sometimes the task interested in a given event will change over time.
|
||||||
|
/// An `LocalWaker` can coordinate concurrent notifications with the consumer
|
||||||
|
/// potentially "updating" the underlying task to wake up. This is useful in
|
||||||
|
/// scenarios where a computation completes in another task and wants to
|
||||||
|
/// notify the consumer, but the consumer is in the process of being migrated to
|
||||||
|
/// a new logical task.
|
||||||
|
///
|
||||||
|
/// Consumers should call `register` before checking the result of a computation
|
||||||
|
/// and producers should call `wake` after producing the computation (this
|
||||||
|
/// differs from the usual `thread::park` pattern). It is also permitted for
|
||||||
|
/// `wake` to be called **before** `register`. This results in a no-op.
|
||||||
|
///
|
||||||
|
/// A single `AtomicWaker` may be reused for any number of calls to `register` or
|
||||||
|
/// `wake`.
|
||||||
|
pub struct LocalWaker {
|
||||||
|
waker: UnsafeCell<Option<Waker>>,
|
||||||
|
_t: PhantomData<rc::Rc<()>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LocalWaker {
|
||||||
|
/// Create an `LocalWaker`.
|
||||||
|
pub fn new() -> Self {
|
||||||
|
LocalWaker {
|
||||||
|
waker: UnsafeCell::new(None),
|
||||||
|
_t: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
/// Registers the waker to be notified on calls to `wake`.
|
||||||
|
pub fn register(&self, waker: &Waker) {
|
||||||
|
unsafe {
|
||||||
|
let w = self.waker.get();
|
||||||
|
if (*w).is_none() {
|
||||||
|
*w = Some(waker.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
/// Calls `wake` on the last `Waker` passed to `register`.
|
||||||
|
///
|
||||||
|
/// If `register` has not been called yet, then this does nothing.
|
||||||
|
pub fn wake(&self) {
|
||||||
|
if let Some(waker) = self.take() {
|
||||||
|
waker.wake();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the last `Waker` passed to `register`, so that the user can wake it.
|
||||||
|
///
|
||||||
|
/// If a waker has not been registered, this returns `None`.
|
||||||
|
pub fn take(&self) -> Option<Waker> {
|
||||||
|
unsafe { (*self.waker.get()).take() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for LocalWaker {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "LocalWaker")
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,10 @@
|
|||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
use std::time::{self, Duration, Instant};
|
use std::time::{self, Duration, Instant};
|
||||||
|
|
||||||
use actix_service::{NewService, Service};
|
use actix_service::{Service, ServiceFactory};
|
||||||
use futures::future::{ok, FutureResult};
|
use futures::future::{ok, ready, FutureExt, Ready};
|
||||||
use futures::{Async, Future, Poll};
|
use tokio_timer::delay_for;
|
||||||
use tokio_timer::sleep;
|
|
||||||
|
|
||||||
use super::cell::Cell;
|
use super::cell::Cell;
|
||||||
|
|
||||||
@ -42,14 +42,14 @@ impl Default for LowResTime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NewService for LowResTime {
|
impl ServiceFactory for LowResTime {
|
||||||
type Request = ();
|
type Request = ();
|
||||||
type Response = Instant;
|
type Response = Instant;
|
||||||
type Error = Infallible;
|
type Error = Infallible;
|
||||||
type InitError = Infallible;
|
type InitError = Infallible;
|
||||||
type Config = ();
|
type Config = ();
|
||||||
type Service = LowResTimeService;
|
type Service = LowResTimeService;
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, _: &()) -> Self::Future {
|
fn new_service(&self, _: &()) -> Self::Future {
|
||||||
ok(self.timer())
|
ok(self.timer())
|
||||||
@ -79,12 +79,10 @@ impl LowResTimeService {
|
|||||||
b.resolution
|
b.resolution
|
||||||
};
|
};
|
||||||
|
|
||||||
tokio_current_thread::spawn(sleep(interval).map_err(|_| panic!()).and_then(
|
tokio_executor::current_thread::spawn(delay_for(interval).then(move |_| {
|
||||||
move |_| {
|
|
||||||
inner.get_mut().current.take();
|
inner.get_mut().current.take();
|
||||||
Ok(())
|
ready(())
|
||||||
},
|
}));
|
||||||
));
|
|
||||||
now
|
now
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,10 +92,10 @@ impl Service for LowResTimeService {
|
|||||||
type Request = ();
|
type Request = ();
|
||||||
type Response = Instant;
|
type Response = Instant;
|
||||||
type Error = Infallible;
|
type Error = Infallible;
|
||||||
type Future = FutureResult<Self::Response, Self::Error>;
|
type Future = Ready<Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, _: ()) -> Self::Future {
|
fn call(&mut self, _: ()) -> Self::Future {
|
||||||
@ -146,12 +144,10 @@ impl SystemTimeService {
|
|||||||
b.resolution
|
b.resolution
|
||||||
};
|
};
|
||||||
|
|
||||||
tokio_current_thread::spawn(sleep(interval).map_err(|_| panic!()).and_then(
|
tokio_executor::current_thread::spawn(delay_for(interval).then(move |_| {
|
||||||
move |_| {
|
|
||||||
inner.get_mut().current.take();
|
inner.get_mut().current.take();
|
||||||
Ok(())
|
ready(())
|
||||||
},
|
}));
|
||||||
));
|
|
||||||
now
|
now
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -160,7 +156,6 @@ impl SystemTimeService {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use futures::future;
|
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
|
|
||||||
/// State Under Test: Two calls of `SystemTimeService::now()` return the same value if they are done within resolution interval of `SystemTimeService`.
|
/// State Under Test: Two calls of `SystemTimeService::now()` return the same value if they are done within resolution interval of `SystemTimeService`.
|
||||||
@ -170,13 +165,11 @@ mod tests {
|
|||||||
fn system_time_service_time_does_not_immediately_change() {
|
fn system_time_service_time_does_not_immediately_change() {
|
||||||
let resolution = Duration::from_millis(50);
|
let resolution = Duration::from_millis(50);
|
||||||
|
|
||||||
let _ = actix_rt::System::new("test").block_on(future::lazy(|| {
|
let _ = actix_rt::System::new("test").block_on(async {
|
||||||
let time_service = SystemTimeService::with(resolution);
|
let time_service = SystemTimeService::with(resolution);
|
||||||
|
|
||||||
assert_eq!(time_service.now(), time_service.now());
|
assert_eq!(time_service.now(), time_service.now());
|
||||||
|
});
|
||||||
Ok::<(), ()>(())
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// State Under Test: Two calls of `LowResTimeService::now()` return the same value if they are done within resolution interval of `SystemTimeService`.
|
/// State Under Test: Two calls of `LowResTimeService::now()` return the same value if they are done within resolution interval of `SystemTimeService`.
|
||||||
@ -186,13 +179,11 @@ mod tests {
|
|||||||
fn lowres_time_service_time_does_not_immediately_change() {
|
fn lowres_time_service_time_does_not_immediately_change() {
|
||||||
let resolution = Duration::from_millis(50);
|
let resolution = Duration::from_millis(50);
|
||||||
|
|
||||||
let _ = actix_rt::System::new("test").block_on(future::lazy(|| {
|
let _ = actix_rt::System::new("test").block_on(async {
|
||||||
let time_service = LowResTimeService::with(resolution);
|
let time_service = LowResTimeService::with(resolution);
|
||||||
|
|
||||||
assert_eq!(time_service.now(), time_service.now());
|
assert_eq!(time_service.now(), time_service.now());
|
||||||
|
});
|
||||||
Ok::<(), ()>(())
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// State Under Test: `SystemTimeService::now()` updates returned value every resolution period.
|
/// State Under Test: `SystemTimeService::now()` updates returned value every resolution period.
|
||||||
@ -204,7 +195,7 @@ mod tests {
|
|||||||
let resolution = Duration::from_millis(100);
|
let resolution = Duration::from_millis(100);
|
||||||
let wait_time = Duration::from_millis(150);
|
let wait_time = Duration::from_millis(150);
|
||||||
|
|
||||||
let _ = actix_rt::System::new("test").block_on(future::lazy(|| {
|
actix_rt::System::new("test").block_on(async {
|
||||||
let time_service = SystemTimeService::with(resolution);
|
let time_service = SystemTimeService::with(resolution);
|
||||||
|
|
||||||
let first_time = time_service
|
let first_time = time_service
|
||||||
@ -212,17 +203,15 @@ mod tests {
|
|||||||
.duration_since(SystemTime::UNIX_EPOCH)
|
.duration_since(SystemTime::UNIX_EPOCH)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
sleep(wait_time).then(move |_| {
|
delay_for(wait_time).await;
|
||||||
|
|
||||||
let second_time = time_service
|
let second_time = time_service
|
||||||
.now()
|
.now()
|
||||||
.duration_since(SystemTime::UNIX_EPOCH)
|
.duration_since(SystemTime::UNIX_EPOCH)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert!(second_time - first_time >= wait_time);
|
assert!(second_time - first_time >= wait_time);
|
||||||
|
});
|
||||||
Ok::<(), ()>(())
|
|
||||||
})
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// State Under Test: `LowResTimeService::now()` updates returned value every resolution period.
|
/// State Under Test: `LowResTimeService::now()` updates returned value every resolution period.
|
||||||
@ -234,18 +223,15 @@ mod tests {
|
|||||||
let resolution = Duration::from_millis(100);
|
let resolution = Duration::from_millis(100);
|
||||||
let wait_time = Duration::from_millis(150);
|
let wait_time = Duration::from_millis(150);
|
||||||
|
|
||||||
let _ = actix_rt::System::new("test").block_on(future::lazy(|| {
|
let _ = actix_rt::System::new("test").block_on(async {
|
||||||
let time_service = LowResTimeService::with(resolution);
|
let time_service = LowResTimeService::with(resolution);
|
||||||
|
|
||||||
let first_time = time_service.now();
|
let first_time = time_service.now();
|
||||||
|
|
||||||
sleep(wait_time).then(move |_| {
|
delay_for(wait_time).await;
|
||||||
|
|
||||||
let second_time = time_service.now();
|
let second_time = time_service.now();
|
||||||
|
|
||||||
assert!(second_time - first_time >= wait_time);
|
assert!(second_time - first_time >= wait_time);
|
||||||
|
});
|
||||||
Ok::<(), ()>(())
|
|
||||||
})
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,19 +2,21 @@
|
|||||||
//!
|
//!
|
||||||
//! If the response does not complete within the specified timeout, the response
|
//! If the response does not complete within the specified timeout, the response
|
||||||
//! will be aborted.
|
//! will be aborted.
|
||||||
use std::fmt;
|
use std::future::Future;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::time::Duration;
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
use std::{fmt, time};
|
||||||
|
|
||||||
use actix_service::{IntoService, Service, Transform};
|
use actix_service::{IntoService, Service, Transform};
|
||||||
use futures::future::{ok, FutureResult};
|
use futures::future::{ok, Ready};
|
||||||
use futures::{Async, Future, Poll};
|
use pin_project::pin_project;
|
||||||
use tokio_timer::{clock, Delay};
|
use tokio_timer::{clock, delay, Delay};
|
||||||
|
|
||||||
/// Applies a timeout to requests.
|
/// Applies a timeout to requests.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Timeout<E = ()> {
|
pub struct Timeout<E = ()> {
|
||||||
timeout: Duration,
|
timeout: time::Duration,
|
||||||
_t: PhantomData<E>,
|
_t: PhantomData<E>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,7 +68,7 @@ impl<E: PartialEq> PartialEq for TimeoutError<E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<E> Timeout<E> {
|
impl<E> Timeout<E> {
|
||||||
pub fn new(timeout: Duration) -> Self {
|
pub fn new(timeout: time::Duration) -> Self {
|
||||||
Timeout {
|
Timeout {
|
||||||
timeout,
|
timeout,
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
@ -89,7 +91,7 @@ where
|
|||||||
type Error = TimeoutError<S::Error>;
|
type Error = TimeoutError<S::Error>;
|
||||||
type InitError = E;
|
type InitError = E;
|
||||||
type Transform = TimeoutService<S>;
|
type Transform = TimeoutService<S>;
|
||||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
fn new_transform(&self, service: S) -> Self::Future {
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
ok(TimeoutService {
|
ok(TimeoutService {
|
||||||
@ -103,14 +105,14 @@ where
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct TimeoutService<S> {
|
pub struct TimeoutService<S> {
|
||||||
service: S,
|
service: S,
|
||||||
timeout: Duration,
|
timeout: time::Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> TimeoutService<S>
|
impl<S> TimeoutService<S>
|
||||||
where
|
where
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
pub fn new<U>(timeout: Duration, service: U) -> Self
|
pub fn new<U>(timeout: time::Duration, service: U) -> Self
|
||||||
where
|
where
|
||||||
U: IntoService<S>,
|
U: IntoService<S>,
|
||||||
{
|
{
|
||||||
@ -130,21 +132,23 @@ where
|
|||||||
type Error = TimeoutError<S::Error>;
|
type Error = TimeoutError<S::Error>;
|
||||||
type Future = TimeoutServiceResponse<S>;
|
type Future = TimeoutServiceResponse<S>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.service.poll_ready().map_err(TimeoutError::Service)
|
self.service.poll_ready(cx).map_err(TimeoutError::Service)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, request: S::Request) -> Self::Future {
|
fn call(&mut self, request: S::Request) -> Self::Future {
|
||||||
TimeoutServiceResponse {
|
TimeoutServiceResponse {
|
||||||
fut: self.service.call(request),
|
fut: self.service.call(request),
|
||||||
sleep: Delay::new(clock::now() + self.timeout),
|
sleep: delay(clock::now() + self.timeout),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `TimeoutService` response future
|
/// `TimeoutService` response future
|
||||||
|
#[pin_project]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TimeoutServiceResponse<T: Service> {
|
pub struct TimeoutServiceResponse<T: Service> {
|
||||||
|
#[pin]
|
||||||
fut: T::Future,
|
fut: T::Future,
|
||||||
sleep: Delay,
|
sleep: Delay,
|
||||||
}
|
}
|
||||||
@ -153,36 +157,34 @@ impl<T> Future for TimeoutServiceResponse<T>
|
|||||||
where
|
where
|
||||||
T: Service,
|
T: Service,
|
||||||
{
|
{
|
||||||
type Item = T::Response;
|
type Output = Result<T::Response, TimeoutError<T::Error>>;
|
||||||
type Error = TimeoutError<T::Error>;
|
|
||||||
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
let mut this = self.project();
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
// First, try polling the future
|
// First, try polling the future
|
||||||
match self.fut.poll() {
|
match this.fut.poll(cx) {
|
||||||
Ok(Async::Ready(v)) => return Ok(Async::Ready(v)),
|
Poll::Ready(Ok(v)) => return Poll::Ready(Ok(v)),
|
||||||
Ok(Async::NotReady) => {}
|
Poll::Ready(Err(e)) => return Poll::Ready(Err(TimeoutError::Service(e))),
|
||||||
Err(e) => return Err(TimeoutError::Service(e)),
|
Poll::Pending => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now check the sleep
|
// Now check the sleep
|
||||||
match self.sleep.poll() {
|
match Pin::new(&mut this.sleep).poll(cx) {
|
||||||
Ok(Async::NotReady) => Ok(Async::NotReady),
|
Poll::Pending => Poll::Pending,
|
||||||
Ok(Async::Ready(_)) => Err(TimeoutError::Timeout),
|
Poll::Ready(_) => Poll::Ready(Err(TimeoutError::Timeout)),
|
||||||
Err(_) => Err(TimeoutError::Timeout),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use futures::future::lazy;
|
use std::task::{Context, Poll};
|
||||||
use futures::{Async, Poll};
|
|
||||||
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use actix_service::blank::{Blank, BlankNewService};
|
use actix_service::{apply, factory_fn, Service, ServiceFactory};
|
||||||
use actix_service::{NewService, Service, ServiceExt};
|
use futures::future::{ok, FutureExt, LocalBoxFuture};
|
||||||
|
|
||||||
struct SleepService(Duration);
|
struct SleepService(Duration);
|
||||||
|
|
||||||
@ -190,14 +192,16 @@ mod tests {
|
|||||||
type Request = ();
|
type Request = ();
|
||||||
type Response = ();
|
type Response = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = Box<dyn Future<Item = (), Error = ()>>;
|
type Future = LocalBoxFuture<'static, Result<(), ()>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
||||||
Ok(Async::Ready(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, _: ()) -> Self::Future {
|
fn call(&mut self, _: ()) -> Self::Future {
|
||||||
Box::new(tokio_timer::sleep(self.0).map_err(|_| ()))
|
tokio_timer::delay_for(self.0)
|
||||||
|
.then(|_| ok::<_, ()>(()))
|
||||||
|
.boxed_local()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,11 +210,10 @@ mod tests {
|
|||||||
let resolution = Duration::from_millis(100);
|
let resolution = Duration::from_millis(100);
|
||||||
let wait_time = Duration::from_millis(50);
|
let wait_time = Duration::from_millis(50);
|
||||||
|
|
||||||
let res = actix_rt::System::new("test").block_on(lazy(|| {
|
let res = actix_rt::System::new("test").block_on(async {
|
||||||
let mut timeout = Blank::default()
|
let mut timeout = TimeoutService::new(resolution, SleepService(wait_time));
|
||||||
.and_then(TimeoutService::new(resolution, SleepService(wait_time)));
|
timeout.call(()).await
|
||||||
timeout.call(())
|
});
|
||||||
}));
|
|
||||||
assert_eq!(res, Ok(()));
|
assert_eq!(res, Ok(()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,11 +222,10 @@ mod tests {
|
|||||||
let resolution = Duration::from_millis(100);
|
let resolution = Duration::from_millis(100);
|
||||||
let wait_time = Duration::from_millis(150);
|
let wait_time = Duration::from_millis(150);
|
||||||
|
|
||||||
let res = actix_rt::System::new("test").block_on(lazy(|| {
|
let res = actix_rt::System::new("test").block_on(async {
|
||||||
let mut timeout = Blank::default()
|
let mut timeout = TimeoutService::new(resolution, SleepService(wait_time));
|
||||||
.and_then(TimeoutService::new(resolution, SleepService(wait_time)));
|
timeout.call(()).await
|
||||||
timeout.call(())
|
});
|
||||||
}));
|
|
||||||
assert_eq!(res, Err(TimeoutError::Timeout));
|
assert_eq!(res, Err(TimeoutError::Timeout));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -232,15 +234,15 @@ mod tests {
|
|||||||
let resolution = Duration::from_millis(100);
|
let resolution = Duration::from_millis(100);
|
||||||
let wait_time = Duration::from_millis(150);
|
let wait_time = Duration::from_millis(150);
|
||||||
|
|
||||||
let res = actix_rt::System::new("test").block_on(lazy(|| {
|
let res = actix_rt::System::new("test").block_on(async {
|
||||||
let timeout = BlankNewService::<(), (), ()>::default()
|
let timeout = apply(
|
||||||
.apply(Timeout::new(resolution), || Ok(SleepService(wait_time)));
|
Timeout::new(resolution),
|
||||||
if let Async::Ready(mut to) = timeout.new_service(&()).poll().unwrap() {
|
factory_fn(|| ok::<_, ()>(SleepService(wait_time))),
|
||||||
to.call(())
|
);
|
||||||
} else {
|
let mut srv = timeout.new_service(&()).await.unwrap();
|
||||||
panic!()
|
|
||||||
}
|
srv.call(()).await
|
||||||
}));
|
});
|
||||||
assert_eq!(res, Err(TimeoutError::Timeout));
|
assert_eq!(res, Err(TimeoutError::Timeout));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user