diff --git a/actix-server/src/builder.rs b/actix-server/src/builder.rs index c16de554..0e533528 100644 --- a/actix-server/src/builder.rs +++ b/actix-server/src/builder.rs @@ -64,7 +64,7 @@ impl ServerBuilder { /// 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. pub fn workers(mut self, num: usize) -> Self { self.threads = num; @@ -108,8 +108,8 @@ impl ServerBuilder { self } - /// Run external configuration as part of the server building - /// process + /// Execute external configuration as part of the server building + /// process. /// /// This function is useful for moving parts of configuration to a /// different module or even library. @@ -117,17 +117,20 @@ impl ServerBuilder { where F: Fn(&mut ServiceConfig) -> io::Result<()>, { - let mut cfg = ServiceConfig::new(); + let mut cfg = ServiceConfig::new(self.threads); f(&mut cfg)?; - let mut srv = ConfiguredService::new(cfg.rt); - for (name, lst) in cfg.services { - let token = self.token.next(); - srv.stream(token, name); - self.sockets.push((token, lst)); + if let Some(apply) = cfg.apply { + let mut srv = ConfiguredService::new(apply); + for (name, lst) in cfg.services { + let token = self.token.next(); + 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) } diff --git a/actix-server/src/config.rs b/actix-server/src/config.rs index 85480978..603dadff 100644 --- a/actix-server/src/config.rs +++ b/actix-server/src/config.rs @@ -15,18 +15,28 @@ use super::services::{ use super::Token; pub struct ServiceConfig { - pub(super) services: Vec<(String, net::TcpListener)>, - pub(super) rt: Box, + pub(crate) services: Vec<(String, net::TcpListener)>, + pub(crate) apply: Option>, + pub(crate) threads: usize, } impl ServiceConfig { - pub(super) fn new() -> ServiceConfig { + pub(super) fn new(threads: usize) -> ServiceConfig { ServiceConfig { + threads, 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 pub fn bind>(&mut self, name: N, addr: U) -> io::Result<&mut Self> where @@ -43,16 +53,20 @@ impl ServiceConfig { /// Add new service to server pub fn listen>(&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 } - /// Register service configuration function - pub fn rt(&mut self, f: F) -> io::Result<()> + /// Register service configuration function. This function get called + /// during worker runtime configuration. It get executed in worker thread. + pub fn apply(&mut self, f: F) -> io::Result<()> where F: Fn(&mut ServiceRuntime) + Send + Clone + 'static, { - self.rt = Box::new(f); + self.apply = Some(Box::new(f)); Ok(()) } }