pub struct Handle<A: Address> { /* private fields */ }Expand description
A handle for Server.
Implementations§
Source§impl<A: Address> Handle<A>
impl<A: Address> Handle<A>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new handle.
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 axum_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
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 axum_server::bind(addr)
27 .handle(handle)
28 .serve(app.into_make_service())
29 .await
30 .unwrap();
31
32 println!("server is shut down");
33}Sourcepub fn connection_count(&self) -> usize
pub fn connection_count(&self) -> usize
Get the number of connections.
Examples found in repository?
examples/graceful_shutdown.rs (line 48)
35async fn graceful_shutdown(handle: Handle<SocketAddr>) {
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}Sourcepub fn graceful_shutdown(&self, duration: Option<Duration>)
pub fn graceful_shutdown(&self, duration: Option<Duration>)
Gracefully shutdown the server.
None means indefinite grace period.
Examples found in repository?
examples/graceful_shutdown.rs (line 42)
35async fn graceful_shutdown(handle: Handle<SocketAddr>) {
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}Trait Implementations§
Auto Trait Implementations§
impl<A> Freeze for Handle<A>
impl<A> RefUnwindSafe for Handle<A>
impl<A> Send for Handle<A>where
A: Send,
impl<A> Sync for Handle<A>where
A: Send,
impl<A> Unpin for Handle<A>
impl<A> UnwindSafe for Handle<A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more