rust
47 lines · 8 steps
A token bucket rate limiter in Rust
A token bucket refills tokens over time and only allows work when enough tokens are on hand.
Explained by
highlit
1use std::time::{Duration, Instant};
2
3pub struct TokenBucket {
4 capacity: f64,
5 tokens: f64,
6 refill_per_sec: f64,
7 last_refill: Instant,
8}
9
10impl TokenBucket {
11 pub fn new(capacity: f64, refill_per_sec: f64) -> Self {
12 Self {
13 capacity,
14 tokens: capacity,
15 refill_per_sec,
16 last_refill: Instant::now(),
17 }
18 }
19
20 fn refill(&mut self) {
21 let now = Instant::now();
22 let elapsed = now.duration_since(self.last_refill).as_secs_f64();
23 if elapsed > 0.0 {
24 self.tokens = (self.tokens + elapsed * self.refill_per_sec).min(self.capacity);
25 self.last_refill = now;
26 }
27 }
28
29 pub fn try_acquire(&mut self, cost: f64) -> bool {
30 self.refill();
31 if self.tokens >= cost {
32 self.tokens -= cost;
33 true
34 } else {
35 false
36 }
37 }
38
39 pub fn time_until_available(&mut self, cost: f64) -> Duration {
40 self.refill();
41 if self.tokens >= cost {
42 return Duration::ZERO;
43 }
44 let deficit = cost - self.tokens;
45 Duration::from_secs_f64(deficit / self.refill_per_sec)
46 }
47}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Lazy refill on each access avoids background timers by computing tokens from elapsed time.
- 2Clamping to capacity keeps a bucket from accumulating credit while idle.
- 3Storing the last refill instant lets you derive both availability and wait time from one clock read.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
rust
use std::cmp::{Ordering, Reverse}; use std::collections::BinaryHeap; use std::fs::File; use std::io::{self, BufRead, BufReader, BufWriter, Lines, Write};
K-way merge of sorted logs in Rust
binary-heap
k-way-merge
streaming-io
Intermediate
8 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
rust
use std::f64::consts::PI; #[derive(Debug, Clone, Copy)] pub struct LatLng {
Building geographic bounding boxes in Rust
geospatial
value-types
option
Intermediate
7 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
python
import secrets from django.contrib.auth import authenticate, login from django.core.cache import cache
Two-factor login with OTP in Django
two-factor-auth
one-time-passwords
caching
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-token-bucket-rate-limiter-in-rust-explained-rust-3376/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.