recipes
recipes copied to clipboard
feat: add rate limiter middleware recipe using Redis
What this PR does
Adds a new recipe for implementing a rate limiter middleware in Fiber using Redis.
The middleware limits incoming requests based on client IP and a fixed time window.
Features
- Redis-backed rate limiting
- Configurable time window and request cap
- Adds
Retry-Afterheader for better client UX - Returns
429 Too Many Requestswhen limit exceeded
Why it's useful
This recipe shows developers how to:
- Integrate Redis with Go Fiber
- Use Fiber middleware pattern
- Handle rate limiting cleanly and idiomatically
- Improve API reliability under load
Folder Structure
rate-limit-redis/ ├── config/ │ └── redis.go ├── handlers/ │ └── home.go ├── middleware/ │ └── limiter.go ├── main.go
How to Run
- Start Redis locally or via Docker: docker run -p 6379:6379 redis
2.Run the project: go run main.go
3.Test with: curl http://localhost:8080/home
Summary by CodeRabbit
- New Features
- Introduced a rate limiting middleware for the Fiber web framework using Redis to restrict the number of requests per IP address.
- Added a sample endpoint to demonstrate rate-limited access.
- Documentation
- Added comprehensive documentation with setup instructions, usage examples, and configuration guidelines for the rate limiter.