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

A handle for Server.

Implementations

Create a new handle.

Examples found in repository?
examples/shutdown.rs (line 16)
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
async fn main() {
    let app = Router::new().route("/", get(|| async { "Hello, world!" }));

    let handle = Handle::new();

    // Spawn a task to shutdown server.
    tokio::spawn(shutdown(handle.clone()));

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    println!("listening on {}", addr);
    axum_server::bind(addr)
        .handle(handle)
        .serve(app.into_make_service())
        .await
        .unwrap();

    println!("server is shut down");
}
More examples
Hide additional examples
examples/graceful_shutdown.rs (line 19)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
async fn main() {
    let app = Router::new().route("/", get(|| async { "Hello, world!" }));

    let handle = Handle::new();

    // Spawn a task to gracefully shutdown server.
    tokio::spawn(graceful_shutdown(handle.clone()));

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    println!("listening on {}", addr);
    axum_server::bind(addr)
        .handle(handle)
        .serve(app.into_make_service())
        .await
        .unwrap();

    println!("server is shut down");
}

Get the number of connections.

Examples found in repository?
examples/graceful_shutdown.rs (line 48)
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
async fn graceful_shutdown(handle: Handle) {
    // Wait 10 seconds.
    sleep(Duration::from_secs(10)).await;

    println!("sending graceful shutdown signal");

    // Signal the server to shutdown using Handle.
    handle.graceful_shutdown(Some(Duration::from_secs(30)));

    // Print alive connection count every second.
    loop {
        sleep(Duration::from_secs(1)).await;

        println!("alive connections: {}", handle.connection_count());
    }
}

Shutdown the server.

Examples found in repository?
examples/shutdown.rs (line 39)
32
33
34
35
36
37
38
39
40
async fn shutdown(handle: Handle) {
    // Wait 20 seconds.
    sleep(Duration::from_secs(20)).await;

    println!("sending shutdown signal");

    // Signal the server to shutdown using Handle.
    handle.shutdown();
}

Gracefully shutdown the server.

Examples found in repository?
examples/graceful_shutdown.rs (line 42)
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
async fn graceful_shutdown(handle: Handle) {
    // Wait 10 seconds.
    sleep(Duration::from_secs(10)).await;

    println!("sending graceful shutdown signal");

    // Signal the server to shutdown using Handle.
    handle.graceful_shutdown(Some(Duration::from_secs(30)));

    // Print alive connection count every second.
    loop {
        sleep(Duration::from_secs(1)).await;

        println!("alive connections: {}", handle.connection_count());
    }
}

Returns local address and port when server starts listening.

Returns None if server fails to bind.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

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

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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