aboutsummaryrefslogtreecommitdiff
path: root/src/util/token_bucket.rs
diff options
context:
space:
mode:
authorAlex <alex@adnab.me>2023-01-04 13:47:42 +0000
committerAlex <alex@adnab.me>2023-01-04 13:47:42 +0000
commit329c0e64f9044511f1a0d46b1b3ed99bdd890630 (patch)
tree222ec61f03c5abcca6678239111e94ad4dcfeb67 /src/util/token_bucket.rs
parent4eb8ca3a528dae2848141f5cc3eb607eb7d40114 (diff)
parent29dbcb82780dcdb6f2a01a9da5122e70abaf93bf (diff)
downloadgarage-329c0e64f9044511f1a0d46b1b3ed99bdd890630.tar.gz
garage-329c0e64f9044511f1a0d46b1b3ed99bdd890630.zip
Merge pull request 'Improve `garage worker set` and add `garage worker get`' (#464) from worker-get into main
Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/garage/pulls/464
Diffstat (limited to 'src/util/token_bucket.rs')
-rw-r--r--src/util/token_bucket.rs40
1 files changed, 0 insertions, 40 deletions
diff --git a/src/util/token_bucket.rs b/src/util/token_bucket.rs
deleted file mode 100644
index cc0dfa1f..00000000
--- a/src/util/token_bucket.rs
+++ /dev/null
@@ -1,40 +0,0 @@
-use std::time::{Duration, Instant};
-
-use tokio::time::sleep;
-
-pub struct TokenBucket {
- // Replenish rate: number of tokens per second
- replenish_rate: u64,
- // Current number of tokens
- tokens: u64,
- // Last replenish time
- last_replenish: Instant,
-}
-
-impl TokenBucket {
- pub fn new(replenish_rate: u64) -> Self {
- Self {
- replenish_rate,
- tokens: 0,
- last_replenish: Instant::now(),
- }
- }
-
- pub async fn take(&mut self, tokens: u64) {
- while self.tokens < tokens {
- let needed = tokens - self.tokens;
- let delay = (needed as f64) / (self.replenish_rate as f64);
- sleep(Duration::from_secs_f64(delay)).await;
- self.replenish();
- }
- self.tokens -= tokens;
- }
-
- pub fn replenish(&mut self) {
- let now = Instant::now();
- let new_tokens =
- ((now - self.last_replenish).as_secs_f64() * (self.replenish_rate as f64)) as u64;
- self.tokens += new_tokens;
- self.last_replenish = now;
- }
-}