1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-23 22:51:07 +01:00

use factory function instead of NewService for service registration

This commit is contained in:
Nikolay Kim 2018-08-23 13:16:14 -07:00
parent 2818540d69
commit d97f78afbe
2 changed files with 18 additions and 30 deletions

View File

@ -19,7 +19,7 @@ use actix::{
use super::accept::{AcceptLoop, AcceptNotify, Command}; use super::accept::{AcceptLoop, AcceptNotify, Command};
use super::server_config::{Config, ServerConfig}; use super::server_config::{Config, ServerConfig};
use super::server_service::{ServerNewService, ServerServiceFactory}; use super::server_service::{ServerNewService, ServerServiceFactory};
use super::service::{IntoNewService, NewService}; use super::service::NewService;
use super::worker::{Conn, StopWorker, Worker, WorkerClient}; use super::worker::{Conn, StopWorker, Worker, WorkerClient};
use super::{PauseServer, ResumeServer, StopServer, Token}; use super::{PauseServer, ResumeServer, StopServer, Token};
@ -168,14 +168,11 @@ impl<C: Config> Server<C> {
} }
/// Add new service to server /// Add new service to server
pub fn bind<T, U, N>(mut self, addr: U, srv: T) -> io::Result<Self> pub fn bind<F, U, N>(mut self, addr: U, factory: F) -> io::Result<Self>
where where
F: Fn() -> N + Copy + Send + 'static,
U: net::ToSocketAddrs, U: net::ToSocketAddrs,
T: IntoNewService<N> + Clone, N: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error> + 'static,
N: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error>
+ Clone
+ Send
+ 'static,
N::Service: 'static, N::Service: 'static,
N::Future: 'static, N::Future: 'static,
N::Error: fmt::Display, N::Error: fmt::Display,
@ -183,28 +180,22 @@ impl<C: Config> Server<C> {
let sockets = bind_addr(addr)?; let sockets = bind_addr(addr)?;
for lst in sockets { for lst in sockets {
self = self.listen(lst, srv.clone()) self = self.listen(lst, factory.clone())
} }
Ok(self) Ok(self)
} }
/// Add new service to server /// Add new service to server
pub fn listen<T, N>(mut self, lst: net::TcpListener, srv: T) -> Self pub fn listen<F, N>(mut self, lst: net::TcpListener, factory: F) -> Self
where where
T: IntoNewService<N>, F: Fn() -> N + Copy + Send + 'static,
N: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error> N: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error> + 'static,
+ Clone
+ Send
+ 'static,
N::Service: 'static, N::Service: 'static,
N::Future: 'static, N::Future: 'static,
N::Error: fmt::Display, N::Error: fmt::Display,
{ {
let token = Token(self.services.len()); let token = Token(self.services.len());
self.services.push(ServerNewService::create( self.services.push(ServerNewService::create(factory, self.config.clone()));
srv.into_new_service(),
self.config.clone(),
));
self.sockets.push((token, lst)); self.sockets.push((token, lst));
self self
} }

View File

@ -56,23 +56,21 @@ where
} }
} }
pub(crate) struct ServerNewService<T, C> { pub(crate) struct ServerNewService<F, T, C> where F: Fn() -> T + Send + Clone {
inner: T, inner: F,
config: C, config: C,
counter: Arc<AtomicUsize>, counter: Arc<AtomicUsize>,
} }
impl<T, C: Config> ServerNewService<T, C> impl<F, T, C: Config> ServerNewService<F, T, C>
where where
T: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error> F: Fn() -> T + Send + Clone + 'static,
+ Clone T: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error> + 'static,
+ Send
+ 'static,
T::Service: 'static, T::Service: 'static,
T::Future: 'static, T::Future: 'static,
T::Error: fmt::Display, T::Error: fmt::Display,
{ {
pub(crate) fn create(inner: T, config: C) -> Box<ServerServiceFactory<C> + Send> { pub(crate) fn create(inner: F, config: C) -> Box<ServerServiceFactory<C> + Send> {
Box::new(Self { Box::new(Self {
inner, inner,
config, config,
@ -89,11 +87,10 @@ pub trait ServerServiceFactory<C> {
fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>>; fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>>;
} }
impl<T, C: Config> ServerServiceFactory<C> for ServerNewService<T, C> impl<F, T, C: Config> ServerServiceFactory<C> for ServerNewService<F, T, C>
where where
F: Fn() -> T + Send + Clone + 'static,
T: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error> T: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error>
+ Clone
+ Send
+ 'static, + 'static,
T::Service: 'static, T::Service: 'static,
T::Future: 'static, T::Future: 'static,
@ -114,7 +111,7 @@ where
fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>> { fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>> {
let counter = self.counter.clone(); let counter = self.counter.clone();
Box::new( Box::new(
self.inner (self.inner)()
.new_service(self.config.clone()) .new_service(self.config.clone())
.map_err(|_| ()) .map_err(|_| ())
.map(move |inner| { .map(move |inner| {