diff --git a/CHANGELOG.md b/CHANGELOG.md index 75e56d7a..dbd5b4d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Now `InlineQueryResultsButton` serializes properly ([issue 1181](https://github.com/teloxide/teloxide/issues/1181)) - Now `ThreadId` is able to serialize in multipart requests ([PR 1179](https://github.com/teloxide/teloxide/pull/1179)) - Now stack does not overflow on dispatch ([issue 1154](https://github.com/teloxide/teloxide/issues/1154)) +- Fixed calculation of per-second limits in the `Throttle` adaptor ([PR 1212](https://github.com/teloxide/teloxide/pull/1212)) ## 0.13.0 - 2024-08-16 diff --git a/crates/teloxide-core/src/adaptors/throttle/worker.rs b/crates/teloxide-core/src/adaptors/throttle/worker.rs index 26d08493..67c11114 100644 --- a/crates/teloxide-core/src/adaptors/throttle/worker.rs +++ b/crates/teloxide-core/src/adaptors/throttle/worker.rs @@ -213,7 +213,7 @@ pub(super) async fn worker( // as truncates which is ok since in case of truncation it would always be >= // limits.overall_s - let used = history.iter().take_while(|(_, time)| time > &sec_back).count() as u32; + let used = history.iter().rev().take_while(|(_, time)| time > &sec_back).count() as u32; let mut allowed = limits.messages_per_sec_overall.saturating_sub(used); if allowed == 0 { @@ -222,7 +222,7 @@ pub(super) async fn worker( continue; } - for (chat, _) in history.iter().take_while(|(_, time)| time > &sec_back) { + for (chat, _) in history.iter().rev().take_while(|(_, time)| time > &sec_back) { *requests_sent.per_sec.entry(*chat).or_insert(0) += 1; }