From 1432dec8285d112435c37ebf201acc18804c5f5f Mon Sep 17 00:00:00 2001 From: spinline Date: Sun, 8 Feb 2026 23:57:32 +0300 Subject: [PATCH] perf: implement dynamic polling interval based on active clients --- backend/src/main.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/backend/src/main.rs b/backend/src/main.rs index bac6fea..668ca7e 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -359,6 +359,14 @@ async fn main() { let mut backoff_duration = Duration::from_secs(1); loop { + // Determine polling interval based on active clients + let active_clients = event_bus_tx.receiver_count(); + let loop_interval = if active_clients > 0 { + Duration::from_secs(1) + } else { + Duration::from_secs(30) + }; + // 1. Fetch Torrents let torrents_result = sse::fetch_torrents(&client).await; @@ -429,6 +437,9 @@ async fn main() { } previous_torrents = new_torrents; + + // Success case: sleep for the determined interval + tokio::time::sleep(loop_interval).await; } Err(e) => { tracing::error!("Error fetching torrents in background: {}", e); @@ -449,20 +460,15 @@ async fn main() { "Backoff: Sleeping for {:?} due to rTorrent error.", backoff_duration ); + + tokio::time::sleep(backoff_duration).await; } } // Handle Stats - match stats_result { - Ok(stats) => { - let _ = event_bus_tx.send(AppEvent::Stats(stats)); - } - Err(e) => { - tracing::warn!("Error fetching global stats: {}", e); - } + if let Ok(stats) = stats_result { + let _ = event_bus_tx.send(AppEvent::Stats(stats)); } - - tokio::time::sleep(backoff_duration).await; } });