Skip to main content
InterviewHubPRO
Section Overview
Q24

Question 24 of 50

Part 2: Concurrency, Channels & The GMP Scheduler (16 - 32)

48%
Golang & Concurrency (50 Qs)•Part 2: Concurrency, Channels & The GMP Scheduler (16 - 32)

Token Bucket Rate Limiting using `golang.org/x/time/rate`.

senior LevelMost AskedCode ExampleSecurity & AuthGolang

In Simple Words: The Token Bucket algorithm allows bursty traffic up to a maximum bucket limit while refilling tokens at a steady rate.

go
import "golang.org/x/time/rate"

// 10 requests per second, burst up to 20
limiter := rate.NewLimiter(10, 20)

func handleRequest(w http.ResponseWriter, r *http.Request) {
	if !limiter.Allow() {
		http.Error(w, "Too Many Requests", http.StatusTooManyRequests)
		return
	}
	w.Write([]byte("OK"))
}