mirror of
https://github.com/tokio-rs/axum.git
synced 2024-12-27 15:00:31 +01:00
Make into_future on Serve, WithGracefulShutdown independent of tokio (#2975)
This commit is contained in:
parent
b3cd8d066d
commit
a0f310a492
1 changed files with 26 additions and 10 deletions
|
@ -312,17 +312,17 @@ where
|
|||
_marker: _,
|
||||
} = self;
|
||||
|
||||
let (signal_tx, signal_rx) = watch::channel(());
|
||||
let signal_tx = Arc::new(signal_tx);
|
||||
tokio::spawn(async move {
|
||||
signal.await;
|
||||
trace!("received graceful shutdown signal. Telling tasks to shutdown");
|
||||
drop(signal_rx);
|
||||
});
|
||||
|
||||
let (close_tx, close_rx) = watch::channel(());
|
||||
|
||||
private::ServeFuture(Box::pin(async move {
|
||||
let (signal_tx, signal_rx) = watch::channel(());
|
||||
let signal_tx = Arc::new(signal_tx);
|
||||
tokio::spawn(async move {
|
||||
signal.await;
|
||||
trace!("received graceful shutdown signal. Telling tasks to shutdown");
|
||||
drop(signal_rx);
|
||||
});
|
||||
|
||||
let (close_tx, close_rx) = watch::channel(());
|
||||
|
||||
loop {
|
||||
let (tcp_stream, remote_addr) = tokio::select! {
|
||||
conn = tcp_accept(&tcp_listener) => {
|
||||
|
@ -597,4 +597,20 @@ mod tests {
|
|||
assert_eq!(address.ip(), IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)));
|
||||
assert_ne!(address.port(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn into_future_outside_tokio() {
|
||||
let router: Router = Router::new();
|
||||
let addr = "0.0.0.0:0";
|
||||
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let listener = rt.block_on(tokio::net::TcpListener::bind(addr)).unwrap();
|
||||
|
||||
// Call Serve::into_future outside of a tokio context. This used to panic.
|
||||
_ = serve(listener, router).into_future();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue