fix: wake up background polling loop immediately when a client connects
All checks were successful
Build MIPS Binary / build (push) Successful in 4m22s

This commit is contained in:
spinline
2026-02-09 00:22:28 +03:00
parent e1370db6ce
commit f3121898e2
2 changed files with 15 additions and 2 deletions

View File

@@ -45,6 +45,7 @@ pub struct AppState {
pub db: db::Db,
#[cfg(feature = "push-notifications")]
pub push_store: push::PushSubscriptionStore,
pub notify_poll: Arc<tokio::sync::Notify>,
}
async fn auth_middleware(
@@ -336,6 +337,8 @@ async fn main() {
#[cfg(not(feature = "push-notifications"))]
let push_store = ();
let notify_poll = Arc::new(tokio::sync::Notify::new());
let app_state = AppState {
tx: tx.clone(),
event_bus: event_bus.clone(),
@@ -343,6 +346,7 @@ async fn main() {
db: db.clone(),
#[cfg(feature = "push-notifications")]
push_store,
notify_poll: notify_poll.clone(),
};
// Spawn background task to poll rTorrent
@@ -351,6 +355,7 @@ async fn main() {
let socket_path = args.socket.clone(); // Clone for background task
#[cfg(feature = "push-notifications")]
let push_store_clone = app_state.push_store.clone();
let notify_poll_clone = notify_poll.clone();
tokio::spawn(async move {
let client = xmlrpc::RtorrentClient::new(&socket_path);
@@ -438,8 +443,13 @@ async fn main() {
previous_torrents = new_torrents;
// Success case: sleep for the determined interval
tokio::time::sleep(loop_interval).await;
// Success case: wait for the determined interval OR a wakeup notification
tokio::select! {
_ = tokio::time::sleep(loop_interval) => {},
_ = notify_poll_clone.notified() => {
tracing::debug!("Background loop awakened by new client connection");
}
}
}
Err(e) => {
tracing::error!("Error fetching torrents in background: {}", e);

View File

@@ -195,6 +195,9 @@ pub async fn fetch_global_stats(client: &RtorrentClient) -> Result<GlobalStats,
pub async fn sse_handler(
State(state): State<AppState>,
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
// Notify background worker to wake up and poll immediately
state.notify_poll.notify_one();
// Get initial value synchronously (from the watch channel's current state)
let initial_rx = state.tx.subscribe();
let initial_torrents = initial_rx.borrow().clone();