Skip to content

Commit c3c3354

Browse files
authored
Merge pull request #2106 from KaiKorla/fix/token-bucket-refill-overflow
cap token bucket refill at capacity
2 parents bae16cc + a442308 commit c3c3354

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fixed:
1717
- Do not send `MARKREAD` for non-`PRIVMSG`/`NOTICE` messages when the server does not support echoes
1818
- Do not show unread/highlight indicators for ignored messages
1919
- Do not copy empty selections to the primary clipboard (i.e. do not clear the primary clipboard when a non-selection action is performed, such as moving the cursor or focusing the text input on some systems)
20+
- Prevent anti-flood rate limiting from refilling the token bucket beyond its configured capacity
2021

2122
Changed:
2223

@@ -27,7 +28,7 @@ Changed:
2728

2829
Thanks:
2930

30-
- Contributions: @englut, @luca020400, @classabbyamp
31+
- Contributions: @englut, @luca020400, @classabbyamp, @KaiKorla
3132
- Bug reports: @luca020400, agent314
3233

3334
# 2026.7.2 (2026-06-08)

data/src/rate_limit.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl<T> TokenBucket<T> {
127127
.saturating_add(
128128
new_permits.try_into().unwrap_or(self.capacity),
129129
)
130-
.max(self.capacity);
130+
.min(self.capacity);
131131

132132
self.last = Some(now);
133133
}
@@ -196,3 +196,20 @@ pub enum TokenPriority {
196196
High, // Most automated messages
197197
User, // Messages that the user triggers directly
198198
}
199+
200+
#[cfg(test)]
201+
mod tests {
202+
use super::*;
203+
204+
#[test]
205+
fn token_bucket_refill_does_not_exceed_capacity() {
206+
let start = Instant::now();
207+
let mut bucket = TokenBucket::<usize>::new(Duration::from_secs(1), 3);
208+
bucket.available_permits = 1;
209+
bucket.last = Some(start);
210+
211+
bucket.add_permits(start + Duration::from_secs(10));
212+
213+
assert_eq!(bucket.available_permits, 3);
214+
}
215+
}

0 commit comments

Comments
 (0)