actix_server/
handle.rs

1use std::future::Future;
2
3use tokio::sync::{mpsc::UnboundedSender, oneshot};
4
5use crate::server::ServerCommand;
6
7/// Server handle.
8#[derive(Debug, Clone)]
9pub struct ServerHandle {
10    cmd_tx: UnboundedSender<ServerCommand>,
11}
12
13impl ServerHandle {
14    pub(crate) fn new(cmd_tx: UnboundedSender<ServerCommand>) -> Self {
15        ServerHandle { cmd_tx }
16    }
17
18    pub(crate) fn worker_faulted(&self, idx: usize) {
19        let _ = self.cmd_tx.send(ServerCommand::WorkerFaulted(idx));
20    }
21
22    /// Pause accepting incoming connections.
23    ///
24    /// May drop socket pending connection. All open connections remain active.
25    pub fn pause(&self) -> impl Future<Output = ()> {
26        let (tx, rx) = oneshot::channel();
27        let _ = self.cmd_tx.send(ServerCommand::Pause(tx));
28        async {
29            let _ = rx.await;
30        }
31    }
32
33    /// Resume accepting incoming connections.
34    pub fn resume(&self) -> impl Future<Output = ()> {
35        let (tx, rx) = oneshot::channel();
36        let _ = self.cmd_tx.send(ServerCommand::Resume(tx));
37        async {
38            let _ = rx.await;
39        }
40    }
41
42    /// Stop incoming connection processing, stop all workers and exit.
43    pub fn stop(&self, graceful: bool) -> impl Future<Output = ()> {
44        let (tx, rx) = oneshot::channel();
45
46        let _ = self.cmd_tx.send(ServerCommand::Stop {
47            graceful,
48            completion: Some(tx),
49            force_system_stop: false,
50        });
51
52        async {
53            let _ = rx.await;
54        }
55    }
56}