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

Walkthrough

Space play step click any line
Three takeaways
  1. 1Bounded channels turn producer/consumer pacing into natural backpressure without explicit locks.
  2. 2A single background task owning a timer keeps rate-limit state off the caller's hot path.
  3. 3Cloning a Sender shares one limiter across tasks while a dropped Sender signals graceful shutdown.

Related explainers

Share this explainer

Here's the card — post it anywhere.

A token-bucket rate limiter in Tokio — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code