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

rename ServiceConfig::rt to ServiceConfig::apply

This commit is contained in:
Nikolay Kim 2018-12-12 14:16:16 -08:00
parent fd3e77ea83
commit 5ca00dc798
2 changed files with 34 additions and 17 deletions

View File

@ -64,7 +64,7 @@ impl ServerBuilder {
/// Set number of workers to start. /// Set number of workers to start.
/// ///
/// By default server uses number of available logical cpu as threads /// By default server uses number of available logical cpu as workers
/// count. /// count.
pub fn workers(mut self, num: usize) -> Self { pub fn workers(mut self, num: usize) -> Self {
self.threads = num; self.threads = num;
@ -108,8 +108,8 @@ impl ServerBuilder {
self self
} }
/// Run external configuration as part of the server building /// Execute external configuration as part of the server building
/// process /// process.
/// ///
/// This function is useful for moving parts of configuration to a /// This function is useful for moving parts of configuration to a
/// different module or even library. /// different module or even library.
@ -117,17 +117,20 @@ impl ServerBuilder {
where where
F: Fn(&mut ServiceConfig) -> io::Result<()>, F: Fn(&mut ServiceConfig) -> io::Result<()>,
{ {
let mut cfg = ServiceConfig::new(); let mut cfg = ServiceConfig::new(self.threads);
f(&mut cfg)?; f(&mut cfg)?;
let mut srv = ConfiguredService::new(cfg.rt); if let Some(apply) = cfg.apply {
for (name, lst) in cfg.services { let mut srv = ConfiguredService::new(apply);
let token = self.token.next(); for (name, lst) in cfg.services {
srv.stream(token, name); let token = self.token.next();
self.sockets.push((token, lst)); srv.stream(token, name);
self.sockets.push((token, lst));
}
self.services.push(Box::new(srv));
} }
self.services.push(Box::new(srv)); self.threads = cfg.threads;
Ok(self) Ok(self)
} }

View File

@ -15,18 +15,28 @@ use super::services::{
use super::Token; use super::Token;
pub struct ServiceConfig { pub struct ServiceConfig {
pub(super) services: Vec<(String, net::TcpListener)>, pub(crate) services: Vec<(String, net::TcpListener)>,
pub(super) rt: Box<ServiceRuntimeConfiguration>, pub(crate) apply: Option<Box<ServiceRuntimeConfiguration>>,
pub(crate) threads: usize,
} }
impl ServiceConfig { impl ServiceConfig {
pub(super) fn new() -> ServiceConfig { pub(super) fn new(threads: usize) -> ServiceConfig {
ServiceConfig { ServiceConfig {
threads,
services: Vec::new(), services: Vec::new(),
rt: Box::new(not_configured), apply: None,
} }
} }
/// Set number of workers to start.
///
/// By default server uses number of available logical cpu as workers
/// count.
pub fn workers(&mut self, num: usize) {
self.threads = num;
}
/// Add new service to server /// Add new service to server
pub fn bind<U, N: AsRef<str>>(&mut self, name: N, addr: U) -> io::Result<&mut Self> pub fn bind<U, N: AsRef<str>>(&mut self, name: N, addr: U) -> io::Result<&mut Self>
where where
@ -43,16 +53,20 @@ impl ServiceConfig {
/// Add new service to server /// Add new service to server
pub fn listen<N: AsRef<str>>(&mut self, name: N, lst: net::TcpListener) -> &mut Self { pub fn listen<N: AsRef<str>>(&mut self, name: N, lst: net::TcpListener) -> &mut Self {
if self.apply.is_none() {
self.apply = Some(Box::new(not_configured));
}
self.services.push((name.as_ref().to_string(), lst)); self.services.push((name.as_ref().to_string(), lst));
self self
} }
/// Register service configuration function /// Register service configuration function. This function get called
pub fn rt<F>(&mut self, f: F) -> io::Result<()> /// during worker runtime configuration. It get executed in worker thread.
pub fn apply<F>(&mut self, f: F) -> io::Result<()>
where where
F: Fn(&mut ServiceRuntime) + Send + Clone + 'static, F: Fn(&mut ServiceRuntime) + Send + Clone + 'static,
{ {
self.rt = Box::new(f); self.apply = Some(Box::new(f));
Ok(()) Ok(())
} }
} }