Skip to main content

rama_ttrpc/server/
controller.rs

1use tokio_util::sync::CancellationToken;
2
3/// Handle for gracefully shutting down a running [`ServerConnection`](super::ServerConnection).
4///
5/// Reachable from within a method handler via [`get_server`](crate::get_server). Call
6/// [`shutdown`](Self::shutdown) to stop accepting new requests and let `start` return once the
7/// in-flight requests have drained.
8#[derive(Clone, Default)]
9pub struct ServerController {
10    pub(super) token: CancellationToken,
11}
12
13impl ServerController {
14    #[must_use]
15    pub fn new() -> Self {
16        Self::default()
17    }
18
19    /// Request a graceful shutdown.
20    ///
21    /// Stops accepting new requests; the server's `start` future returns once the currently
22    /// in-flight requests have finished.
23    pub fn shutdown(&self) {
24        self.token.cancel();
25    }
26}