Struct Handle

Source
pub struct Handle { /* private fields */ }
Expand description

A handle to manage and interact with the server.

Handle provides methods to access server information, such as the number of active connections, and to perform actions like initiating a shutdown.

Implementations§

Source§

impl Handle

Source

pub fn new() -> Self

Create a new handle for the server.

§Returns

A new Handle instance.

Examples found in repository?
examples/shutdown.rs (line 16)
13async fn main() {
14    let app = Router::new().route("/", get(|| async { "Hello, world!" }));
15
16    let handle = Handle::new();
17
18    // Spawn a task to shutdown server.
19    tokio::spawn(shutdown(handle.clone()));
20
21    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
22    println!("listening on {}", addr);
23    hyper_server::bind(addr)
24        .handle(handle)
25        .serve(app.into_make_service())
26        .await
27        .unwrap();
28
29    println!("server is shut down");
30}
More examples
Hide additional examples
examples/graceful_shutdown.rs (line 19)
16async fn main() {
17    let app = Router::new().route("/", get(|| async { "Hello, world!" }));
18
19    let handle = Handle::new();
20
21    // Spawn a task to gracefully shutdown server.
22    tokio::spawn(graceful_shutdown(handle.clone()));
23
24    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
25    println!("listening on {}", addr);
26    hyper_server::bind(addr)
27        .handle(handle)
28        .serve(app.into_make_service())
29        .await
30        .unwrap();
31
32    println!("server is shut down");
33}
Source

pub fn connection_count(&self) -> usize

Get the number of active connections to the server.

§Returns

The number of active connections.

Examples found in repository?
examples/graceful_shutdown.rs (line 48)
35async fn graceful_shutdown(handle: Handle) {
36    // Wait 10 seconds.
37    sleep(Duration::from_secs(10)).await;
38
39    println!("sending graceful shutdown signal");
40
41    // Signal the server to shutdown using Handle.
42    handle.graceful_shutdown(Some(Duration::from_secs(30)));
43
44    // Print alive connection count every second.
45    loop {
46        sleep(Duration::from_secs(1)).await;
47
48        println!("alive connections: {}", handle.connection_count());
49    }
50}
Source

pub fn shutdown(&self)

Initiate an immediate shutdown of the server.

This method will terminate the server without waiting for active connections to close.

Examples found in repository?
examples/shutdown.rs (line 39)
32async fn shutdown(handle: Handle) {
33    // Wait 20 seconds.
34    sleep(Duration::from_secs(20)).await;
35
36    println!("sending shutdown signal");
37
38    // Signal the server to shutdown using Handle.
39    handle.shutdown();
40}
Source

pub fn graceful_shutdown(&self, duration: Option<Duration>)

Initiate a graceful shutdown of the server.

The server will wait for active connections to close before shutting down. If a duration is provided, the server will wait up to that duration for active connections to close before forcing a shutdown.

§Parameters
  • duration: Maximum time to wait for active connections to close. None means the server will wait indefinitely.
Examples found in repository?
examples/graceful_shutdown.rs (line 42)
35async fn graceful_shutdown(handle: Handle) {
36    // Wait 10 seconds.
37    sleep(Duration::from_secs(10)).await;
38
39    println!("sending graceful shutdown signal");
40
41    // Signal the server to shutdown using Handle.
42    handle.graceful_shutdown(Some(Duration::from_secs(30)));
43
44    // Print alive connection count every second.
45    loop {
46        sleep(Duration::from_secs(1)).await;
47
48        println!("alive connections: {}", handle.connection_count());
49    }
50}
Source

pub async fn listening(&self) -> Option<SocketAddr>

Wait until the server starts listening and then returns its local address and port.

§Returns

The local SocketAddr if the server successfully binds, otherwise None.

Trait Implementations§

Source§

impl Clone for Handle

Source§

fn clone(&self) -> Handle

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Handle

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Handle

Source§

fn default() -> Handle

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Handle

§

impl RefUnwindSafe for Handle

§

impl Send for Handle

§

impl Sync for Handle

§

impl Unpin for Handle

§

impl UnwindSafe for Handle

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more