mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-23 19:51:06 +01:00
use factory function instead of NewService for service registration
This commit is contained in:
parent
2818540d69
commit
d97f78afbe
@ -19,7 +19,7 @@ use actix::{
|
||||
use super::accept::{AcceptLoop, AcceptNotify, Command};
|
||||
use super::server_config::{Config, ServerConfig};
|
||||
use super::server_service::{ServerNewService, ServerServiceFactory};
|
||||
use super::service::{IntoNewService, NewService};
|
||||
use super::service::NewService;
|
||||
use super::worker::{Conn, StopWorker, Worker, WorkerClient};
|
||||
use super::{PauseServer, ResumeServer, StopServer, Token};
|
||||
|
||||
@ -168,14 +168,11 @@ impl<C: Config> Server<C> {
|
||||
}
|
||||
|
||||
/// 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
|
||||
F: Fn() -> N + Copy + Send + 'static,
|
||||
U: net::ToSocketAddrs,
|
||||
T: IntoNewService<N> + Clone,
|
||||
N: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error>
|
||||
+ Clone
|
||||
+ Send
|
||||
+ 'static,
|
||||
N: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error> + 'static,
|
||||
N::Service: 'static,
|
||||
N::Future: 'static,
|
||||
N::Error: fmt::Display,
|
||||
@ -183,28 +180,22 @@ impl<C: Config> Server<C> {
|
||||
let sockets = bind_addr(addr)?;
|
||||
|
||||
for lst in sockets {
|
||||
self = self.listen(lst, srv.clone())
|
||||
self = self.listen(lst, factory.clone())
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// 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
|
||||
T: IntoNewService<N>,
|
||||
N: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error>
|
||||
+ Clone
|
||||
+ Send
|
||||
+ 'static,
|
||||
F: Fn() -> N + Copy + Send + 'static,
|
||||
N: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error> + 'static,
|
||||
N::Service: 'static,
|
||||
N::Future: 'static,
|
||||
N::Error: fmt::Display,
|
||||
{
|
||||
let token = Token(self.services.len());
|
||||
self.services.push(ServerNewService::create(
|
||||
srv.into_new_service(),
|
||||
self.config.clone(),
|
||||
));
|
||||
self.services.push(ServerNewService::create(factory, self.config.clone()));
|
||||
self.sockets.push((token, lst));
|
||||
self
|
||||
}
|
||||
|
@ -56,23 +56,21 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct ServerNewService<T, C> {
|
||||
inner: T,
|
||||
pub(crate) struct ServerNewService<F, T, C> where F: Fn() -> T + Send + Clone {
|
||||
inner: F,
|
||||
config: C,
|
||||
counter: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
impl<T, C: Config> ServerNewService<T, C>
|
||||
impl<F, T, C: Config> ServerNewService<F, T, C>
|
||||
where
|
||||
T: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error>
|
||||
+ Clone
|
||||
+ Send
|
||||
+ 'static,
|
||||
F: Fn() -> T + Send + Clone + 'static,
|
||||
T: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error> + 'static,
|
||||
T::Service: 'static,
|
||||
T::Future: 'static,
|
||||
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 {
|
||||
inner,
|
||||
config,
|
||||
@ -89,11 +87,10 @@ pub trait ServerServiceFactory<C> {
|
||||
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
|
||||
F: Fn() -> T + Send + Clone + 'static,
|
||||
T: NewService<Request = TcpStream, Response = (), Config = C, InitError = io::Error>
|
||||
+ Clone
|
||||
+ Send
|
||||
+ 'static,
|
||||
T::Service: 'static,
|
||||
T::Future: 'static,
|
||||
@ -114,7 +111,7 @@ where
|
||||
fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>> {
|
||||
let counter = self.counter.clone();
|
||||
Box::new(
|
||||
self.inner
|
||||
(self.inner)()
|
||||
.new_service(self.config.clone())
|
||||
.map_err(|_| ())
|
||||
.map(move |inner| {
|
||||
|
Loading…
Reference in New Issue
Block a user