Struct hyper_server::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
impl Handle
sourcepub fn new() -> Self
pub fn new() -> Self
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);
hyper_server::bind(addr)
.handle(handle)
.serve(app.into_make_service())
.await
.unwrap();
println!("server is shut down");
}
More 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);
hyper_server::bind(addr)
.handle(handle)
.serve(app.into_make_service())
.await
.unwrap();
println!("server is shut down");
}
sourcepub fn connection_count(&self) -> usize
pub fn connection_count(&self) -> usize
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());
}
}
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)
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());
}
}
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 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