1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-08-12 15:37:05 +02:00

Compare commits

..

27 Commits

Author SHA1 Message Date
Nikolay Kim
1fcc0734b5 prep release 2019-03-12 17:01:02 -07:00
Nikolay Kim
b6f952b036 remove constraint 2019-03-12 16:49:57 -07:00
Nikolay Kim
0fdac38307 add constraint 2019-03-12 16:48:29 -07:00
Nikolay Kim
a3c4637372 prepare actix-server release 2019-03-12 16:40:22 -07:00
Nikolay Kim
ae9bc5ae78 refactor ApplyConfig combinator 2019-03-12 16:32:10 -07:00
Nikolay Kim
21c289d7e4 fix InOrderService::poll_ready() nested service rediness check 2019-03-12 16:03:05 -07:00
Nikolay Kim
5e6eed905c fix InFlightService::poll_ready() nested service rediness check 2019-03-12 15:48:02 -07:00
Nikolay Kim
6801a38de5 check readiness for all services 2019-03-12 15:15:14 -07:00
Nikolay Kim
39356690b0 use ServerConfig for system configuration 2019-03-12 14:14:21 -07:00
Nikolay Kim
f6f292a678 enforce constraint on OpensslAcceptor 2019-03-12 13:57:28 -07:00
Nikolay Kim
b3366bc1af service response is not important 2019-03-12 13:55:31 -07:00
Nikolay Kim
2c1f8f0b96 enforce constraints on AndThenNewService type 2019-03-12 13:50:14 -07:00
Nikolay Kim
e7465bfa2e fix map_err constraints 2019-03-12 13:45:05 -07:00
Nikolay Kim
755d4958c5 use IntoService types for transform services 2019-03-12 13:39:04 -07:00
Nikolay Kim
825117fd4c add Deref/DerefMut impls for Io 2019-03-12 13:12:22 -07:00
Nikolay Kim
7033b50fed add Io::set method for overriding param 2019-03-12 12:53:43 -07:00
Nikolay Kim
ef9bfb8981 add helpers 2019-03-12 12:53:08 -07:00
Nikolay Kim
bef199f831 add blocking mod support 2019-03-11 22:54:27 -07:00
Nikolay Kim
f1d4bcef4b add Arbiter::exec_fn and exec functions 2019-03-11 22:51:17 -07:00
Nikolay Kim
8e13ba7bce update readme 2019-03-11 18:24:31 -07:00
Nikolay Kim
9a9b3e9ca9 update server tests 2019-03-11 15:19:28 -07:00
Nikolay Kim
5567fb41d2 add default type parameter 2019-03-11 13:48:55 -07:00
Nikolay Kim
ad50595ece add PartialEq impl for Io 2019-03-11 13:37:30 -07:00
Nikolay Kim
2430c7247b update timing for travis 2019-03-11 12:53:16 -07:00
Nikolay Kim
1bf0f1e1a5 add Debug impl for Io; update examples 2019-03-11 12:46:12 -07:00
Nikolay Kim
9887aef6e8 add delay to test 2019-03-11 12:35:57 -07:00
Nikolay Kim
787255d030 add io parameters 2019-03-11 12:01:55 -07:00
40 changed files with 1014 additions and 416 deletions

View File

@@ -35,7 +35,7 @@ script:
cargo test --features="ssl,tls,rust-tls" -- --nocapture
cd actix-codec && cargo test && cd ..
cd actix-service && cargo test && cd ..
cd actix-server && cargo test --features="ssl,tls,rust-tls" -- --nocapture && cd ..
cd actix-server && cargo test --all-features -- --nocapture && cd ..
cd actix-rt && cargo test && cd ..
cd actix-connector && cargo test && cd ..
cd actix-utils && cargo test && cd ..

View File

@@ -1,20 +1,18 @@
# Actix net [![Build Status](https://travis-ci.org/actix/actix-net.svg?branch=master)](https://travis-ci.org/actix/actix-net) [![codecov](https://codecov.io/gh/actix/actix-net/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-net) [![crates.io](https://meritbadge.herokuapp.com/actix-net)](https://crates.io/crates/actix-net) [![Join the chat at https://gitter.im/actix/actix](https://badges.gitter.im/actix/actix.svg)](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
Actix net - framework for composable network services (experimental)
Actix net - framework for composable network services
## Documentation & community resources
* [API Documentation (Development)](https://actix.rs/actix-net/actix_net/)
* [Chat on gitter](https://gitter.im/actix/actix)
* Cargo package: [actix-net](https://crates.io/crates/actix-net)
* Minimum supported Rust version: 1.26 or later
* Minimum supported Rust version: 1.32 or later
## Example
```rust
fn main() {
let sys = actix_rt::System::new("test");
fn main() -> io::Result<()> {
// load ssl keys
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
builder.set_private_key_file("./examples/key.pem", SslFiletype::PEM).unwrap();
@@ -26,7 +24,7 @@ fn main() {
// bind socket address and start workers. By default server uses number of
// available logical cpu as threads count. actix net start separate
// instances of service pipeline in each worker.
actix_server::build()
Server::build()
.bind(
// configure service pipeline
"basic", "0.0.0.0:8443",
@@ -35,28 +33,23 @@ fn main() {
let acceptor = acceptor.clone();
// service for converting incoming TcpStream to a SslStream<TcpStream>
(move |stream| {
SslAcceptorExt::accept_async(&acceptor, stream)
.map_err(|e| println!("Openssl error: {}", e))
})
// convert closure to a `NewService`
.into_new_service()
// .and_then() combinator uses other service to convert incoming `Request` to a `Response`
// and then uses that response as an input for next service.
// in this case, on success we use `logger` service
.and_then(logger)
// Next service counts number of connections
.and_then(move |req| {
let num = num.fetch_add(1, Ordering::Relaxed);
println!("processed {:?} connections", num);
future::ok(())
})
}).unwrap()
.start();
sys.run();
fn_service(move |stream: Io<tokio_tcp::TcpStream>| {
SslAcceptorExt::accept_async(&acceptor, stream.into_parts().0)
.map_err(|e| println!("Openssl error: {}", e))
})
// .and_then() combinator uses other service to convert incoming `Request` to a
// `Response` and then uses that response as an input for next
// service. in this case, on success we use `logger` service
.and_then(fn_service(logger))
// Next service counts number of connections
.and_then(move |_| {
let num = num.fetch_add(1, Ordering::Relaxed);
println!("got ssl connection {:?}", num);
future::ok(())
})
},
)?
.run()
}
```

View File

@@ -1,5 +1,15 @@
# Changes
## [0.2.1] - 2019-03-11
### Added
* Added `blocking` module
* Arbiter::exec_fn - execute fn on the arbiter's thread
* Arbiter::exec - execute fn on the arbiter's thread and wait result
## [0.2.0] - 2019-03-06
* `run` method returns `io::Result<()>`

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-rt"
version = "0.2.0"
version = "0.2.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix runtime"
keywords = ["network", "framework", "async", "futures"]
@@ -18,9 +18,14 @@ name = "actix_rt"
path = "src/lib.rs"
[dependencies]
log = "0.4"
bytes = "0.4"
derive_more = "0.14"
futures = "0.1.25"
parking_lot = "0.7"
lazy_static = "1.2"
log = "0.4"
num_cpus = "1.10"
threadpool = "1.7"
tokio-current-thread = "0.1"
tokio-executor = "0.1.5"
tokio-reactor = "0.1.7"

View File

@@ -4,7 +4,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::{fmt, thread};
use futures::sync::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
use futures::sync::oneshot::{channel, Sender};
use futures::sync::oneshot::{channel, Canceled, Sender};
use futures::{future, Async, Future, IntoFuture, Poll, Stream};
use tokio_current_thread::spawn;
@@ -22,6 +22,7 @@ pub(crate) static COUNT: AtomicUsize = AtomicUsize::new(0);
pub(crate) enum ArbiterCommand {
Stop,
Execute(Box<Future<Item = (), Error = ()> + Send>),
ExecuteFn(Box<FnExec>),
}
impl fmt::Debug for ArbiterCommand {
@@ -29,6 +30,7 @@ impl fmt::Debug for ArbiterCommand {
match self {
ArbiterCommand::Stop => write!(f, "ArbiterCommand::Stop"),
ArbiterCommand::Execute(_) => write!(f, "ArbiterCommand::Execute"),
ArbiterCommand::ExecuteFn(_) => write!(f, "ArbiterCommand::ExecuteFn"),
}
}
}
@@ -158,6 +160,35 @@ impl Arbiter {
.0
.unbounded_send(ArbiterCommand::Execute(Box::new(future)));
}
/// Send a function to the arbiter's thread and exeute.
pub fn exec_fn<F>(&self, f: F)
where
F: FnOnce() + Send + 'static,
{
let _ = self
.0
.unbounded_send(ArbiterCommand::ExecuteFn(Box::new(move || {
let _ = f();
})));
}
/// Send a function to the arbiter's thread, exeute and return result.
pub fn exec<F, R>(&self, f: F) -> impl Future<Item = R, Error = Canceled>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
let (tx, rx) = channel();
let _ = self
.0
.unbounded_send(ArbiterCommand::ExecuteFn(Box::new(move || {
if !tx.is_canceled() {
let _ = tx.send(f());
}
})));
rx
}
}
struct ArbiterController {
@@ -194,6 +225,9 @@ impl Future for ArbiterController {
ArbiterCommand::Execute(fut) => {
spawn(fut);
}
ArbiterCommand::ExecuteFn(f) => {
f.call_box();
}
},
Ok(Async::NotReady) => return Ok(Async::NotReady),
}
@@ -257,11 +291,16 @@ impl Future for SystemArbiter {
}
}
// /// Execute function in arbiter's thread
// impl<I: Send, E: Send> Handler<Execute<I, E>> for SystemArbiter {
// type Result = Result<I, E>;
pub trait FnExec: Send + 'static {
fn call_box(self: Box<Self>);
}
// fn handle(&mut self, msg: Execute<I, E>, _: &mut Context<Self>) -> Result<I, E> {
// msg.exec()
// }
// }
impl<F> FnExec for F
where
F: FnOnce() + Send + 'static,
{
#[cfg_attr(feature = "cargo-clippy", allow(boxed_local))]
fn call_box(self: Box<Self>) {
(*self)()
}
}

