rust
45 lines · 8 steps
A token-bucket rate limiter in Tokio
A background ticker paces requests through channels so callers acquire permits at a fixed rate per second.
Explained by
highlit
1use std::time::Duration;
2use tokio::sync::mpsc;
3use tokio::time::{interval, MissedTickBehavior};
4
5pub struct RateLimiter {
6 permits: mpsc::Sender<()>,
7}
8
9impl RateLimiter {
10 pub fn new(per_second: u32) -> Self {
11 let (tx, mut rx) = mpsc::channel::<()>(per_second as usize);
12 let (permits, mut requests) = mpsc::channel::<()>(1);
13
14 let period = Duration::from_secs_f64(1.0 / per_second as f64);
15 tokio::spawn(async move {
16 let mut ticker = interval(period);
17 ticker.set_missed_tick_behavior(MissedTickBehavior::Delay);
18 loop {
19 ticker.tick().await;
20 if requests.recv().await.is_none() {
21 break;
22 }
23 let _ = rx.recv().await;
24 }
25 });
26
27 drop(tx);
28 Self { permits }
29 }
30
31 pub async fn acquire(&self) {
32 let _ = self.permits.send(()).await;
33 }
34}
35
36impl Clone for RateLimiter {
37 fn clone(&self) -> Self {
38 Self { permits: self.permits.clone() }
39 }
40}
41
42pub async fn throttled_fetch(limiter: &RateLimiter, client: &reqwest::Client, url: &str) -> reqwest::Result<String> {
43 limiter.acquire().await;
44 client.get(url).send().await?.text().await
45}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Bounded channels turn producer/consumer pacing into natural backpressure without explicit locks.
- 2A single background task owning a timer keeps rate-limit state off the caller's hot path.
- 3Cloning a Sender shares one limiter across tasks while a dropped Sender signals graceful shutdown.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
python
from fastapi import FastAPI, WebSocket, WebSocketDisconnect app = FastAPI()
Building a WebSocket chat with FastAPI
websockets
broadcast
connection-management
Intermediate
9 steps
go
package streaming import ( "bufio"
Streaming NDJSON logs over HTTP in Go
http-streaming
channels
select
Advanced
10 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
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-tokio-explained-rust-66cd/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.