2018-08-20 22:21:23 -07:00
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicUsize, Ordering},
|
|
|
|
Arc,
|
|
|
|
};
|
2018-08-19 10:47:04 -07:00
|
|
|
use std::{fmt, io, net};
|
|
|
|
|
|
|
|
use futures::{future, Future, Poll};
|
|
|
|
use tokio_reactor::Handle;
|
|
|
|
use tokio_tcp::TcpStream;
|
2018-08-21 17:08:23 -07:00
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
use super::{NewService, Service};
|
2018-08-19 10:47:04 -07:00
|
|
|
|
|
|
|
pub(crate) type BoxedServerService = Box<
|
|
|
|
Service<
|
|
|
|
Request = net::TcpStream,
|
|
|
|
Response = (),
|
|
|
|
Error = (),
|
|
|
|
Future = Box<Future<Item = (), Error = ()>>,
|
|
|
|
>,
|
|
|
|
>;
|
|
|
|
|
|
|
|
pub(crate) struct ServerService<T> {
|
|
|
|
inner: T,
|
2018-08-20 22:21:23 -07:00
|
|
|
counter: Arc<AtomicUsize>,
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Service for ServerService<T>
|
|
|
|
where
|
|
|
|
T: Service<Request = TcpStream, Response = ()>,
|
|
|
|
T::Future: 'static,
|
|
|
|
T::Error: fmt::Display + 'static,
|
|
|
|
{
|
|
|
|
type Request = net::TcpStream;
|
|
|
|
type Response = ();
|
|
|
|
type Error = ();
|
|
|
|
type Future = Box<Future<Item = (), Error = ()>>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
self.inner.poll_ready().map_err(|_| ())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, stream: net::TcpStream) -> Self::Future {
|
|
|
|
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 {
|
2018-08-20 22:21:23 -07:00
|
|
|
let counter = self.counter.clone();
|
|
|
|
let _ = counter.fetch_add(1, Ordering::Relaxed);
|
|
|
|
Box::new(self.inner.call(stream).map_err(|_| ()).map(move |_| {
|
|
|
|
let _ = counter.fetch_sub(1, Ordering::Relaxed);
|
|
|
|
}))
|
2018-08-19 10:47:04 -07:00
|
|
|
} else {
|
|
|
|
Box::new(future::err(()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
pub(crate) struct ServerNewService<F, T>
|
|
|
|
where
|
|
|
|
F: Fn() -> T + Send + Clone,
|
|
|
|
{
|
2018-08-23 13:16:14 -07:00
|
|
|
inner: F,
|
2018-08-20 22:21:23 -07:00
|
|
|
counter: Arc<AtomicUsize>,
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
impl<F, T> ServerNewService<F, T>
|
2018-08-19 10:47:04 -07:00
|
|
|
where
|
2018-08-23 13:16:14 -07:00
|
|
|
F: Fn() -> T + Send + Clone + 'static,
|
2018-08-23 15:42:34 -07:00
|
|
|
T: NewService<Request = TcpStream, Response = (), InitError = io::Error> + 'static,
|
2018-08-19 10:47:04 -07:00
|
|
|
T::Service: 'static,
|
|
|
|
T::Future: 'static,
|
|
|
|
T::Error: fmt::Display,
|
|
|
|
{
|
2018-08-23 15:42:34 -07:00
|
|
|
pub(crate) fn create(inner: F) -> Box<ServerServiceFactory + Send> {
|
2018-08-20 22:21:23 -07:00
|
|
|
Box::new(Self {
|
|
|
|
inner,
|
|
|
|
counter: Arc::new(AtomicUsize::new(0)),
|
|
|
|
})
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
pub trait ServerServiceFactory {
|
2018-08-20 22:21:23 -07:00
|
|
|
fn counter(&self) -> Arc<AtomicUsize>;
|
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
fn clone_factory(&self) -> Box<ServerServiceFactory + Send>;
|
2018-08-19 10:47:04 -07:00
|
|
|
|
|
|
|
fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>>;
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
impl<F, T> ServerServiceFactory for ServerNewService<F, T>
|
2018-08-19 10:47:04 -07:00
|
|
|
where
|
2018-08-23 13:16:14 -07:00
|
|
|
F: Fn() -> T + Send + Clone + 'static,
|
2018-08-23 15:42:34 -07:00
|
|
|
T: NewService<Request = TcpStream, Response = (), InitError = io::Error> + 'static,
|
2018-08-19 10:47:04 -07:00
|
|
|
T::Service: 'static,
|
|
|
|
T::Future: 'static,
|
|
|
|
T::Error: fmt::Display,
|
|
|
|
{
|
2018-08-20 22:21:23 -07:00
|
|
|
fn counter(&self) -> Arc<AtomicUsize> {
|
|
|
|
self.counter.clone()
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
fn clone_factory(&self) -> Box<ServerServiceFactory + Send> {
|
2018-08-19 10:47:04 -07:00
|
|
|
Box::new(Self {
|
|
|
|
inner: self.inner.clone(),
|
2018-08-20 22:21:23 -07:00
|
|
|
counter: Arc::new(AtomicUsize::new(0)),
|
2018-08-19 10:47:04 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>> {
|
2018-08-20 22:21:23 -07:00
|
|
|
let counter = self.counter.clone();
|
2018-08-21 17:08:23 -07:00
|
|
|
Box::new(
|
2018-08-23 13:16:14 -07:00
|
|
|
(self.inner)()
|
2018-08-23 15:42:34 -07:00
|
|
|
.new_service()
|
2018-08-21 17:08:23 -07:00
|
|
|
.map_err(|_| ())
|
|
|
|
.map(move |inner| {
|
|
|
|
let service: BoxedServerService =
|
|
|
|
Box::new(ServerService { inner, counter });
|
|
|
|
service
|
|
|
|
}),
|
|
|
|
)
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
impl ServerServiceFactory for Box<ServerServiceFactory> {
|
2018-08-20 22:21:23 -07:00
|
|
|
fn counter(&self) -> Arc<AtomicUsize> {
|
|
|
|
self.as_ref().counter()
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
fn clone_factory(&self) -> Box<ServerServiceFactory + Send> {
|
2018-08-19 10:47:04 -07:00
|
|
|
self.as_ref().clone_factory()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>> {
|
|
|
|
self.as_ref().create()
|
|
|
|
}
|
|
|
|
}
|