Make into_future on Serve, WithGracefulShutdown independent of tokio (#2975)

This commit is contained in:
Jonas Platte 2024-10-11 08:57:05 +00:00 committed by GitHub
parent b3cd8d066d
commit a0f310a492
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -312,17 +312,17 @@ where
_marker: _, _marker: _,
} = self; } = 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 { 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 { loop {
let (tcp_stream, remote_addr) = tokio::select! { let (tcp_stream, remote_addr) = tokio::select! {
conn = tcp_accept(&tcp_listener) => { conn = tcp_accept(&tcp_listener) => {
@ -597,4 +597,20 @@ mod tests {
assert_eq!(address.ip(), IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))); assert_eq!(address.ip(), IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)));
assert_ne!(address.port(), 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();
}
} }