88
actix-rt/src/blocking.rs Normal file
View File

@@ -0,0 +1,88 @@
//! Thread pool for blocking operations
use std::fmt;
use derive_more::Display;
use futures::sync::oneshot;
use futures::{Async, Future, Poll};
use parking_lot::Mutex;
use threadpool::ThreadPool;
/// Env variable for default cpu pool size
const ENV_CPU_POOL_VAR: &str = "ACTIX_CPU_POOL";
lazy_static::lazy_static! {
pub(crate) static ref DEFAULT_POOL: Mutex<ThreadPool> = {
let default = match std::env::var(ENV_CPU_POOL_VAR) {
Ok(val) => {
if let Ok(val) = val.parse() {
val
} else {
log::error!("Can not parse ACTIX_CPU_POOL value");
num_cpus::get() * 5
}
}
Err(_) => num_cpus::get() * 5,
};
Mutex::new(
threadpool::Builder::new()
.thread_name("actix-web".to_owned())
.num_threads(default)
.build(),
)
};
}
thread_local! {
static POOL: ThreadPool = {
DEFAULT_POOL.lock().clone()
};
}
/// Blocking operation execution error
#[derive(Debug, Display)]
pub enum BlockingError<E: fmt::Debug> {
#[display(fmt = "{:?}", _0)]
Error(E),
#[display(fmt = "Thread pool is gone")]
Canceled,
}
/// Execute blocking function on a thread pool, returns future that resolves
/// to result of the function execution.
pub fn run<F, I, E>(f: F) -> CpuFuture<I, E>
where
F: FnOnce() -> Result<I, E> + Send + 'static,
I: Send + 'static,
E: Send + fmt::Debug + 'static,
{
let (tx, rx) = oneshot::channel();
POOL.with(|pool| {
pool.execute(move || {
if !tx.is_canceled() {
let _ = tx.send(f());
}
})
});
CpuFuture { rx }
}
/// Blocking operation completion future. It resolves with results
/// of blocking function execution.
pub struct CpuFuture<I, E> {
rx: oneshot::Receiver<Result<I, E>>,
}
impl<I, E: fmt::Debug> Future for CpuFuture<I, E> {
type Item = I;
type Error = BlockingError<E>;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let res = futures::try_ready!(self.rx.poll().map_err(|_| BlockingError::Canceled));
match res {
Ok(val) => Ok(Async::Ready(val)),
Err(err) => Err(BlockingError::Error(err)),
}
}
}

View File

@@ -1,6 +1,7 @@
//! A runtime implementation that runs everything on the current thread.
mod arbiter;
pub mod blocking;
mod builder;
mod runtime;
mod system;

View File

@@ -1,14 +1,18 @@
use std::cell::RefCell;
use std::io;
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
use futures::sync::mpsc::UnboundedSender;
use crate::arbiter::{Arbiter, SystemCommand};
use crate::builder::{Builder, SystemRunner};
static SYSTEM_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
/// System is a runtime manager.
#[derive(Clone, Debug)]
pub struct System {
id: usize,
sys: UnboundedSender<SystemCommand>,
arbiter: Arbiter,
stop_on_panic: bool,
@@ -29,6 +33,7 @@ impl System {
sys,
arbiter,
stop_on_panic,
id: SYSTEM_COUNT.fetch_add(1, Ordering::SeqCst),
};
System::set_current(sys.clone());
sys
@@ -82,6 +87,11 @@ impl System {
})
}
/// System id
pub fn id(&self) -> usize {
self.id
}
/// Stop the system
pub fn stop(&self) {
self.stop_with_code(0)

View File

@@ -1,4 +1,5 @@
use std::cell::Cell;
use std::fmt;
use std::net::SocketAddr;
use std::rc::Rc;
@@ -31,3 +32,101 @@ impl ServerConfig {
self.secure.as_ref().set(true)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Protocol {
Unknown,
Http10,
Http11,
Http2,
Proto1,
Proto2,
Proto3,
Proto4,
Proto5,
Proto6,
}
pub struct Io<T, P = ()> {
io: T,
proto: Protocol,
params: P,
}
impl<T> Io<T, ()> {
pub fn new(io: T) -> Self {
Self {
io,
proto: Protocol::Unknown,
params: (),
}
}
}
impl<T, P> Io<T, P> {
/// Reconstruct from a parts.
pub fn from_parts(io: T, params: P, proto: Protocol) -> Self {
Self { io, params, proto }
}
/// Deconstruct into a parts.
pub fn into_parts(self) -> (T, P, Protocol) {
(self.io, self.params, self.proto)
}
/// Returns a shared reference to the underlying stream.
pub fn get_ref(&self) -> &T {
&self.io
}
/// Returns a mutable reference to the underlying stream.
pub fn get_mut(&mut self) -> &mut T {
&mut self.io
}
/// Get selected protocol
pub fn protocol(&self) -> Protocol {
self.proto
}
/// Return new Io object with new parameter.
pub fn set<U>(self, params: U) -> Io<T, U> {
Io {
io: self.io,
proto: self.proto,
params: params,
}
}
/// Maps an Io<_, P> to Io<_, U> by applying a function to a contained value.
pub fn map<U, F>(self, op: F) -> Io<T, U>
where
F: FnOnce(P) -> U,
{
Io {
io: self.io,
proto: self.proto,
params: op(self.params),
}
}
}
impl<T, P> std::ops::Deref for Io<T, P> {
type Target = T;
fn deref(&self) -> &T {
&self.io
}
}
impl<T, P> std::ops::DerefMut for Io<T, P> {
fn deref_mut(&mut self) -> &mut T {
&mut self.io
}
}
impl<T: fmt::Debug, P> fmt::Debug for Io<T, P> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Io {{{:?}}}", self.io)
}
}

View File

@@ -1,6 +1,12 @@
# Changes
## [0.4.0] - 2019-03-xx
## [0.4.0] - 2019-03-12
### Changed
* Use `ServerConfig` for service factory
* Wrap tcp socket to `Io` type
* Upgrade actix-service

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-server"
version = "0.3.1"
version = "0.4.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix server - General purpose tcp server"
keywords = ["network", "framework", "async", "futures"]
@@ -33,9 +33,9 @@ ssl = ["openssl", "tokio-openssl"]
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
[dependencies]
actix-rt = "0.2.0"
actix-service = "0.3.3"
actix-server-config = { path="../actix-server-config" }
actix-rt = "0.2.1"
actix-service = "0.3.4"
actix-server-config = "0.1.0"
log = "0.4"
num_cpus = "1.0"
@@ -64,5 +64,6 @@ webpki = { version = "0.19", optional = true }
webpki-roots = { version = "0.16", optional = true }
[dev-dependencies]
env_logger = "0.6"
bytes = "0.4"
actix-codec = "0.1.0"
env_logger = "0.6"

View File

