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 std::collections::HashMap; #[derive(Debug)] pub struct RequestHead {
Parsing an HTTP request head in Rust
parsing
error-handling
iterators
Intermediate
9 steps
php
<?php final class MarkdownParser {
Building a small Markdown-to-HTML parser in PHP
state-machine
parsing
closures
Intermediate
10 steps
rust
#[derive(Debug, Default)] pub struct RequestBuilder { url: String, method: String,
The builder pattern in Rust
builder-pattern
method-chaining
ergonomic-api
Intermediate
8 steps
rust
use axum::{extract::{Path, State}, http::StatusCode, Json}; use dashmap::DashMap; use serde::Serialize; use std::sync::Arc;
Request coalescing in an Axum handler
caching
concurrency
request-coalescing
Advanced
8 steps
javascript
import { useReducer, useEffect } from "react"; const initialState = { status: "idle", data: null, error: null };
Building a data-fetching hook in React
custom-hooks
usereducer
data-fetching
Intermediate
9 steps
rust
use std::net::Ipv4Addr; use std::str::FromStr; #[derive(Debug, Clone, Copy)]
Parsing and matching IPv4 CIDR ranges in Rust
bitwise-operations
parsing
error-handling
Intermediate
8 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.