From c3faf2d6e673650b275bbefad9070706efddf3d3 Mon Sep 17 00:00:00 2001 From: luna Date: Wed, 24 Sep 2025 15:45:41 -0300 Subject: [PATCH] [router] select first healthy worker on proxied get requests (#10827) --- sgl-router/src/routers/http/router.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/sgl-router/src/routers/http/router.rs b/sgl-router/src/routers/http/router.rs index 28d701ef2..1745c8eba 100644 --- a/sgl-router/src/routers/http/router.rs +++ b/sgl-router/src/routers/http/router.rs @@ -103,10 +103,11 @@ impl Router { fn select_first_worker(&self) -> Result { let workers = self.worker_registry.get_all(); - if workers.is_empty() { + let healthy_workers: Vec<_> = workers.iter().filter(|w| w.is_healthy()).collect(); + if healthy_workers.is_empty() { Err("No workers are available".to_string()) } else { - Ok(workers[0].url().to_string()) + Ok(healthy_workers[0].url().to_string()) } } @@ -1119,6 +1120,13 @@ mod tests { } } + fn create_test_unhealthy_router() -> Router { + let router = create_test_regular_router(); + let workers = router.worker_registry.get_all(); + workers[0].set_healthy(false); + router + } + #[test] fn test_router_get_worker_urls_regular() { let router = create_test_regular_router(); @@ -1140,4 +1148,16 @@ mod tests { // DashMap doesn't guarantee order, so just check we get one of the workers assert!(url == "http://worker1:8080" || url == "http://worker2:8080"); } + + #[test] + fn test_select_first_worker_with_unhealthy_worker() { + let router = create_test_unhealthy_router(); + let result = router.select_first_worker(); + + assert!(result.is_ok()); + let url = result.unwrap(); + + let worker = router.worker_registry.get_by_url(&url).unwrap(); + assert!(worker.is_healthy()); + } }