This repository demonstrates three different approaches to implementing rate limiting in Go:
- Per-Client Rate Limiting (IP-Based)
- Token Bucket Algorithm
- Tollbooth Middleware
Each implementation is organized into separate folders for clarity and modularity.
This method limits requests per unique IP address. It maintains a map of clients and applies rate limiting per client using the golang.org/x/time/rate
package.
- Tracks clients based on IP
- Uses
rate.Limiter
for rate limiting - Cleans up inactive clients after 3 minutes
cd perclientlimit
go run main.go
A general rate-limiting technique that allows a burst of requests up to a limit and refills tokens at a fixed rate.
- Implements a token bucket system
- Uses
rate.Limiter(2, 4)
, allowing 2 requests per second with a burst capacity of 4
cd tokenbucket
go run main.go
This method uses the tollbooth
package to implement rate limiting as middleware, making it easy to integrate with existing applications.
- Simple and effective middleware
- Customizable error messages and response types
- Limits requests using
tollbooth.NewLimiter(1, nil)
, allowing 1 request per second
cd tollbooth
go run main.go
Endpoint | Description | Rate Limit |
---|---|---|
/pingp |
IP-based rate limiter | 2 req/sec, burst 4 |
/ping |
Token bucket rate limiter | 2 req/sec, burst 4 |
/pingtoll |
Tollbooth middleware limiter | 1 req/sec |
Ensure you have Go installed and run:
go mod tidy
Install Tollbooth if needed:
go get -u github.com/didip/tollbooth/v7
MIT License