Lesson 22 — Token bucket limiter (expert)
A bucket that refills
A token bucket is a fairer rate limiter. The bucket holds up to max tokens; each request takes one. Tokens refill over time (here: rate tokens per second), so a client can make a short burst and then continues at a steady pace. Real APIs (GitHub, Stripe) work like this.
We pass the time in seconds as now so the result is predictable.
Why it matters: it allows normal bursts while still stopping floods.
At the start of take(now), refill the bucket for the time since last (rate tokens per second, never above max), then remember now as last.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based Node.js Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for Node.js Foundations books — check the publication year so the examples match current versions.
Search Amazon →