From e4443226f694035c7d6752389be45f4e5e781b0d Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 17 Jun 2018 02:58:56 +0600 Subject: [PATCH] update actix usage --- src/httprequest.rs | 2 +- src/server/srv.rs | 52 +++++++++++++++++------------------ src/server/worker.rs | 2 ++ src/test.rs | 65 +++++++++++++++++++++----------------------- 4 files changed, 59 insertions(+), 62 deletions(-) diff --git a/src/httprequest.rs b/src/httprequest.rs index a54a9958..0cbdbb25 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -105,7 +105,7 @@ pub struct HttpRequest(SharedHttpInnerMessage, Option>, Option { /// Construct a new Request. #[inline] - pub fn new( + pub(crate) fn new( method: Method, uri: Uri, version: Version, headers: HeaderMap, payload: Option, ) -> HttpRequest { diff --git a/src/server/srv.rs b/src/server/srv.rs index c5aca757..c1cf0a18 100644 --- a/src/server/srv.rs +++ b/src/server/srv.rs @@ -4,8 +4,8 @@ use std::time::Duration; use std::{io, net, thread}; use actix::{ - fut, signal, Actor, ActorFuture, Addr, Arbiter, AsyncContext, Context, - ContextFutureSpawner, Handler, Response, StreamHandler, System, WrapFuture, + fut, signal, Actor, ActorFuture, Addr, Arbiter, AsyncContext, Context, Handler, + Response, StreamHandler, System, WrapFuture, }; use futures::sync::mpsc; @@ -64,8 +64,7 @@ where no_signals: bool, } -unsafe impl Sync for HttpServer where H: IntoHttpHandler {} -unsafe impl Send for HttpServer where H: IntoHttpHandler {} +unsafe impl Send for HttpServer {} enum ServerCommand { WorkerDied(usize, Slab), @@ -485,11 +484,9 @@ impl HttpServer { self.no_signals = false; let _ = thread::spawn(move || { - System::new("http-server") - .config(|| { - self.start(); - }) - .run(); + let sys = System::new("http-server"); + self.start(); + sys.run(); }).join(); } } @@ -557,7 +554,7 @@ impl HttpServer { pub fn start_incoming(mut self, stream: S, secure: bool) -> Addr where S: Stream + Send + 'static, - T: AsyncRead + AsyncWrite + 'static, + T: AsyncRead + AsyncWrite + Send + 'static, { // set server settings let addr: net::SocketAddr = "127.0.0.1:8080".parse().unwrap(); @@ -730,25 +727,26 @@ impl Handler for HttpServer { }; for worker in &self.workers { let tx2 = tx.clone(); - worker - .1 - .send(StopWorker { graceful: dur }) - .into_actor(self) - .then(move |_, slf, ctx| { - slf.workers.pop(); - if slf.workers.is_empty() { - let _ = tx2.send(()); + ctx.spawn( + worker + .1 + .send(StopWorker { graceful: dur }) + .into_actor(self) + .then(move |_, slf, ctx| { + slf.workers.pop(); + if slf.workers.is_empty() { + let _ = tx2.send(()); - // we need to stop system if server was spawned - if slf.exit { - ctx.run_later(Duration::from_millis(300), |_, _| { - System::current().stop(); - }); + // we need to stop system if server was spawned + if slf.exit { + ctx.run_later(Duration::from_millis(300), |_, _| { + System::current().stop(); + }); + } } - } - fut::ok(()) - }) - .spawn(ctx); + fut::ok(()) + }), + ); } if !self.workers.is_empty() { diff --git a/src/server/worker.rs b/src/server/worker.rs index 3d4ee863..6cf2bbd6 100644 --- a/src/server/worker.rs +++ b/src/server/worker.rs @@ -65,6 +65,8 @@ where tcp_ka: Option, } +unsafe impl Send for Worker {} + impl Worker { pub(crate) fn new( h: Vec, socks: Slab, keep_alive: KeepAlive, diff --git a/src/test.rs b/src/test.rs index b8372f6a..cf9b7f1c 100644 --- a/src/test.rs +++ b/src/test.rs @@ -109,15 +109,14 @@ impl TestServer { let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap(); let local_addr = tcp.local_addr().unwrap(); - sys.config(move || { - HttpServer::new(factory) - .disable_signals() - .listen(tcp) - .start(); + HttpServer::new(factory) + .disable_signals() + .listen(tcp) + .start(); - tx.send((System::current(), local_addr, TestServer::get_conn())) - .unwrap(); - }).run(); + tx.send((System::current(), local_addr, TestServer::get_conn())) + .unwrap(); + sys.run(); }); let (system, addr, conn) = rx.recv().unwrap(); @@ -280,34 +279,32 @@ impl TestServerBuilder { let tcp = net::TcpListener::bind("127.0.0.1:0").unwrap(); let local_addr = tcp.local_addr().unwrap(); - System::new("actix-test-server") - .config(move || { - let state = self.state; - let srv = HttpServer::new(move || { - let mut app = TestApp::new(state()); - config(&mut app); - vec![app] - }).workers(1) - .disable_signals(); + let sys = System::new("actix-test-server"); + let state = self.state; + let srv = HttpServer::new(move || { + let mut app = TestApp::new(state()); + config(&mut app); + vec![app] + }).workers(1) + .disable_signals(); - tx.send((System::current(), local_addr, TestServer::get_conn())) - .unwrap(); + tx.send((System::current(), local_addr, TestServer::get_conn())) + .unwrap(); - #[cfg(feature = "alpn")] - { - let ssl = self.ssl.take(); - if let Some(ssl) = ssl { - srv.listen_ssl(tcp, ssl).unwrap().start(); - } else { - srv.listen(tcp).start(); - } - } - #[cfg(not(feature = "alpn"))] - { - srv.listen(tcp).start(); - } - }) - .run(); + #[cfg(feature = "alpn")] + { + let ssl = self.ssl.take(); + if let Some(ssl) = ssl { + srv.listen_ssl(tcp, ssl).unwrap().start(); + } else { + srv.listen(tcp).start(); + } + } + #[cfg(not(feature = "alpn"))] + { + srv.listen(tcp).start(); + } + sys.run(); }); let (system, addr, conn) = rx.recv().unwrap();