diff --git a/Cargo.toml b/Cargo.toml index 1cae30f1..5ba8c745 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,8 +34,7 @@ thiserror = "1.0.20" once_cell = "1.5.0" never = "0.1.0" -# FIXME(waffle): use crates.io once published -vecrem = { git = "https://github.com/WaffleLapkin/vecrem", rev = "6b9b6f42342df8b75548c6ed387072ff235429b1" } +vecrem = { version = "0.1", optional = true } [features] default = [] @@ -47,7 +46,7 @@ default = [] nightly = [] # Throttling bot adaptor -throttle = [] +throttle = ["vecrem"] # CacheMe bot adaptor cache_me = [] diff --git a/src/adaptors/throttle.rs b/src/adaptors/throttle.rs index 75d79a80..de56338a 100644 --- a/src/adaptors/throttle.rs +++ b/src/adaptors/throttle.rs @@ -267,34 +267,33 @@ async fn worker(limits: Limits, mut queue_rx: mpsc::Receiver<(Id, Sender<Never>) *hchats_s.entry(*chat).or_insert(0) += 1; } - let mut queue_rem = queue.removing(); - while let Some(entry) = queue_rem.next() { - let chat = &entry.value().0; - let cond = { - hchats_s.get(chat).copied().unwrap_or(0) < limits.chat_s - && hchats.get(chat).copied().unwrap_or(0) < limits.chat_m - }; + { + let mut queue_rem = queue.removing(); + while let Some(entry) = queue_rem.next() { + let chat = &entry.value().0; + let cond = { + hchats_s.get(chat).copied().unwrap_or(0) < limits.chat_s + && hchats.get(chat).copied().unwrap_or(0) < limits.chat_m + }; - if cond { - { - *hchats_s.entry(*chat).or_insert(0) += 1; - *hchats.entry(*chat).or_insert(0) += 1; - history.push_back((*chat, Instant::now())); + if cond { + { + *hchats_s.entry(*chat).or_insert(0) += 1; + *hchats.entry(*chat).or_insert(0) += 1; + history.push_back((*chat, Instant::now())); + } + + // This will close the channel unlocking associated request + drop(entry.remove()); + + // We've "sent" 1 request, so now we can send 1 less + allowed -= 1; + if allowed == 0 { + break; + } } - - // This will close the channel unlocking associated request - drop(entry.remove()); - - // We've "sent" 1 request, so now we can send 1 less - allowed -= 1; - if allowed == 0 { - break; - } - } else { - entry.skip(); } } - drop(queue_rem); // It's easier to just recompute last second stats, instead of keeping // track of it alongside with minute stats, so we just throw this away.