@@ -23,6 +23,7 @@ use crate::{ssl, Token};
pub struct ServerBuilder {
threads: usize,
token: Token,
backlog: i32,
workers: Vec<(usize, WorkerClient)>,
services: Vec<Box<InternalServiceFactory>>,
sockets: Vec<(Token, net::TcpListener)>,
@@ -53,6 +54,7 @@ impl ServerBuilder {
services: Vec::new(),
sockets: Vec::new(),
accept: AcceptLoop::new(server.clone()),
backlog: 2048,
exit: false,
shutdown_timeout: Duration::from_secs(30),
no_signals: false,
@@ -70,6 +72,21 @@ impl ServerBuilder {
self
}
/// Set the maximum number of pending connections.
///
/// This refers to the number of clients that can be waiting to be served.
/// Exceeding this number results in the client getting an error when
/// attempting to connect. It should only affect servers under significant
/// load.
///
/// Generally set in the 64-2048 range. Default value is 2048.
///
/// This method should be called before `bind()` method call.
pub fn backlog(mut self, num: i32) -> Self {
self.backlog = num;
self
}
/// Sets the maximum per-worker number of concurrent connections.
///
/// All socket listeners will stop accepting connections when this limit is
@@ -125,7 +142,7 @@ impl ServerBuilder {
where
F: Fn(&mut ServiceConfig) -> io::Result<()>,
{
let mut cfg = ServiceConfig::new(self.threads);
let mut cfg = ServiceConfig::new(self.threads, self.backlog);
f(&mut cfg)?;
@@ -133,7 +150,7 @@ impl ServerBuilder {
let mut srv = ConfiguredService::new(apply);
for (name, lst) in cfg.services {
let token = self.token.next();
srv.stream(token, name);
srv.stream(token, name, lst.local_addr()?);
self.sockets.push((token, lst));
}
self.services.push(Box::new(srv));
@@ -149,7 +166,7 @@ impl ServerBuilder {
F: ServiceFactory,
U: net::ToSocketAddrs,
{
let sockets = bind_addr(addr)?;
let sockets = bind_addr(addr, self.backlog)?;
for lst in sockets {
let token = self.token.next();
@@ -393,12 +410,15 @@ impl Future for ServerBuilder {
}
}
pub(super) fn bind_addr<S: net::ToSocketAddrs>(addr: S) -> io::Result<Vec<net::TcpListener>> {
pub(super) fn bind_addr<S: net::ToSocketAddrs>(
addr: S,
backlog: i32,
) -> io::Result<Vec<net::TcpListener>> {
let mut err = None;
let mut succ = false;
let mut sockets = Vec::new();
for addr in addr.to_socket_addrs()? {
match create_tcp_listener(addr) {
match create_tcp_listener(addr, backlog) {
Ok(lst) => {
succ = true;
sockets.push(lst);
@@ -421,12 +441,12 @@ pub(super) fn bind_addr<S: net::ToSocketAddrs>(addr: S) -> io::Result<Vec<net::T
}
}
fn create_tcp_listener(addr: net::SocketAddr) -> io::Result<net::TcpListener> {
fn create_tcp_listener(addr: net::SocketAddr, backlog: i32) -> io::Result<net::TcpListener> {
let builder = match addr {
net::SocketAddr::V4(_) => TcpBuilder::new_v4()?,
net::SocketAddr::V6(_) => TcpBuilder::new_v6()?,
};
builder.reuse_address(true)?;
builder.bind(addr)?;
Ok(builder.listen(1024)?)
Ok(builder.listen(backlog)?)
}

View File

@@ -10,7 +10,7 @@ mod signals;
pub mod ssl;
mod worker;
pub use actix_server_config::ServerConfig;
pub use actix_server_config::{Io, Protocol, ServerConfig};
pub use self::builder::ServerBuilder;
pub use self::server::Server;

View File

@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::{fmt, io, net};
use actix_server_config::{Io, ServerConfig};
use actix_service::{IntoNewService, NewService};
use futures::future::{join_all, Future};
use log::error;
@@ -18,12 +19,14 @@ pub struct ServiceConfig {
pub(crate) services: Vec<(String, net::TcpListener)>,
pub(crate) apply: Option<Box<ServiceRuntimeConfiguration>>,
pub(crate) threads: usize,
pub(crate) backlog: i32,
}
impl ServiceConfig {
pub(super) fn new(threads: usize) -> ServiceConfig {
pub(super) fn new(threads: usize, backlog: i32) -> ServiceConfig {
ServiceConfig {
threads,
backlog,
services: Vec::new(),
apply: None,
}
@@ -42,7 +45,7 @@ impl ServiceConfig {
where
U: net::ToSocketAddrs,
{
let sockets = bind_addr(addr)?;
let sockets = bind_addr(addr, self.backlog)?;
for lst in sockets {
self.listen(name.as_ref(), lst);
@@ -73,7 +76,7 @@ impl ServiceConfig {
pub(super) struct ConfiguredService {
rt: Box<ServiceRuntimeConfiguration>,
names: HashMap<Token, String>,
names: HashMap<Token, (String, net::SocketAddr)>,
services: HashMap<String, Token>,
}
@@ -86,15 +89,15 @@ impl ConfiguredService {
}
}
pub(super) fn stream(&mut self, token: Token, name: String) {
self.names.insert(token, name.clone());
pub(super) fn stream(&mut self, token: Token, name: String, addr: net::SocketAddr) {
self.names.insert(token, (name.clone(), addr));
self.services.insert(name, token);
}
}
impl InternalServiceFactory for ConfiguredService {
fn name(&self, token: Token) -> &str {
&self.names[&token]
&self.names[&token].0
}
fn clone_factory(&self) -> Box<InternalServiceFactory> {
@@ -114,7 +117,8 @@ impl InternalServiceFactory for ConfiguredService {
// construct services
let mut fut = Vec::new();
for (token, ns) in rt.services {
fut.push(ns.new_service(&()).map(move |service| (token, service)));
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| {
@@ -169,8 +173,8 @@ impl ServiceRuntime {
pub fn service<T, F>(&mut self, name: &str, service: F)
where
F: IntoNewService<T>,
T: NewService<Request = TcpStream, Response = ()> + 'static,
F: IntoNewService<T, ServerConfig>,
T: NewService<ServerConfig, Request = Io<TcpStream>> + 'static,
T::Future: 'static,
T::Service: 'static,
T::InitError: fmt::Debug,
@@ -191,6 +195,7 @@ impl ServiceRuntime {
type BoxedNewService = Box<
NewService<
ServerConfig,
Request = (Option<CounterGuard>, ServerMessage),
Response = (),
Error = (),
@@ -204,9 +209,9 @@ struct ServiceFactory<T> {
inner: T,
}
impl<T> NewService for ServiceFactory<T>
impl<T> NewService<ServerConfig> for ServiceFactory<T>
where
T: NewService<Request = TcpStream, Response = ()>,
T: NewService<ServerConfig, Request = Io<TcpStream>>,
T::Future: 'static,
T::Service: 'static,
T::Error: 'static,
@@ -219,8 +224,8 @@ where
type Service = BoxedServerService;
type Future = Box<Future<Item = BoxedServerService, Error = ()>>;
fn new_service(&self, _: &()) -> Self::Future {
Box::new(self.inner.new_service(&()).map_err(|_| ()).map(|s| {
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
Box::new(self.inner.new_service(cfg).map_err(|_| ()).map(|s| {
let service: BoxedServerService = Box::new(StreamService::new(s));
service
}))

View File

@@ -1,14 +1,14 @@
use std::net::{SocketAddr, TcpStream};
use std::net::{self, SocketAddr};
use std::time::Duration;
use actix_rt::spawn;
use actix_server_config::ServerConfig;
use actix_server_config::{Io, ServerConfig};
use actix_service::{NewService, Service};
use futures::future::{err, ok, FutureResult};
use futures::{Future, Poll};
use log::error;
use tokio_reactor::Handle;
use tokio_tcp::TcpStream as TokioTcpStream;
use tokio_tcp::TcpStream;
use super::Token;
use crate::counter::CounterGuard;
@@ -16,7 +16,7 @@ use crate::counter::CounterGuard;
/// Server message
pub(crate) enum ServerMessage {
/// New stream
Connect(TcpStream),
Connect(net::TcpStream),
/// Gracefull shutdown
Shutdown(Duration),
/// Force shutdown
@@ -24,7 +24,7 @@ pub(crate) enum ServerMessage {
}
pub trait ServiceFactory: Send + Clone + 'static {
type NewService: NewService<ServerConfig, Request = TokioTcpStream>;
type NewService: NewService<ServerConfig, Request = Io<TcpStream>>;
fn create(&self) -> Self::NewService;
}
@@ -58,7 +58,7 @@ impl<T> StreamService<T> {
impl<T> Service for StreamService<T>
where
T: Service<Request = TokioTcpStream>,
T: Service<Request = Io<TcpStream>>,
T::Future: 'static,
T::Error: 'static,
{
@@ -74,13 +74,12 @@ where
fn call(&mut self, (guard, req): (Option<CounterGuard>, ServerMessage)) -> Self::Future {
match req {
ServerMessage::Connect(stream) => {
let stream =
TokioTcpStream::from_std(stream, &Handle::default()).map_err(|e| {
error!("Can not convert to an async tcp stream: {}", e);
});
let stream = TcpStream::from_std(stream, &Handle::default()).map_err(|e| {
error!("Can not convert to an async tcp stream: {}", e);
});
if let Ok(stream) = stream {
spawn(self.service.call(stream).then(move |res| {
spawn(self.service.call(Io::new(stream)).then(move |res| {
drop(guard);
res.map_err(|_| ()).map(|_| ())
}));
@@ -170,7 +169,7 @@ impl InternalServiceFactory for Box<InternalServiceFactory> {
impl<F, T> ServiceFactory for F
where
F: Fn() -> T + Send + Clone + 'static,
T: NewService<ServerConfig, Request = TokioTcpStream>,
T: NewService<ServerConfig, Request = Io<TcpStream>>,
{
type NewService = T;

View File

@@ -1,7 +1,6 @@
use std::io;
use std::marker::PhantomData;
use actix_server_config::ServerConfig;
use actix_service::{NewService, Service};
use futures::{future::ok, future::FutureResult, Async, Future, Poll};
use native_tls::{self, Error, HandshakeError, TlsAcceptor};
@@ -9,16 +8,17 @@ use tokio_io::{AsyncRead, AsyncWrite};
use crate::counter::{Counter, CounterGuard};
use crate::ssl::MAX_CONN_COUNTER;
use crate::{Io, Protocol, ServerConfig};
/// Support `SSL` connections via native-tls package
///
/// `tls` feature enables `NativeTlsAcceptor` type
pub struct NativeTlsAcceptor<T> {
pub struct NativeTlsAcceptor<T, P = ()> {
acceptor: TlsAcceptor,
io: PhantomData<T>,
io: PhantomData<(T, P)>,
}
impl<T: AsyncRead + AsyncWrite> NativeTlsAcceptor<T> {
impl<T: AsyncRead + AsyncWrite, P> NativeTlsAcceptor<T, P> {
/// Create `NativeTlsAcceptor` instance
pub fn new(acceptor: TlsAcceptor) -> Self {
NativeTlsAcceptor {
@@ -28,7 +28,7 @@ impl<T: AsyncRead + AsyncWrite> NativeTlsAcceptor<T> {
}
}
impl<T: AsyncRead + AsyncWrite> Clone for NativeTlsAcceptor<T> {
impl<T: AsyncRead + AsyncWrite, P> Clone for NativeTlsAcceptor<T, P> {
fn clone(&self) -> Self {
Self {
acceptor: self.acceptor.clone(),
@@ -37,11 +37,11 @@ impl<T: AsyncRead + AsyncWrite> Clone for NativeTlsAcceptor<T> {
}
}
impl<T: AsyncRead + AsyncWrite> NewService<ServerConfig> for NativeTlsAcceptor<T> {
type Request = T;
type Response = TlsStream<T>;
impl<T: AsyncRead + AsyncWrite, P> NewService<ServerConfig> for NativeTlsAcceptor<T, P> {
type Request = Io<T, P>;
type Response = Io<TlsStream<T>, P>;
type Error = Error;
type Service = NativeTlsAcceptorService<T>;
type Service = NativeTlsAcceptorService<T, P>;
type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>;
@@ -58,17 +58,17 @@ impl<T: AsyncRead + AsyncWrite> NewService<ServerConfig> for NativeTlsAcceptor<T
}
}
pub struct NativeTlsAcceptorService<T> {
pub struct NativeTlsAcceptorService<T, P> {
acceptor: TlsAcceptor,
io: PhantomData<T>,
io: PhantomData<(T, P)>,
conns: Counter,
}
impl<T: AsyncRead + AsyncWrite> Service for NativeTlsAcceptorService<T> {
type Request = T;
type Response = TlsStream<T>;
impl<T: AsyncRead + AsyncWrite, P> Service for NativeTlsAcceptorService<T, P> {
type Request = Io<T, P>;
type Response = Io<TlsStream<T>, P>;
type Error = Error;
type Future = Accept<T>;
type Future = Accept<T, P>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
if self.conns.available() {
@@ -78,10 +78,12 @@ impl<T: AsyncRead + AsyncWrite> Service for NativeTlsAcceptorService<T> {
}
}
fn call(&mut self, req: T) -> Self::Future {
fn call(&mut self, req: Self::Request) -> Self::Future {
let (io, params, _) = req.into_parts();
Accept {
_guard: self.conns.get(),
inner: Some(self.acceptor.accept(req)),
inner: Some(self.acceptor.accept(io)),
params: Some(params),
}
}
}
@@ -100,21 +102,30 @@ pub struct TlsStream<S> {
/// Future returned from `NativeTlsAcceptor::accept` which will resolve
/// once the accept handshake has finished.
pub struct Accept<S> {
pub struct Accept<S, P> {
inner: Option<Result<native_tls::TlsStream<S>, HandshakeError<S>>>,
params: Option<P>,
_guard: CounterGuard,
}
impl<Io: AsyncRead + AsyncWrite> Future for Accept<Io> {
type Item = TlsStream<Io>;
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(TlsStream { inner: stream }.into()),
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(TlsStream { inner: stream }.into()),
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)));

View File

@@ -8,17 +8,17 @@ use tokio_openssl::{AcceptAsync, SslAcceptorExt, SslStream};
use crate::counter::{Counter, CounterGuard};
use crate::ssl::MAX_CONN_COUNTER;
use crate::ServerConfig;
use crate::{Io, Protocol, ServerConfig};
/// Support `SSL` connections via openssl package
///
/// `ssl` feature enables `OpensslAcceptor` type
pub struct OpensslAcceptor<T> {
pub struct OpensslAcceptor<T: AsyncRead + AsyncWrite, P = ()> {
acceptor: SslAcceptor,
io: PhantomData<T>,
io: PhantomData<(T, P)>,
}
impl<T> OpensslAcceptor<T> {
impl<T: AsyncRead + AsyncWrite, P> OpensslAcceptor<T, P> {
/// Create default `OpensslAcceptor`
pub fn new(acceptor: SslAcceptor) -> Self {
OpensslAcceptor {
@@ -28,7 +28,7 @@ impl<T> OpensslAcceptor<T> {
}
}
impl<T: AsyncRead + AsyncWrite> Clone for OpensslAcceptor<T> {
impl<T: AsyncRead + AsyncWrite, P> Clone for OpensslAcceptor<T, P> {
fn clone(&self) -> Self {
Self {
acceptor: self.acceptor.clone(),
@@ -37,11 +37,11 @@ impl<T: AsyncRead + AsyncWrite> Clone for OpensslAcceptor<T> {
}
}
impl<T: AsyncRead + AsyncWrite> NewService<ServerConfig> for OpensslAcceptor<T> {
type Request = T;
type Response = SslStream<T>;
impl<T: AsyncRead + AsyncWrite, P> NewService<ServerConfig> for OpensslAcceptor<T, P> {
type Request = Io<T, P>;
type Response = Io<SslStream<T>, P>;
type Error = HandshakeError<T>;
type Service = OpensslAcceptorService<T>;
type Service = OpensslAcceptorService<T, P>;
type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>;
@@ -58,17 +58,17 @@ impl<T: AsyncRead + AsyncWrite> NewService<ServerConfig> for OpensslAcceptor<T>
}
}
pub struct OpensslAcceptorService<T> {
pub struct OpensslAcceptorService<T, P> {
acceptor: SslAcceptor,
io: PhantomData<T>,
conns: Counter,
io: PhantomData<(T, P)>,
}
impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> {
type Request = T;
type Response = SslStream<T>;
impl<T: AsyncRead + AsyncWrite, P> Service for OpensslAcceptorService<T, P> {
type Request = Io<T, P>;
type Response = Io<SslStream<T>, P>;
type Error = HandshakeError<T>;
type Future = OpensslAcceptorServiceFut<T>;
type Future = OpensslAcceptorServiceFut<T, P>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
if self.conns.available() {
@@ -78,27 +78,52 @@ impl<T: AsyncRead + AsyncWrite> Service for OpensslAcceptorService<T> {
}
}
fn call(&mut self, req: T) -> Self::Future {
fn call(&mut self, req: Self::Request) -> Self::Future {
let (io, params, _) = req.into_parts();
OpensslAcceptorServiceFut {
_guard: self.conns.get(),
fut: SslAcceptorExt::accept_async(&self.acceptor, req),
fut: SslAcceptorExt::accept_async(&self.acceptor, io),
params: Some(params),
}
}
}
pub struct OpensslAcceptorServiceFut<T>
pub struct OpensslAcceptorServiceFut<T, P>
where
T: AsyncRead + AsyncWrite,
{
fut: AcceptAsync<T>,
params: Option<P>,
_guard: CounterGuard,
}
impl<T: AsyncRead + AsyncWrite> Future for OpensslAcceptorServiceFut<T> {
type Item = SslStream<T>;
impl<T: AsyncRead + AsyncWrite, P> Future for OpensslAcceptorServiceFut<T, P> {
type Item = Io<SslStream<T>, P>;
type Error = HandshakeError<T>;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.fut.poll()
let io = futures::try_ready!(self.fut.poll());
let proto = if let Some(protos) = io.get_ref().ssl().selected_alpn_protocol() {
const H2: &[u8] = b"\x02h2";
const HTTP10: &[u8] = b"\x08http/1.0";
const HTTP11: &[u8] = b"\x08http/1.1";
if protos.windows(3).any(|window| window == H2) {
Protocol::Http2
} else if protos.windows(9).any(|window| window == HTTP11) {
Protocol::Http11
} else if protos.windows(9).any(|window| window == HTTP10) {
Protocol::Http10
} else {
Protocol::Unknown
}
} else {
Protocol::Unknown
};
Ok(Async::Ready(Io::from_parts(
io,
self.params.take().unwrap(),
proto,
)))
}
}

View File

@@ -10,17 +10,17 @@ use tokio_rustls::{Accept, TlsAcceptor, TlsStream};
use crate::counter::{Counter, CounterGuard};
use crate::ssl::MAX_CONN_COUNTER;
use crate::ServerConfig as SrvConfig;
use crate::{Io, Protocol, ServerConfig as SrvConfig};
/// Support `SSL` connections via rustls package
///
/// `rust-tls` feature enables `RustlsAcceptor` type
pub struct RustlsAcceptor<T> {
pub struct RustlsAcceptor<T, P = ()> {
config: Arc<ServerConfig>,
io: PhantomData<T>,
io: PhantomData<(T, P)>,
}
impl<T: AsyncRead + AsyncWrite> RustlsAcceptor<T> {
impl<T: AsyncRead + AsyncWrite, P> RustlsAcceptor<T, P> {
/// Create `RustlsAcceptor` new service
pub fn new(config: ServerConfig) -> Self {
RustlsAcceptor {
@@ -30,7 +30,7 @@ impl<T: AsyncRead + AsyncWrite> RustlsAcceptor<T> {
}
}
impl<T> Clone for RustlsAcceptor<T> {
impl<T, P> Clone for RustlsAcceptor<T, P> {
fn clone(&self) -> Self {
Self {
config: self.config.clone(),
@@ -39,11 +39,11 @@ impl<T> Clone for RustlsAcceptor<T> {
}
}
impl<T: AsyncRead + AsyncWrite> NewService<SrvConfig> for RustlsAcceptor<T> {
type Request = T;
type Response = TlsStream<T, ServerSession>;
impl<T: AsyncRead + AsyncWrite, P> NewService<SrvConfig> for RustlsAcceptor<T, P> {
type Request = Io<T, P>;
type Response = Io<TlsStream<T, ServerSession>, P>;
type Error = io::Error;
type Service = RustlsAcceptorService<T>;
type Service = RustlsAcceptorService<T, P>;
type InitError = ();
type Future = FutureResult<Self::Service, Self::InitError>;
@@ -60,17 +60,17 @@ impl<T: AsyncRead + AsyncWrite> NewService<SrvConfig> for RustlsAcceptor<T> {
}
}
pub struct RustlsAcceptorService<T> {
pub struct RustlsAcceptorService<T, P> {
acceptor: TlsAcceptor,
io: PhantomData<T>,
io: PhantomData<(T, P)>,
conns: Counter,
}
impl<T: AsyncRead + AsyncWrite> Service for RustlsAcceptorService<T> {
type Request = T;
type Response = TlsStream<T, ServerSession>;
impl<T: AsyncRead + AsyncWrite, P> Service for RustlsAcceptorService<T, P> {
type Request = Io<T, P>;
type Response = Io<TlsStream<T, ServerSession>, P>;
type Error = io::Error;
type Future = RustlsAcceptorServiceFut<T>;
type Future = RustlsAcceptorServiceFut<T, P>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
if self.conns.available() {
@@ -80,27 +80,35 @@ impl<T: AsyncRead + AsyncWrite> Service for RustlsAcceptorService<T> {
}
}
fn call(&mut self, req: T) -> Self::Future {
fn call(&mut self, req: Self::Request) -> Self::Future {
let (io, params, _) = req.into_parts();
RustlsAcceptorServiceFut {
_guard: self.conns.get(),
fut: self.acceptor.accept(req),
fut: self.acceptor.accept(io),
params: Some(params),
}
}
}
pub struct RustlsAcceptorServiceFut<T>
pub struct RustlsAcceptorServiceFut<T, P>
where
T: AsyncRead + AsyncWrite,
{
fut: Accept<T>,
params: Option<P>,
_guard: CounterGuard,
}
impl<T: AsyncRead + AsyncWrite> Future for RustlsAcceptorServiceFut<T> {
type Item = TlsStream<T, ServerSession>;
impl<T: AsyncRead + AsyncWrite, P> Future for RustlsAcceptorServiceFut<T, P> {
type Item = Io<TlsStream<T, ServerSession>, P>;
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.fut.poll()
let io = futures::try_ready!(self.fut.poll());
Ok(Async::Ready(Io::from_parts(
io,
self.params.take().unwrap(),
Protocol::Unknown,
)))
}
}

View File

@@ -1,8 +1,14 @@
use std::io::Read;
use std::sync::mpsc;
use std::{net, thread, time};
use actix_server::{Server, ServerConfig};
use actix_codec::{BytesCodec, Framed};
use actix_server::{Io, Server, ServerConfig};
use actix_service::{fn_cfg_factory, fn_service, IntoService};
use bytes::Bytes;
use futures::{Future, Sink};
use net2::TcpBuilder;
use tokio_tcp::TcpStream;
fn unused_addr() -> net::SocketAddr {
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
@@ -36,16 +42,20 @@ fn test_bind() {
#[test]
fn test_bind_no_config() {
let addr = unused_addr();
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
Server::build()
let sys = actix_rt::System::new("test");
let srv = Server::build()
.bind("test", addr, move || fn_service(|_| Ok::<_, ()>(())))
.unwrap()
.run()
.start();
let _ = tx.send((srv, actix_rt::System::current()));
let _ = sys.run();
});
thread::sleep(time::Duration::from_millis(500));
let (_, sys) = rx.recv().unwrap();
assert!(net::TcpStream::connect(addr).is_ok());
let _ = sys.stop();
}
#[test]
@@ -68,3 +78,70 @@ fn test_listen() {
thread::sleep(time::Duration::from_millis(500));
assert!(net::TcpStream::connect(addr).is_ok());
}
#[test]
#[cfg(unix)]
fn test_start() {
let addr = unused_addr();
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let sys = actix_rt::System::new("test");
let srv = Server::build()
.backlog(100)
.bind("test", addr, move || {
fn_cfg_factory(move |cfg: &ServerConfig| {
assert_eq!(cfg.local_addr(), addr);
Ok::<_, ()>(
(|io: Io<TcpStream>| {
Framed::new(io.into_parts().0, BytesCodec)
.send(Bytes::from_static(b"test"))
.then(|_| Ok::<_, ()>(()))
})
.into_service(),
)
})
})
.unwrap()
.start();
let _ = tx.send((srv, actix_rt::System::current()));
let _ = sys.run();
});
let (srv, sys) = rx.recv().unwrap();
let mut buf = [0u8; 4];
let mut conn = net::TcpStream::connect(addr).unwrap();
let _ = conn.read_exact(&mut buf);
assert_eq!(buf, b"test"[..]);
// pause
let _ = srv.pause();
thread::sleep(time::Duration::from_millis(200));
let mut conn = net::TcpStream::connect(addr).unwrap();
conn.set_read_timeout(Some(time::Duration::from_millis(100)))
.unwrap();
let res = conn.read_exact(&mut buf);
assert!(res.is_err());
// resume
let _ = srv.resume();
thread::sleep(time::Duration::from_millis(100));
assert!(net::TcpStream::connect(addr).is_ok());
assert!(net::TcpStream::connect(addr).is_ok());
assert!(net::TcpStream::connect(addr).is_ok());
let mut buf = [0u8; 4];
let mut conn = net::TcpStream::connect(addr).unwrap();
let _ = conn.read_exact(&mut buf);
assert_eq!(buf, b"test"[..]);
// stop
let _ = srv.stop(false);
thread::sleep(time::Duration::from_millis(100));
assert!(net::TcpStream::connect(addr).is_err());
thread::sleep(time::Duration::from_millis(100));
let _ = sys.stop();
}

View File

@@ -1,5 +1,20 @@
# Changes
## [0.3.4] - 2019-03-12
### Added
* Add `Transform::from_err()` combinator
* Add `apply_fn` helper
* Add `apply_fn_factory` helper
* Add `apply_transform` helper
* Add `apply_cfg` helper
## [0.3.3] - 2019-03-09
### Added

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-service"
version = "0.3.3"
version = "0.3.4"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix Service"
keywords = ["network", "framework", "async", "futures"]
@@ -27,4 +27,4 @@ futures = "0.1.24"
void = "1.0.2"
[dev-dependencies]
actix-rt = "0.1"
actix-rt = "0.2"

View File

@@ -1,6 +1,6 @@
use std::marker::PhantomData;
use futures::{try_ready, Async, Future, Poll};
use futures::{Async, Future, Poll};
use super::{IntoNewService, NewService, Service};
use crate::cell::Cell;
@@ -48,8 +48,12 @@ where
type Future = AndThenFuture<A, B>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.a.poll_ready());
self.b.get_mut().poll_ready()
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 {
@@ -107,19 +111,23 @@ where
}
/// `AndThenNewService` new service combinator
pub struct AndThenNewService<A, B, C> {
pub struct AndThenNewService<A, B, C>
where
A: NewService<C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
{
a: A,
b: B,
_t: PhantomData<C>,
}
impl<A, B, C> AndThenNewService<A, B, C> {
impl<A, B, C> AndThenNewService<A, B, C>
where
A: NewService<C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
{
/// Create new `AndThen` combinator
pub fn new<F: IntoNewService<B, C>>(a: A, f: F) -> Self
where
A: NewService<C>,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError>,
{
pub fn new<F: IntoNewService<B, C>>(a: A, f: F) -> Self {
Self {
a,
b: f.into_new_service(),
@@ -148,8 +156,8 @@ where
impl<A, B, C> Clone for AndThenNewService<A, B, C>
where
A: Clone,
B: Clone,
A: NewService<C> + Clone,
B: NewService<C, Request = A::Response, Error = A::Error, InitError = A::InitError> + Clone,
{
fn clone(&self) -> Self {
Self {

View File

@@ -1,6 +1,6 @@
use std::marker::PhantomData;
use futures::{try_ready, Async, Future, IntoFuture, Poll};
use futures::{Async, Future, IntoFuture, Poll};
use super::{IntoNewService, IntoService, NewService, Service};
use crate::cell::Cell;
@@ -71,8 +71,12 @@ where
type Future = AndThenApplyFuture<A, B, F, Out>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.a.poll_ready());
self.b.get_mut().poll_ready()
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 {

View File

@@ -4,6 +4,34 @@ use futures::{Async, Future, IntoFuture, Poll};
use super::{IntoNewService, IntoService, NewService, 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>
where
T: Service,
F: FnMut(In, &mut T) -> Out,
Out: IntoFuture,
Out::Error: From<T::Error>,
U: IntoService<T>,
{
Apply::new(service.into_service(), f)
}
/// Create fractory for `apply_fn` service.
pub fn apply_fn_factory<T, F, In, Out, Cfg, U>(
service: U,
f: F,
) -> ApplyNewService<T, F, In, Out, Cfg>
where
T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
U: IntoNewService<T, Cfg>,
{
ApplyNewService::new(service.into_new_service(), f)
}
#[doc(hidden)]
/// `Apply` service combinator
pub struct Apply<T, F, In, Out>
where

View File

@@ -1,121 +1,70 @@
use std::marker::PhantomData;
use futures::{Async, Future, Poll};
use crate::and_then::AndThen;
use crate::{IntoNewService, NewService};
/// `ApplyNewService` new service combinator
pub struct ApplyConfig<F, A, B, C1, C2> {
a: A,
b: B,
/// Create new ApplyConfig` service factory combinator
pub fn apply_cfg<F, S, C1, C2, U>(f: F, service: U) -> ApplyConfig<F, S, C1, C2>
where
S: NewService<C2>,
F: Fn(&C1) -> C2,
U: IntoNewService<S, C2>,
{
ApplyConfig::new(service.into_new_service(), f)
}
/// `ApplyConfig` service factory combinator
pub struct ApplyConfig<F, S, C1, C2> {
s: S,
f: F,
r: PhantomData<(C1, C2)>,
}
impl<F, A, B, C1, C2> ApplyConfig<F, A, B, C1, C2>
impl<F, S, C1, C2> ApplyConfig<F, S, C1, C2>
where
A: NewService<C1>,
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
S: NewService<C2>,
F: Fn(&C1) -> C2,
{
/// Create new `ApplyNewService` new service instance
pub fn new<A1: IntoNewService<A, C1>, B1: IntoNewService<B, C2>>(
a: A1,
b: B1,
f: F,
) -> Self {
/// Create new ApplyConfig` service factory combinator
pub fn new<U: IntoNewService<S, C2>>(a: U, f: F) -> Self {
Self {
f,
a: a.into_new_service(),
b: b.into_new_service(),
s: a.into_new_service(),
r: PhantomData,
}
}
}
impl<F, A, B, C1, C2> Clone for ApplyConfig<F, A, B, C1, C2>
impl<F, S, C1, C2> Clone for ApplyConfig<F, S, C1, C2>
where
A: Clone,
B: Clone,
S: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
a: self.a.clone(),
b: self.b.clone(),
s: self.s.clone(),
f: self.f.clone(),
r: PhantomData,
}
}
}
impl<F, A, B, C1, C2> NewService<C1> for ApplyConfig<F, A, B, C1, C2>
impl<F, S, C1, C2> NewService<C1> for ApplyConfig<F, S, C1, C2>
where
A: NewService<C1>,
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
S: NewService<C2>,
F: Fn(&C1) -> C2,
{
type Request = A::Request;
type Response = B::Response;
type Error = A::Error;
type Service = AndThen<A::Service, B::Service>;
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S::Service;
type InitError = A::InitError;
type Future = ApplyConfigResponse<A, B, C1, C2>;
type InitError = S::InitError;
type Future = S::Future;
fn new_service(&self, cfg: &C1) -> Self::Future {
let cfg2 = (self.f)(cfg);
ApplyConfigResponse {
a: None,
b: None,
fut_a: self.a.new_service(cfg),
fut_b: self.b.new_service(&cfg2),
}
}
}
pub struct ApplyConfigResponse<A, B, C1, C2>
where
A: NewService<C1>,
B: NewService<C2>,
{
fut_b: B::Future,
fut_a: A::Future,
a: Option<A::Service>,
b: Option<B::Service>,
}
impl<A, B, C1, C2> Future for ApplyConfigResponse<A, B, C1, C2>
where
A: NewService<C1>,
B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
{
type Item = AndThen<A::Service, B::Service>;
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(AndThen::new(
self.a.take().unwrap(),
self.b.take().unwrap(),
)))
} else {
Ok(Async::NotReady)
}
self.s.new_service(&cfg2)
}
}

View File

@@ -5,6 +5,19 @@ use futures::IntoFuture;
use crate::{Apply, IntoTransform, Service, Transform};
/// Use function as transform service
pub fn fn_transform<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,

View File

@@ -21,21 +21,35 @@ mod map_err;
mod map_init_err;
mod then;
mod transform;
mod transform_map_init_err;
mod transform_err;
#[doc(hidden)]
#[deprecated(since = "0.3.4", note = "please use `apply_fn` instead")]
pub use self::apply::Apply;
#[doc(hidden)]
#[deprecated(since = "0.3.4", note = "please use `apply_fn_factory` instead")]
pub use self::apply::ApplyNewService;
#[doc(hidden)]
#[deprecated(since = "0.3.4", note = "please use `fn_transform` instead")]
pub use self::fn_transform::FnTransform;
#[doc(hidden)]
#[deprecated(since = "0.3.4", note = "please use `apply_transform` instead")]
pub use self::transform::ApplyTransform;
pub use self::and_then::{AndThen, AndThenNewService};
use self::and_then_apply::AndThenTransform;
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
pub use self::apply::{Apply, ApplyNewService};
pub use self::apply_cfg::ApplyConfig;
pub use self::apply::{apply_fn, apply_fn_factory};
pub use self::apply_cfg::apply_cfg;
use self::apply_cfg::ApplyConfig;
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
pub use self::fn_transform::FnTransform;
pub use self::fn_transform::fn_transform;
pub use self::from_err::{FromErr, FromErrNewService};
pub use self::map::{Map, MapNewService};
pub use self::map_err::{MapErr, MapErrNewService};
pub use self::map_init_err::MapInitErr;
pub use self::then::{Then, ThenNewService};
pub use self::transform::{ApplyTransform, IntoTransform, Transform};
pub use self::transform::{apply_transform, IntoTransform, Transform};
/// An asynchronous function from `Request` to a `Response`.
pub trait Service {
@@ -244,19 +258,23 @@ pub trait NewService<Config = ()> {
/// Map this service's config type to a different config,
/// and use for nested service
fn apply_cfg<F, C, B, B1>(self, service: B1, f: F) -> ApplyConfig<F, Self, B, Config, C>
fn apply_cfg<F, C, S, U>(
self,
service: U,
f: F,
) -> AndThenNewService<Self, ApplyConfig<F, S, Config, C>, Config>
where
Self: Sized,
F: Fn(&Config) -> C,
B1: IntoNewService<B, C>,
B: NewService<
U: IntoNewService<S, C>,
S: NewService<
C,
Request = Self::Response,
Error = Self::Error,
InitError = Self::InitError,
>,
{
ApplyConfig::new(self, service, f)
self.and_then(ApplyConfig::new(service, f))
}
/// Call another service after call to this one has resolved successfully.
@@ -322,7 +340,7 @@ pub trait NewService<Config = ()> {
fn map_err<F, E>(self, f: F) -> MapErrNewService<Self, F, E, Config>
where
Self: Sized,
F: Fn(Self::Error) -> E,
F: Fn(Self::Error) -> E + Clone,
{
MapErrNewService::new(self, f)
}

View File

@@ -98,19 +98,23 @@ where
/// service's error.
///
/// This is created by the `NewServiceExt::map_err` method.
pub struct MapErrNewService<A, F, E, C> {
pub struct MapErrNewService<A, F, E, C>
where
A: NewService<C>,
F: Fn(A::Error) -> E + Clone,
{
a: A,
f: F,
e: PhantomData<(E, C)>,
}
impl<A, F, E, C> MapErrNewService<A, F, E, C> {
impl<A, F, E, C> MapErrNewService<A, F, E, C>
where
A: NewService<C>,
F: Fn(A::Error) -> E + Clone,
{
/// Create new `MapErr` new service instance
pub fn new(a: A, f: F) -> Self
where
A: NewService<C>,
F: Fn(A::Error) -> E,
{
pub fn new(a: A, f: F) -> Self {
Self {
a,
f,
@@ -121,8 +125,8 @@ impl<A, F, E, C> MapErrNewService<A, F, E, C> {
impl<A, F, E, C> Clone for MapErrNewService<A, F, E, C>
where
A: Clone,
F: Clone,
A: NewService<C> + Clone,
F: Fn(A::Error) -> E + Clone,
{
fn clone(&self) -> Self {
Self {

View File

@@ -1,6 +1,6 @@
use std::marker::PhantomData;
use futures::{try_ready, Async, Future, Poll};
use futures::{Async, Future, Poll};
use super::{IntoNewService, NewService, Service};
use crate::cell::Cell;
@@ -48,8 +48,12 @@ where
type Future = ThenFuture<A, B>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.a.poll_ready());
self.b.get_mut().poll_ready()
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 {

View File

@@ -3,8 +3,8 @@ use std::sync::Arc;
use futures::{Async, Future, IntoFuture, Poll};
use crate::transform_map_init_err::TransformMapInitErr;
use crate::{NewService, Service};
use crate::transform_err::{TransformFromErr, TransformMapInitErr};
use crate::{IntoNewService, NewService, Service};
/// `Transform` service factory.
///
@@ -36,7 +36,7 @@ pub trait Transform<S> {
/// Create and return a new service value asynchronously.
fn new_transform(&self, service: S) -> Self::Future;
/// Map this service's factory init error to a different error,
/// Map this service's factory error to a different error,
/// returning a new transform service factory.
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, F, E>
where
@@ -45,6 +45,32 @@ pub trait Transform<S> {
{
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>
@@ -97,33 +123,65 @@ where
}
}
/// `Apply` transform new service
#[derive(Clone)]
pub struct ApplyTransform<T, A, C> {
a: A,
/// Apply transform to service factory. Function returns
/// services factory that in initialization creates
/// service and applies transform to this service.
pub fn apply_transform<T, S, C, F, U>(
t: F,
service: U,
) -> impl NewService<
C,
Request = T::Request,
Response = T::Response,
Error = T::Error,
Service = T::Transform,
InitError = S::InitError,
> + Clone
where
S: NewService<C>,
T: Transform<S::Service, InitError = S::InitError>,
F: IntoTransform<T, S::Service>,
U: IntoNewService<S, C>,
{
ApplyTransform::new(t.into_transform(), service.into_new_service())
}
/// `Apply` transform to new service
pub struct ApplyTransform<T, S, C> {
s: Rc<S>,
t: Rc<T>,
_t: std::marker::PhantomData<C>,
}
impl<T, A, C> ApplyTransform<T, A, C>
impl<T, S, C> ApplyTransform<T, S, C>
where
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
S: NewService<C>,
T: Transform<S::Service, InitError = S::InitError>,
{
/// Create new `ApplyNewService` new service instance
pub fn new<F: IntoTransform<T, A::Service>>(t: F, a: A) -> Self {
/// Create new `ApplyTransform` new service instance
pub fn new<F: IntoTransform<T, S::Service>>(t: F, service: S) -> Self {
Self {
a,
s: Rc::new(service),
t: Rc::new(t.into_transform()),
_t: std::marker::PhantomData,
}
}
}
impl<T, A, C> NewService<C> for ApplyTransform<T, A, C>
impl<T, S, C> Clone for ApplyTransform<T, S, C> {
fn clone(&self) -> Self {
ApplyTransform {
s: self.s.clone(),
t: self.t.clone(),
_t: std::marker::PhantomData,
}
}
}
impl<T, S, C> NewService<C> for ApplyTransform<T, S, C>
where
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
S: NewService<C>,
T: Transform<S::Service, InitError = S::InitError>,
{
type Request = T::Request;
type Response = T::Response;
@@ -131,31 +189,31 @@ where
type Service = T::Transform;
type InitError = T::InitError;
type Future = ApplyTransformFuture<T, A, C>;
type Future = ApplyTransformFuture<T, S, C>;
fn new_service(&self, cfg: &C) -> Self::Future {
ApplyTransformFuture {
t_cell: self.t.clone(),
fut_a: self.a.new_service(cfg).into_future(),
fut_a: self.s.new_service(cfg).into_future(),
fut_t: None,
}
}
}
pub struct ApplyTransformFuture<T, A, C>
pub struct ApplyTransformFuture<T, S, C>
where
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
S: NewService<C>,
T: Transform<S::Service, InitError = S::InitError>,
{
fut_a: A::Future,
fut_a: S::Future,
fut_t: Option<<T::Future as IntoFuture>::Future>,
t_cell: Rc<T>,
}
impl<T, A, C> Future for ApplyTransformFuture<T, A, C>
impl<T, S, C> Future for ApplyTransformFuture<T, S, C>
where
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
S: NewService<C>,
T: Transform<S::Service, InitError = S::InitError>,
{
type Item = T::Transform;
type Error = T::InitError;

View File

@@ -0,0 +1,162 @@
use std::marker::PhantomData;
use futures::{Future, Poll};
use super::Transform;
/// Transform for the `map_err` combinator, changing the type of a new
/// transform's init error.
///
/// This is created by the `Transform::map_err` method.
pub struct TransformMapInitErr<T, S, F, E> {
t: T,
f: F,
e: PhantomData<(S, E)>,
}
impl<T, S, F, E> TransformMapInitErr<T, S, F, E> {
/// Create new `TransformMapErr` new transform instance
pub fn new(t: T, f: F) -> Self
where
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
Self {
t,
f,
e: PhantomData,
}
}
}
impl<T, S, F, E> Clone for TransformMapInitErr<T, S, F, E>
where
T: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
t: self.t.clone(),
f: self.f.clone(),
e: PhantomData,
}
}
}
impl<T, S, F, E> Transform<S> for TransformMapInitErr<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::InitError) -> E + Clone,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Transform = T::Transform;
type InitError = E;
type Future = TransformMapInitErrFuture<T, S, F, E>;
fn new_transform(&self, service: S) -> Self::Future {
TransformMapInitErrFuture {
fut: self.t.new_transform(service),
f: self.f.clone(),
}
}
}
pub struct TransformMapInitErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
fut: T::Future,
f: F,
}
impl<T, S, F, E> Future for TransformMapInitErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::InitError) -> E + Clone,
{
type Item = T::Transform;
type Error = E;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.fut.poll().map_err(&self.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)
}
}

View File

@@ -1,94 +0,0 @@
use std::marker::PhantomData;
use futures::{Future, Poll};
use super::Transform;
/// NewTransform for the `map_init_err` combinator, changing the type of a new
/// transform's error.
///
/// This is created by the `NewTransform::map_init_err` method.
pub struct TransformMapInitErr<T, S, F, E> {
t: T,
f: F,
e: PhantomData<(S, E)>,
}
impl<T, S, F, E> TransformMapInitErr<T, S, F, E> {
/// Create new `MapInitErr` new transform instance
pub fn new(t: T, f: F) -> Self
where
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
Self {
t,
f,
e: PhantomData,
}
}
}
impl<T, S, F, E> Clone for TransformMapInitErr<T, S, F, E>
where
T: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
t: self.t.clone(),
f: self.f.clone(),
e: PhantomData,
}
}
}
impl<T, S, F, E> Transform<S> for TransformMapInitErr<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::InitError) -> E + Clone,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Transform = T::Transform;
type InitError = E;
type Future = TransformMapInitErrFuture<T, S, F, E>;
fn new_transform(&self, service: S) -> Self::Future {
TransformMapInitErrFuture::new(self.t.new_transform(service), self.f.clone())
}
}
pub struct TransformMapInitErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
fut: T::Future,
f: F,
}
impl<T, S, F, E> TransformMapInitErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
fn new(fut: T::Future, f: F) -> Self {
TransformMapInitErrFuture { f, fut }
}
}
impl<T, S, F, E> Future for TransformMapInitErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::InitError) -> E + Clone,
{
type Item = T::Transform;
type Error = E;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.fut.poll().map_err(&self.f)
}
}

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-test-server"
version = "0.1.0"
version = "0.2.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix test server"
keywords = ["network", "framework", "async", "futures"]
@@ -33,9 +33,8 @@ ssl = ["openssl", "actix-server/ssl"]
rust-tls = ["rustls", "tokio-rustls", "webpki", "webpki-roots"]
[dependencies]
actix-rt = "0.2.0"
# actix-server = "0.3.3"
actix-server = { path="../actix-server" }
actix-rt = "0.2.1"
actix-server = "0.4.0"
log = "0.4"
net2 = "0.2"
@@ -56,4 +55,4 @@ webpki = { version = "0.19", optional = true }
webpki-roots = { version = "0.16", optional = true }
[dev-dependencies]
actix-service = "0.3.3"
actix-service = "0.3.4"

View File

@@ -1,5 +1,18 @@
# Changes
## [0.3.4] - 2019-03-12
### Changed
* `TimeoutService`, `InOrderService`, `InFlightService` accepts generic IntoService services.
### Fixed
* Fix `InFlightService::poll_ready()` nested service readiness check
* Fix `InOrderService::poll_ready()` nested service readiness check
## [0.3.3] - 2019-03-09
### Changed

View File

@@ -1,6 +1,6 @@
[package]
name = "actix-utils"
version = "0.3.3"
version = "0.3.4"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix utils - various actix net related services"
keywords = ["network", "framework", "async", "futures"]
@@ -27,4 +27,4 @@ tokio-current-thread = "0.1.4"
log = "0.4"
[dev-dependencies]
actix-rt = "0.2"
actix-rt = "0.2.1"

View File

@@ -221,7 +221,7 @@ where
fn poll_read(&mut self) -> bool {
loop {
match self.service.poll_ready() {
Ok(Async::Ready(_)) => loop {
Ok(Async::Ready(_)) => {
let item = match self.framed.poll() {
Ok(Async::Ready(Some(el))) => el,
Err(err) => {
@@ -244,7 +244,7 @@ where
inner.task.notify();
Ok(())
}));
},
}
Ok(Async::NotReady) => return false,
Err(err) => {
self.state = TransportState::Error(FramedTransportError::Service(err));

View File

@@ -1,4 +1,4 @@
use actix_service::{Service, Transform, Void};
use actix_service::{IntoService, Service, Transform, Void};
use futures::future::{ok, FutureResult};
use futures::{Async, Future, Poll};
@@ -42,11 +42,17 @@ pub struct InFlightService<S> {
service: S,
}
impl<S> InFlightService<S> {
pub fn new(max: usize, service: S) -> Self {
impl<S> InFlightService<S>
where
S: Service,
{
pub fn new<U>(max: usize, service: U) -> Self
where
U: IntoService<S>,
{
Self {
service,
count: Counter::new(max),
service: service.into_service(),
}
}
}
@@ -61,9 +67,9 @@ where
type Future = InFlightServiceResponse<T>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready()?;
if !self.count.available() {
if let Async::NotReady = self.service.poll_ready()? {
Ok(Async::NotReady)
} else if !self.count.available() {
log::trace!("InFlight limit exceeded");
Ok(Async::NotReady)
} else {

View File

@@ -3,7 +3,7 @@ use std::fmt;
use std::marker::PhantomData;
use std::rc::Rc;
use actix_service::{Service, Transform, Void};
use actix_service::{IntoService, Service, Transform, Void};
use futures::future::{ok, FutureResult};
use futures::task::AtomicTask;
use futures::unsync::oneshot;
@@ -112,9 +112,12 @@ where
S::Future: 'static,
S::Error: 'static,
{
pub fn new(service: S) -> Self {
pub fn new<U>(service: U) -> Self
where
U: IntoService<S>,
{
Self {
service,
service: service.into_service(),
acks: VecDeque::new(),
task: Rc::new(AtomicTask::new()),
}
@@ -137,9 +140,6 @@ where
// poll_ready could be called from different task
self.task.register();
// check nested service
self.service.poll_ready().map_err(InOrderError::Service)?;
// check acks
while !self.acks.is_empty() {
let rec = self.acks.front_mut().unwrap();
@@ -153,7 +153,12 @@ where
}
}
Ok(Async::Ready(()))
// check nested service
if let Async::NotReady = self.service.poll_ready().map_err(InOrderError::Service)? {
Ok(Async::NotReady)
} else {
Ok(Async::Ready(()))
}
}
fn call(&mut self, request: S::Request) -> Self::Future {

View File

@@ -6,7 +6,7 @@ use std::fmt;
use std::marker::PhantomData;
use std::time::Duration;
use actix_service::{Service, Transform};
use actix_service::{IntoService, Service, Transform};
use futures::future::{ok, FutureResult};
use futures::{Async, Future, Poll};
use tokio_timer::{clock, Delay};
@@ -106,9 +106,18 @@ pub struct TimeoutService<S> {
timeout: Duration,
}
impl<S> TimeoutService<S> {
pub fn new(timeout: Duration, service: S) -> Self {
TimeoutService { service, timeout }
impl<S> TimeoutService<S>
where
S: Service,
{
pub fn new<U>(timeout: Duration, service: U) -> Self
where
U: IntoService<S>,
{
TimeoutService {
timeout,
service: service.into_service(),
}
}
}

View File

@@ -9,7 +9,7 @@ use std::{env, fmt, io};
use actix_codec::{AsyncRead, AsyncWrite};
use actix_rt::System;
use actix_server::Server;
use actix_server::{Io, Server};
use actix_service::{fn_service, NewService};
use futures::{future, Future};
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
@@ -54,8 +54,8 @@ fn main() -> io::Result<()> {
let acceptor = acceptor.clone();
// service for converting incoming TcpStream to a SslStream<TcpStream>
fn_service(move |stream: tokio_tcp::TcpStream| {
SslAcceptorExt::accept_async(&acceptor, stream)
fn_service(move |stream: Io<tokio_tcp::TcpStream>| {
SslAcceptorExt::accept_async(&acceptor, stream.into_parts().0)
.map_err(|e| println!("Openssl error: {}", e))
})
// .and_then() combinator uses other service to convert incoming `Request` to a