mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-09-26 23:52:05 +00:00
refactor(rate_limit): one balance() behind tokens() and is_idle()
Both took the lock, refilled and read the field, differing only in the comparison.
This commit is contained in:
@@ -91,21 +91,24 @@ impl TokenBucket {
|
||||
tokio::time::sleep(Duration::from_secs_f64(wait)).await;
|
||||
}
|
||||
|
||||
/// Current balance, for the tests that assert a call site charged the
|
||||
/// bucket (a charge is otherwise only observable as a delay).
|
||||
#[cfg(test)]
|
||||
pub(crate) fn tokens(&self) -> f64 {
|
||||
/// Current balance, refilled to now.
|
||||
fn balance(&self) -> f64 {
|
||||
let mut state = self.state.lock();
|
||||
self.refill(&mut state);
|
||||
state.tokens
|
||||
}
|
||||
|
||||
/// Current balance, for the tests that assert a call site charged the
|
||||
/// bucket (a charge is otherwise only observable as a delay).
|
||||
#[cfg(test)]
|
||||
pub(crate) fn tokens(&self) -> f64 {
|
||||
self.balance()
|
||||
}
|
||||
|
||||
/// True when the bucket has refilled to capacity: no debt outstanding, so
|
||||
/// the chat has not sent anything recently.
|
||||
fn is_idle(&self) -> bool {
|
||||
let mut state = self.state.lock();
|
||||
self.refill(&mut state);
|
||||
state.tokens >= self.capacity
|
||||
self.balance() >= self.capacity
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user