Add ServerCommand::Restart and Command::Restart

This PR creates a new `ServerCommand` and corresponding `Command` to restart
server workers.

Related to actix/actix-web#280
This commit is contained in:
Josh Leeb-du Toit 2019-05-16 19:17:18 +10:00
parent 0156f479a0
commit d19a5e1ea1
3 changed files with 33 additions and 0 deletions

View File

@ -16,6 +16,7 @@ use super::Token;
pub(crate) enum Command { pub(crate) enum Command {
Pause, Pause,
Resume, Resume,
Restart,
Stop, Stop,
Worker(WorkerClient), Worker(WorkerClient),
} }
@ -315,6 +316,26 @@ impl Accept {
} }
} }
} }
Command::Restart => {
for (token, info) in self.sockets.iter_mut() {
if let Err(err) = self.poll.deregister(&info.sock) {
error!("Can not restart server socket {}", err);
}
if let Err(err) = self.poll.reregister(
&info.sock,
mio::Token(token + DELTA),
mio::Ready::readable(),
mio::PollOpt::edge(),
) {
error!("Can not restart socket accept process: {}", err);
} else {
info!(
"Accepting connections on {} has been restarted",
info.addr
);
}
}
}
Command::Stop => { Command::Stop => {
for (_, info) in self.sockets.iter() { for (_, info) in self.sockets.iter() {
let _ = self.poll.deregister(&info.sock); let _ = self.poll.deregister(&info.sock);

View File

@ -287,6 +287,10 @@ impl ServerBuilder {
self.accept.send(Command::Resume); self.accept.send(Command::Resume);
let _ = tx.send(()); let _ = tx.send(());
} }
ServerCommand::Restart(tx) => {
self.accept.send(Command::Restart);
let _ = tx.send(());
}
ServerCommand::Signal(sig) => { ServerCommand::Signal(sig) => {
// Signals support // Signals support
// Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and stop actix system // Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and stop actix system

View File

@ -10,6 +10,7 @@ pub(crate) enum ServerCommand {
WorkerDied(usize), WorkerDied(usize),
Pause(oneshot::Sender<()>), Pause(oneshot::Sender<()>),
Resume(oneshot::Sender<()>), Resume(oneshot::Sender<()>),
Restart(oneshot::Sender<()>),
Signal(Signal), Signal(Signal),
/// Whether to try and shut down gracefully /// Whether to try and shut down gracefully
Stop { Stop {
@ -56,6 +57,13 @@ impl Server {
rx.map_err(|_| ()) rx.map_err(|_| ())
} }
/// Restart server
pub fn restart(&self) -> impl Future<Item = (), Error = ()> {
let (tx, rx) = oneshot::channel();
let _ = self.0.unbounded_send(ServerCommand::Restart(tx));
rx.map_err(|_| ())
}
/// Stop incoming connection processing, stop all workers and exit. /// Stop incoming connection processing, stop all workers and exit.
/// ///
/// If server starts with `spawn()` method, then spawned thread get terminated. /// If server starts with `spawn()` method, then spawned thread get terminated.