2018-09-28 02:15:38 +02:00
|
|
|
use std::time::Duration;
|
2018-09-28 21:35:09 +02:00
|
|
|
use std::{fmt, net};
|
2018-09-28 02:15:38 +02:00
|
|
|
|
|
|
|
use actix_net::server::ServerMessage;
|
|
|
|
use actix_net::service::{NewService, Service};
|
|
|
|
use futures::future::{err, ok, Either, FutureResult};
|
|
|
|
use futures::{Async, Future, Poll};
|
|
|
|
use tokio_reactor::Handle;
|
|
|
|
use tokio_tcp::TcpStream;
|
|
|
|
use tokio_timer::{sleep, Delay};
|
|
|
|
|
2018-10-03 00:25:32 +02:00
|
|
|
use super::channel::HttpProtocol;
|
2018-09-28 20:50:47 +02:00
|
|
|
use super::error::AcceptorError;
|
2018-09-28 02:15:38 +02:00
|
|
|
use super::handler::HttpHandler;
|
2018-10-03 00:25:32 +02:00
|
|
|
use super::settings::ServiceConfig;
|
2018-09-28 02:15:38 +02:00
|
|
|
use super::IoStream;
|
|
|
|
|
|
|
|
/// This trait indicates types that can create acceptor service for http server.
|
|
|
|
pub trait AcceptorServiceFactory: Send + Clone + 'static {
|
|
|
|
type Io: IoStream + Send;
|
2018-09-28 20:50:47 +02:00
|
|
|
type NewService: NewService<Request = TcpStream, Response = Self::Io>;
|
2018-09-28 02:15:38 +02:00
|
|
|
|
|
|
|
fn create(&self) -> Self::NewService;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, T> AcceptorServiceFactory for F
|
|
|
|
where
|
|
|
|
F: Fn() -> T + Send + Clone + 'static,
|
|
|
|
T::Response: IoStream + Send,
|
2018-09-28 20:50:47 +02:00
|
|
|
T: NewService<Request = TcpStream>,
|
2018-09-28 21:35:09 +02:00
|
|
|
T::InitError: fmt::Debug,
|
2018-09-28 02:15:38 +02:00
|
|
|
{
|
|
|
|
type Io = T::Response;
|
|
|
|
type NewService = T;
|
|
|
|
|
|
|
|
fn create(&self) -> T {
|
|
|
|
(self)()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
/// Default acceptor service convert `TcpStream` to a `tokio_tcp::TcpStream`
|
|
|
|
pub(crate) struct DefaultAcceptor;
|
|
|
|
|
|
|
|
impl AcceptorServiceFactory for DefaultAcceptor {
|
|
|
|
type Io = TcpStream;
|
|
|
|
type NewService = DefaultAcceptor;
|
|
|
|
|
|
|
|
fn create(&self) -> Self::NewService {
|
|
|
|
DefaultAcceptor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NewService for DefaultAcceptor {
|
|
|
|
type Request = TcpStream;
|
|
|
|
type Response = TcpStream;
|
|
|
|
type Error = ();
|
|
|
|
type InitError = ();
|
|
|
|
type Service = DefaultAcceptor;
|
|
|
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
|
|
|
ok(DefaultAcceptor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Service for DefaultAcceptor {
|
|
|
|
type Request = TcpStream;
|
|
|
|
type Response = TcpStream;
|
|
|
|
type Error = ();
|
|
|
|
type Future = FutureResult<Self::Response, Self::Error>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
|
|
ok(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 20:50:47 +02:00
|
|
|
pub(crate) struct TcpAcceptor<T> {
|
2018-09-28 02:15:38 +02:00
|
|
|
inner: T,
|
|
|
|
}
|
|
|
|
|
2018-09-28 20:50:47 +02:00
|
|
|
impl<T, E> TcpAcceptor<T>
|
2018-09-28 02:15:38 +02:00
|
|
|
where
|
2018-09-28 20:50:47 +02:00
|
|
|
T: NewService<Request = TcpStream, Error = AcceptorError<E>>,
|
2018-09-28 21:35:09 +02:00
|
|
|
T::InitError: fmt::Debug,
|
2018-09-28 02:15:38 +02:00
|
|
|
{
|
2018-09-28 20:50:47 +02:00
|
|
|
pub(crate) fn new(inner: T) -> Self {
|
|
|
|
TcpAcceptor { inner }
|
2018-09-28 02:15:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 20:50:47 +02:00
|
|
|
impl<T, E> NewService for TcpAcceptor<T>
|
2018-09-28 02:15:38 +02:00
|
|
|
where
|
2018-09-28 20:50:47 +02:00
|
|
|
T: NewService<Request = TcpStream, Error = AcceptorError<E>>,
|
2018-09-28 21:35:09 +02:00
|
|
|
T::InitError: fmt::Debug,
|
2018-09-28 02:15:38 +02:00
|
|
|
{
|
2018-09-28 20:50:47 +02:00
|
|
|
type Request = net::TcpStream;
|
|
|
|
type Response = T::Response;
|
|
|
|
type Error = AcceptorError<E>;
|
|
|
|
type InitError = T::InitError;
|
|
|
|
type Service = TcpAcceptorService<T::Service>;
|
|
|
|
type Future = TcpAcceptorResponse<T>;
|
2018-09-28 02:15:38 +02:00
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
|
|
|
TcpAcceptorResponse {
|
|
|
|
fut: self.inner.new_service(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 20:50:47 +02:00
|
|
|
pub(crate) struct TcpAcceptorResponse<T>
|
2018-09-28 02:15:38 +02:00
|
|
|
where
|
|
|
|
T: NewService<Request = TcpStream>,
|
2018-09-28 21:35:09 +02:00
|
|
|
T::InitError: fmt::Debug,
|
2018-09-28 02:15:38 +02:00
|
|
|
{
|
|
|
|
fut: T::Future,
|
|
|
|
}
|
|
|
|
|
2018-09-28 20:50:47 +02:00
|
|
|
impl<T> Future for TcpAcceptorResponse<T>
|
2018-09-28 02:15:38 +02:00
|
|
|
where
|
|
|
|
T: NewService<Request = TcpStream>,
|
2018-09-28 21:35:09 +02:00
|
|
|
T::InitError: fmt::Debug,
|
2018-09-28 02:15:38 +02:00
|
|
|
{
|
2018-09-28 20:50:47 +02:00
|
|
|
type Item = TcpAcceptorService<T::Service>;
|
|
|
|
type Error = T::InitError;
|
2018-09-28 02:15:38 +02:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2018-09-28 21:35:09 +02:00
|
|
|
match self.fut.poll() {
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
|
|
Ok(Async::Ready(service)) => {
|
2018-09-28 20:50:47 +02:00
|
|
|
Ok(Async::Ready(TcpAcceptorService { inner: service }))
|
|
|
|
}
|
2018-09-28 21:35:09 +02:00
|
|
|
Err(e) => {
|
|
|
|
error!("Can not create accetor service: {:?}", e);
|
|
|
|
Err(e)
|
|
|
|
}
|
2018-09-28 02:15:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 20:50:47 +02:00
|
|
|
pub(crate) struct TcpAcceptorService<T> {
|
2018-09-28 02:15:38 +02:00
|
|
|
inner: T,
|
|
|
|
}
|
|
|
|
|
2018-09-28 20:50:47 +02:00
|
|
|
impl<T, E> Service for TcpAcceptorService<T>
|
2018-09-28 02:15:38 +02:00
|
|
|
where
|
2018-09-28 20:50:47 +02:00
|
|
|
T: Service<Request = TcpStream, Error = AcceptorError<E>>,
|
2018-09-28 02:15:38 +02:00
|
|
|
{
|
2018-09-28 20:50:47 +02:00
|
|
|
type Request = net::TcpStream;
|
|
|
|
type Response = T::Response;
|
|
|
|
type Error = AcceptorError<E>;
|
|
|
|
type Future = Either<T::Future, FutureResult<Self::Response, Self::Error>>;
|
2018-09-28 02:15:38 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2018-09-28 20:50:47 +02:00
|
|
|
self.inner.poll_ready()
|
2018-09-28 02:15:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
2018-09-28 20:50:47 +02:00
|
|
|
let stream = TcpStream::from_std(req, &Handle::default()).map_err(|e| {
|
|
|
|
error!("Can not convert to an async tcp stream: {}", e);
|
|
|
|
AcceptorError::Io(e)
|
|
|
|
});
|
|
|
|
|
|
|
|
match stream {
|
|
|
|
Ok(stream) => Either::A(self.inner.call(stream)),
|
|
|
|
Err(e) => Either::B(err(e)),
|
2018-09-28 02:15:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 04:12:08 +02:00
|
|
|
#[doc(hidden)]
|
2018-09-28 02:15:38 +02:00
|
|
|
/// Acceptor timeout middleware
|
|
|
|
///
|
|
|
|
/// Applies timeout to request prcoessing.
|
2018-10-03 04:12:08 +02:00
|
|
|
pub struct AcceptorTimeout<T> {
|
2018-09-28 02:15:38 +02:00
|
|
|
inner: T,
|
2018-10-02 20:18:59 +02:00
|
|
|
timeout: Duration,
|
2018-09-28 02:15:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: NewService> AcceptorTimeout<T> {
|
2018-10-02 05:04:16 +02:00
|
|
|
pub(crate) fn new(timeout: u64, inner: T) -> Self {
|
2018-10-02 20:18:59 +02:00
|
|
|
Self {
|
|
|
|
inner,
|
|
|
|
timeout: Duration::from_millis(timeout),
|
|
|
|
}
|
2018-09-28 02:15:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: NewService> NewService for AcceptorTimeout<T> {
|
|
|
|
type Request = T::Request;
|
|
|
|
type Response = T::Response;
|
2018-09-28 20:50:47 +02:00
|
|
|
type Error = AcceptorError<T::Error>;
|
2018-09-28 02:15:38 +02:00
|
|
|
type InitError = T::InitError;
|
|
|
|
type Service = AcceptorTimeoutService<T::Service>;
|
|
|
|
type Future = AcceptorTimeoutFut<T>;
|
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
|
|
|
AcceptorTimeoutFut {
|
|
|
|
fut: self.inner.new_service(),
|
|
|
|
timeout: self.timeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2018-10-03 04:12:08 +02:00
|
|
|
pub struct AcceptorTimeoutFut<T: NewService> {
|
2018-09-28 02:15:38 +02:00
|
|
|
fut: T::Future,
|
2018-10-02 20:18:59 +02:00
|
|
|
timeout: Duration,
|
2018-09-28 02:15:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: NewService> Future for AcceptorTimeoutFut<T> {
|
|
|
|
type Item = AcceptorTimeoutService<T::Service>;
|
|
|
|
type Error = T::InitError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
let inner = try_ready!(self.fut.poll());
|
|
|
|
Ok(Async::Ready(AcceptorTimeoutService {
|
|
|
|
inner,
|
2018-10-02 05:04:16 +02:00
|
|
|
timeout: self.timeout,
|
2018-09-28 02:15:38 +02:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 04:12:08 +02:00
|
|
|
#[doc(hidden)]
|
2018-09-28 02:15:38 +02:00
|
|
|
/// Acceptor timeout service
|
|
|
|
///
|
|
|
|
/// Applies timeout to request prcoessing.
|
2018-10-03 04:12:08 +02:00
|
|
|
pub struct AcceptorTimeoutService<T> {
|
2018-09-28 02:15:38 +02:00
|
|
|
inner: T,
|
2018-10-02 20:18:59 +02:00
|
|
|
timeout: Duration,
|
2018-09-28 02:15:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Service> Service for AcceptorTimeoutService<T> {
|
|
|
|
type Request = T::Request;
|
|
|
|
type Response = T::Response;
|
2018-09-28 20:50:47 +02:00
|
|
|
type Error = AcceptorError<T::Error>;
|
2018-09-28 02:15:38 +02:00
|
|
|
type Future = AcceptorTimeoutResponse<T>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2018-09-28 20:50:47 +02:00
|
|
|
self.inner.poll_ready().map_err(AcceptorError::Service)
|
2018-09-28 02:15:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
|
|
AcceptorTimeoutResponse {
|
|
|
|
fut: self.inner.call(req),
|
2018-10-02 20:18:59 +02:00
|
|
|
sleep: sleep(self.timeout),
|
2018-09-28 02:15:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 04:12:08 +02:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct AcceptorTimeoutResponse<T: Service> {
|
2018-09-28 02:15:38 +02:00
|
|
|
fut: T::Future,
|
|
|
|
sleep: Delay,
|
|
|
|
}
|
2018-10-03 04:12:08 +02:00
|
|
|
|
2018-09-28 02:15:38 +02:00
|
|
|
impl<T: Service> Future for AcceptorTimeoutResponse<T> {
|
|
|
|
type Item = T::Response;
|
2018-09-28 20:50:47 +02:00
|
|
|
type Error = AcceptorError<T::Error>;
|
2018-09-28 02:15:38 +02:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2018-09-28 20:50:47 +02:00
|
|
|
match self.fut.poll().map_err(AcceptorError::Service)? {
|
|
|
|
Async::NotReady => match self.sleep.poll() {
|
|
|
|
Err(_) => Err(AcceptorError::Timeout),
|
|
|
|
Ok(Async::Ready(_)) => Err(AcceptorError::Timeout),
|
2018-09-28 02:15:38 +02:00
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
|
|
},
|
2018-09-28 20:50:47 +02:00
|
|
|
Async::Ready(resp) => Ok(Async::Ready(resp)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct ServerMessageAcceptor<T, H: HttpHandler> {
|
|
|
|
inner: T,
|
2018-10-03 00:25:32 +02:00
|
|
|
settings: ServiceConfig<H>,
|
2018-09-28 20:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, H> ServerMessageAcceptor<T, H>
|
|
|
|
where
|
|
|
|
H: HttpHandler,
|
|
|
|
T: NewService<Request = net::TcpStream>,
|
|
|
|
{
|
2018-10-03 00:25:32 +02:00
|
|
|
pub(crate) fn new(settings: ServiceConfig<H>, inner: T) -> Self {
|
2018-09-28 20:50:47 +02:00
|
|
|
ServerMessageAcceptor { inner, settings }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, H> NewService for ServerMessageAcceptor<T, H>
|
|
|
|
where
|
|
|
|
H: HttpHandler,
|
|
|
|
T: NewService<Request = net::TcpStream>,
|
|
|
|
{
|
|
|
|
type Request = ServerMessage;
|
|
|
|
type Response = ();
|
|
|
|
type Error = T::Error;
|
|
|
|
type InitError = T::InitError;
|
|
|
|
type Service = ServerMessageAcceptorService<T::Service, H>;
|
|
|
|
type Future = ServerMessageAcceptorResponse<T, H>;
|
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
|
|
|
ServerMessageAcceptorResponse {
|
|
|
|
fut: self.inner.new_service(),
|
|
|
|
settings: self.settings.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct ServerMessageAcceptorResponse<T, H>
|
|
|
|
where
|
|
|
|
H: HttpHandler,
|
|
|
|
T: NewService<Request = net::TcpStream>,
|
|
|
|
{
|
|
|
|
fut: T::Future,
|
2018-10-03 00:25:32 +02:00
|
|
|
settings: ServiceConfig<H>,
|
2018-09-28 20:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, H> Future for ServerMessageAcceptorResponse<T, H>
|
|
|
|
where
|
|
|
|
H: HttpHandler,
|
|
|
|
T: NewService<Request = net::TcpStream>,
|
|
|
|
{
|
|
|
|
type Item = ServerMessageAcceptorService<T::Service, H>;
|
|
|
|
type Error = T::InitError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
match self.fut.poll()? {
|
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
|
|
|
Async::Ready(service) => Ok(Async::Ready(ServerMessageAcceptorService {
|
|
|
|
inner: service,
|
|
|
|
settings: self.settings.clone(),
|
|
|
|
})),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct ServerMessageAcceptorService<T, H: HttpHandler> {
|
|
|
|
inner: T,
|
2018-10-03 00:25:32 +02:00
|
|
|
settings: ServiceConfig<H>,
|
2018-09-28 20:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, H> Service for ServerMessageAcceptorService<T, H>
|
|
|
|
where
|
|
|
|
H: HttpHandler,
|
|
|
|
T: Service<Request = net::TcpStream>,
|
|
|
|
{
|
|
|
|
type Request = ServerMessage;
|
|
|
|
type Response = ();
|
|
|
|
type Error = T::Error;
|
|
|
|
type Future =
|
|
|
|
Either<ServerMessageAcceptorServiceFut<T>, FutureResult<(), Self::Error>>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
self.inner.poll_ready()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
|
|
match req {
|
|
|
|
ServerMessage::Connect(stream) => {
|
|
|
|
Either::A(ServerMessageAcceptorServiceFut {
|
|
|
|
fut: self.inner.call(stream),
|
|
|
|
})
|
|
|
|
}
|
2018-10-03 00:25:32 +02:00
|
|
|
ServerMessage::Shutdown(_) => Either::B(ok(())),
|
2018-09-28 20:50:47 +02:00
|
|
|
ServerMessage::ForceShutdown => {
|
2018-10-03 00:25:32 +02:00
|
|
|
self.settings
|
|
|
|
.head()
|
|
|
|
.traverse(|proto: &mut HttpProtocol<TcpStream, H>| proto.shutdown());
|
2018-09-28 20:50:47 +02:00
|
|
|
Either::B(ok(()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct ServerMessageAcceptorServiceFut<T: Service> {
|
|
|
|
fut: T::Future,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Future for ServerMessageAcceptorServiceFut<T>
|
|
|
|
where
|
|
|
|
T: Service,
|
|
|
|
{
|
|
|
|
type Item = ();
|
|
|
|
type Error = T::Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
match self.fut.poll()? {
|
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
|
|
|
Async::Ready(_) => Ok(Async::Ready(())),
|
2018-09-28 02:15:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|