Skip to content

Commit 7010281

Browse files
refactor(server): await shutdown without spawning
Signed-off-by: nachiketb <nachiketb@nvidia.com>
1 parent 7151c7d commit 7010281

1 file changed

Lines changed: 28 additions & 31 deletions

File tree

  • crates/switchyard-server/src

crates/switchyard-server/src/lib.rs

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -309,18 +309,13 @@ async fn serve_tls(
309309
let config = RustlsConfig::from_pem_file(tls.cert, tls.key)
310310
.await
311311
.map_err(server_io_error)?;
312-
let handle = axum_server::Handle::new();
313-
let shutdown_task = schedule_shutdown(handle.clone(), shutdown_timeout, shutdown);
314-
315312
let std_listener = listener.into_std().map_err(server_io_error)?;
316-
let result = axum_server::from_tcp_rustls(std_listener, config)
317-
.map_err(server_io_error)?
318-
.handle(handle)
319-
.serve(router.into_make_service())
320-
.await
321-
.map_err(server_io_error);
322-
shutdown_task.abort();
323-
result
313+
let server = axum_server::from_tcp_rustls(std_listener, config).map_err(server_io_error)?;
314+
let handle = axum_server::Handle::new();
315+
let server = server
316+
.handle(handle.clone())
317+
.serve(router.into_make_service());
318+
serve_until_shutdown(server, handle, shutdown_timeout, shutdown).await
324319
}
325320

326321
async fn serve(
@@ -329,32 +324,34 @@ async fn serve(
329324
shutdown_timeout: Duration,
330325
shutdown: impl Future<Output = ()> + Send + 'static,
331326
) -> ServerResult<()> {
332-
let handle = axum_server::Handle::new();
333-
let shutdown_task = schedule_shutdown(handle.clone(), shutdown_timeout, shutdown);
334327
let std_listener = listener.into_std().map_err(server_io_error)?;
335-
let result = axum_server::from_tcp(std_listener)
336-
.map_err(server_io_error)?
337-
.handle(handle)
338-
.serve(router.into_make_service())
339-
.await
340-
.map_err(server_io_error);
341-
shutdown_task.abort();
342-
result
328+
let server = axum_server::from_tcp(std_listener).map_err(server_io_error)?;
329+
let handle = axum_server::Handle::new();
330+
let server = server
331+
.handle(handle.clone())
332+
.serve(router.into_make_service());
333+
serve_until_shutdown(server, handle, shutdown_timeout, shutdown).await
343334
}
344335

345-
fn schedule_shutdown(
336+
/// Runs the server until it exits or shutdown begins, then drains active requests.
337+
async fn serve_until_shutdown(
338+
server: impl Future<Output = std::io::Result<()>>,
346339
handle: axum_server::Handle<SocketAddr>,
347340
timeout: Duration,
348341
shutdown: impl Future<Output = ()> + Send + 'static,
349-
) -> task::JoinHandle<()> {
350-
tokio::spawn(async move {
351-
shutdown.await;
352-
tracing::info!(
353-
?timeout,
354-
"shutdown signal received; draining active requests"
355-
);
356-
handle.graceful_shutdown(Some(timeout));
357-
})
342+
) -> ServerResult<()> {
343+
tokio::pin!(server);
344+
tokio::select! {
345+
result = &mut server => result.map_err(server_io_error),
346+
_ = shutdown => {
347+
tracing::info!(
348+
?timeout,
349+
"shutdown signal received; draining active requests"
350+
);
351+
handle.graceful_shutdown(Some(timeout));
352+
server.await.map_err(server_io_error)
353+
}
354+
}
358355
}
359356

360357
/// Ingress timestamp for one request, taken before any body is read.

0 commit comments

Comments
 (0)