golang.org/x/time/rate
Use the rate limiter.
The rate Package
golang.org/x/time/rate provides a production-ready token bucket Limiter. It is the standard choice for rate limiting in Go.
- Battle-tested implementation
- Allow, Wait, and Reserve methods
- Context-aware
Creating a Limiter
rate.NewLimiter takes a rate (tokens per second) and a burst size. rate.Limit is a float64 of events per second.
limiter := rate.NewLimiter(rate.Limit(10), 5)All lessons in this course
- Token Bucket Concept
- golang.org/x/time/rate
- Throttling Goroutines
- Backoff and Retry