From 1261ecbce035e810baa1e5c7363d142038197fe7 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 23 Aug 2018 13:39:13 -0700 Subject: [PATCH] fix examples --- examples/basic.rs | 49 +++++++++++++++++++++++++---------------------- examples/ssl.rs | 14 +++++++++----- src/server.rs | 4 ++-- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index 001dcf28..5dd95b40 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -61,32 +61,35 @@ fn main() { let num = Arc::new(AtomicUsize::new(0)); - // configure service pipeline - let srv = - // service for converting incoming TcpStream to a SslStream - (move |stream| { - SslAcceptorExt::accept_async(&acceptor, stream) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) - }) - // convert closure to a `NewService` - .into_new_service() - - // .and_then() combinator uses other service to convert incoming `Request` to a `Response` - // and then uses that response as an input for next service. - // in this case, on success we use `logger` service - .and_then(logger) - - // next service uses two components, service state and service function - // actix-net generates `NewService` impl that creates `ServiceState` instance for each new service - // and use `service` function as `Service::call` - .and_then((service, move || { - Ok::<_, io::Error>(ServiceState { num: num.clone() }) - })); - // bind socket address and start workers. By default server uses number of // available logical cpu as threads count. actix net start separate // instances of service pipeline in each worker. - Server::default().bind("0.0.0.0:8443", srv).unwrap().start(); + Server::default().bind( + // configure service pipeline + "0.0.0.0:8443", move || { + let num = num.clone(); + let acceptor = acceptor.clone(); + + // service for converting incoming TcpStream to a SslStream + (move |stream| { + SslAcceptorExt::accept_async(&acceptor, stream) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) + }) + // convert closure to a `NewService` + .into_new_service() + + // .and_then() combinator uses other service to convert incoming `Request` to a `Response` + // and then uses that response as an input for next service. + // in this case, on success we use `logger` service + .and_then(logger) + + // next service uses two components, service state and service function + // actix-net generates `NewService` impl that creates `ServiceState` instance for each new service + // and use `service` function as `Service::call` + .and_then((service, move || { + Ok::<_, io::Error>(ServiceState { num: num.clone() }) + })) + }).unwrap().start(); sys.run(); } diff --git a/examples/ssl.rs b/examples/ssl.rs index 5d974f72..25ec9551 100644 --- a/examples/ssl.rs +++ b/examples/ssl.rs @@ -43,13 +43,17 @@ fn main() { .unwrap(); let num = Arc::new(AtomicUsize::new(0)); + let openssl = ssl::OpensslService::new(builder); - // configure service - let srv = ssl::OpensslService::new(builder).and_then((service, move || { - Ok::<_, io::Error>(ServiceState { num: num.clone() }) - })); + // server start mutiple workers, it runs supplied `Fn` in each worker. + Server::default().bind("0.0.0.0:8443", move || { + let num = num.clone(); - Server::default().bind("0.0.0.0:8443", srv).unwrap().start(); + // configure service + openssl.clone().and_then((service, move || { + Ok::<_, io::Error>(ServiceState { num: num.clone() }) + })) + }).unwrap().start(); sys.run(); } diff --git a/src/server.rs b/src/server.rs index efdd5541..6c38c206 100644 --- a/src/server.rs +++ b/src/server.rs @@ -170,7 +170,7 @@ impl Server { /// Add new service to server pub fn bind(mut self, addr: U, factory: F) -> io::Result where - F: Fn() -> N + Copy + Send + 'static, + F: Fn() -> N + Clone + Send + 'static, U: net::ToSocketAddrs, N: NewService + 'static, N::Service: 'static, @@ -188,7 +188,7 @@ impl Server { /// Add new service to server pub fn listen(mut self, lst: net::TcpListener, factory: F) -> Self where - F: Fn() -> N + Copy + Send + 'static, + F: Fn() -> N + Clone + Send + 'static, N: NewService + 'static, N::Service: 'static, N::Future: 'static,