actix_server/
handle.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::future::Future;

use tokio::sync::{mpsc::UnboundedSender, oneshot};

use crate::server::ServerCommand;

/// Server handle.
#[derive(Debug, Clone)]
pub struct ServerHandle {
    cmd_tx: UnboundedSender<ServerCommand>,
}

impl ServerHandle {
    pub(crate) fn new(cmd_tx: UnboundedSender<ServerCommand>) -> Self {
        ServerHandle { cmd_tx }
    }

    pub(crate) fn worker_faulted(&self, idx: usize) {
        let _ = self.cmd_tx.send(ServerCommand::WorkerFaulted(idx));
    }

    /// Pause accepting incoming connections.
    ///
    /// May drop socket pending connection. All open connections remain active.
    pub fn pause(&self) -> impl Future<Output = ()> {
        let (tx, rx) = oneshot::channel();
        let _ = self.cmd_tx.send(ServerCommand::Pause(tx));
        async {
            let _ = rx.await;
        }
    }

    /// Resume accepting incoming connections.
    pub fn resume(&self) -> impl Future<Output = ()> {
        let (tx, rx) = oneshot::channel();
        let _ = self.cmd_tx.send(ServerCommand::Resume(tx));
        async {
            let _ = rx.await;
        }
    }

    /// Stop incoming connection processing, stop all workers and exit.
    pub fn stop(&self, graceful: bool) -> impl Future<Output = ()> {
        let (tx, rx) = oneshot::channel();

        let _ = self.cmd_tx.send(ServerCommand::Stop {
            graceful,
            completion: Some(tx),
            force_system_stop: false,
        });

        async {
            let _ = rx.await;
        }
    }
}