From 603724ebcc1f2346a4d9e47e8af6d0b4650b8a83 Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 26 Mar 2024 23:53:44 +0100 Subject: [PATCH] feat: improved dispatcher sleep logic --- src/dispatcher.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/dispatcher.rs b/src/dispatcher.rs index 34753cd8..e99c379f 100644 --- a/src/dispatcher.rs +++ b/src/dispatcher.rs @@ -22,17 +22,16 @@ impl Dispatcher { } async fn worker(db: DatabaseConnection, domain: String, poll_interval: u64) -> Result<(), UpubError> { - let mut nosleep = true; loop { - if nosleep { nosleep = false } else { - tokio::time::sleep(std::time::Duration::from_secs(poll_interval)).await; - } let Some(delivery) = model::delivery::Entity::find() .filter(Condition::all().add(model::delivery::Column::NotBefore.lte(chrono::Utc::now()))) .order_by(model::delivery::Column::NotBefore, Order::Asc) .one(&db) .await? - else { continue }; + else { + tokio::time::sleep(std::time::Duration::from_secs(poll_interval)).await; + continue + }; let del_row = model::delivery::ActiveModel { id: sea_orm::ActiveValue::Set(delivery.id), @@ -44,12 +43,10 @@ async fn worker(db: DatabaseConnection, domain: String, poll_interval: u64) -> R if del.rows_affected == 0 { // another worker claimed this delivery - nosleep = true; continue; // go back to the top } if delivery.expired() { // try polling for another one - nosleep = true; continue; // go back to top }