pub struct Control { /* private fields */ }Expand description
A handle for shutdown and connection counting that is not tied to an address type.
Unlike Handle, one Control can drive servers listening on different
address types, for example TCP and Unix at the same time. Build a Handle
for each listener with Handle::with_control, then
graceful_shutdown or shutdown
drains all of them and connection_count covers
every listener.
use axum_server::{Control, Handle};
use std::net::SocketAddr;
let control = Control::new();
let handle = Handle::<SocketAddr>::with_control(control.clone());Implementations§
Source§impl Control
impl Control
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new control handle.
Examples found in repository?
examples/multiple_listeners.rs (line 11)
7async fn main() -> io::Result<()> {
8 let app = Router::new().route("/", get(|| async { "Hello, world!" }));
9
10 // One control handle drains every listener below.
11 let control = Control::new();
12 let mut servers = JoinSet::new();
13
14 let tcp_handle = Handle::<SocketAddr>::with_control(control.clone());
15 let tcp =
16 axum_server::bind(SocketAddr::from(([127, 0, 0, 1], 3000))).handle(tcp_handle.clone());
17 let tcp_service = app.clone().into_make_service();
18
19 servers.spawn(async move { tcp.serve(tcp_service).await });
20
21 #[cfg(unix)]
22 let unix_handle = {
23 use std::os::unix::net::SocketAddr;
24
25 let path = std::env::temp_dir().join("axum-server-multiple-listeners.sock");
26 let _ = std::fs::remove_file(&path);
27
28 let handle = Handle::with_control(control.clone());
29 let unix = axum_server::bind(SocketAddr::from_pathname(path)?).handle(handle.clone());
30 let service = app.clone().into_make_service();
31
32 servers.spawn(async move { unix.serve(service).await });
33
34 handle
35 };
36
37 println!("tcp: http://{}/", tcp_handle.listening().await.unwrap());
38 #[cfg(unix)]
39 println!(
40 "unix: {:?}",
41 unix_handle
42 .listening()
43 .await
44 .unwrap()
45 .as_pathname()
46 .unwrap()
47 );
48
49 let shutdown = control.clone();
50 tokio::spawn(async move {
51 let _ = tokio::signal::ctrl_c().await;
52 shutdown.graceful_shutdown(Some(Duration::from_secs(10)));
53 });
54
55 // A failing listener stops the others instead of leaving them running.
56 while let Some(result) = servers.join_next().await {
57 match result {
58 Ok(Ok(())) => {}
59 Ok(Err(error)) => {
60 eprintln!("server error: {error}");
61 control.shutdown();
62 }
63 Err(error) => {
64 eprintln!("server task failed: {error}");
65 control.shutdown();
66 }
67 }
68 }
69
70 #[cfg(unix)]
71 let _ = std::fs::remove_file(std::env::temp_dir().join("axum-server-multiple-listeners.sock"));
72
73 Ok(())
74}Sourcepub fn connection_count(&self) -> usize
pub fn connection_count(&self) -> usize
Get the number of live connections across all servers using this control.
Sourcepub fn shutdown(&self)
pub fn shutdown(&self)
Shutdown all servers using this control.
Examples found in repository?
examples/multiple_listeners.rs (line 61)
7async fn main() -> io::Result<()> {
8 let app = Router::new().route("/", get(|| async { "Hello, world!" }));
9
10 // One control handle drains every listener below.
11 let control = Control::new();
12 let mut servers = JoinSet::new();
13
14 let tcp_handle = Handle::<SocketAddr>::with_control(control.clone());
15 let tcp =
16 axum_server::bind(SocketAddr::from(([127, 0, 0, 1], 3000))).handle(tcp_handle.clone());
17 let tcp_service = app.clone().into_make_service();
18
19 servers.spawn(async move { tcp.serve(tcp_service).await });
20
21 #[cfg(unix)]
22 let unix_handle = {
23 use std::os::unix::net::SocketAddr;
24
25 let path = std::env::temp_dir().join("axum-server-multiple-listeners.sock");
26 let _ = std::fs::remove_file(&path);
27
28 let handle = Handle::with_control(control.clone());
29 let unix = axum_server::bind(SocketAddr::from_pathname(path)?).handle(handle.clone());
30 let service = app.clone().into_make_service();
31
32 servers.spawn(async move { unix.serve(service).await });
33
34 handle
35 };
36
37 println!("tcp: http://{}/", tcp_handle.listening().await.unwrap());
38 #[cfg(unix)]
39 println!(
40 "unix: {:?}",
41 unix_handle
42 .listening()
43 .await
44 .unwrap()
45 .as_pathname()
46 .unwrap()
47 );
48
49 let shutdown = control.clone();
50 tokio::spawn(async move {
51 let _ = tokio::signal::ctrl_c().await;
52 shutdown.graceful_shutdown(Some(Duration::from_secs(10)));
53 });
54
55 // A failing listener stops the others instead of leaving them running.
56 while let Some(result) = servers.join_next().await {
57 match result {
58 Ok(Ok(())) => {}
59 Ok(Err(error)) => {
60 eprintln!("server error: {error}");
61 control.shutdown();
62 }
63 Err(error) => {
64 eprintln!("server task failed: {error}");
65 control.shutdown();
66 }
67 }
68 }
69
70 #[cfg(unix)]
71 let _ = std::fs::remove_file(std::env::temp_dir().join("axum-server-multiple-listeners.sock"));
72
73 Ok(())
74}Sourcepub fn graceful_shutdown(&self, duration: Option<Duration>)
pub fn graceful_shutdown(&self, duration: Option<Duration>)
Gracefully shutdown all servers using this control.
None means indefinite grace period.
Examples found in repository?
examples/multiple_listeners.rs (line 52)
7async fn main() -> io::Result<()> {
8 let app = Router::new().route("/", get(|| async { "Hello, world!" }));
9
10 // One control handle drains every listener below.
11 let control = Control::new();
12 let mut servers = JoinSet::new();
13
14 let tcp_handle = Handle::<SocketAddr>::with_control(control.clone());
15 let tcp =
16 axum_server::bind(SocketAddr::from(([127, 0, 0, 1], 3000))).handle(tcp_handle.clone());
17 let tcp_service = app.clone().into_make_service();
18
19 servers.spawn(async move { tcp.serve(tcp_service).await });
20
21 #[cfg(unix)]
22 let unix_handle = {
23 use std::os::unix::net::SocketAddr;
24
25 let path = std::env::temp_dir().join("axum-server-multiple-listeners.sock");
26 let _ = std::fs::remove_file(&path);
27
28 let handle = Handle::with_control(control.clone());
29 let unix = axum_server::bind(SocketAddr::from_pathname(path)?).handle(handle.clone());
30 let service = app.clone().into_make_service();
31
32 servers.spawn(async move { unix.serve(service).await });
33
34 handle
35 };
36
37 println!("tcp: http://{}/", tcp_handle.listening().await.unwrap());
38 #[cfg(unix)]
39 println!(
40 "unix: {:?}",
41 unix_handle
42 .listening()
43 .await
44 .unwrap()
45 .as_pathname()
46 .unwrap()
47 );
48
49 let shutdown = control.clone();
50 tokio::spawn(async move {
51 let _ = tokio::signal::ctrl_c().await;
52 shutdown.graceful_shutdown(Some(Duration::from_secs(10)));
53 });
54
55 // A failing listener stops the others instead of leaving them running.
56 while let Some(result) = servers.join_next().await {
57 match result {
58 Ok(Ok(())) => {}
59 Ok(Err(error)) => {
60 eprintln!("server error: {error}");
61 control.shutdown();
62 }
63 Err(error) => {
64 eprintln!("server task failed: {error}");
65 control.shutdown();
66 }
67 }
68 }
69
70 #[cfg(unix)]
71 let _ = std::fs::remove_file(std::env::temp_dir().join("axum-server-multiple-listeners.sock"));
72
73 Ok(())
74}Trait Implementations§
Auto Trait Implementations§
impl Forget for Control
impl Freeze for Control
impl RefUnwindSafe for Control
impl Send for Control
impl Sync for Control
impl Unpin for Control
impl UnsafeUnpin for Control
impl UnwindSafe for Control
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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