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
impl Handle
Sourcepub fn new() -> Self
pub fn new() -> Self
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
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}
Sourcepub fn connection_count(&self) -> usize
pub fn connection_count(&self) -> usize
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}
Sourcepub fn shutdown(&self)
pub fn shutdown(&self)
Initiate an immediate shutdown of the server.
This method will terminate the server without waiting for active connections to close.
Sourcepub fn graceful_shutdown(&self, duration: Option<Duration>)
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}
Sourcepub async fn listening(&self) -> Option<SocketAddr>
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§
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> 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