A high-performance rate limiting middleware for Fiber with Redis and in-memory support, implementing multiple rate-limiting algorithms.
- Features
- Installation
- Usage
- Configuration Options
- Response Headers
- Algorithms
- Examples
- Contributing
- License
- 🚀 Multiple Algorithms: Token Bucket, Sliding Window, and Fixed Window
- 💾 Storage Options: Redis (for distributed systems) and in-memory (for single-instance)
- ⚡ High Performance: Minimal overhead with efficient algorithms
- 🔧 Customizable: Flexible key generation and response handling
- 📊 RFC Compliance: Standard
RateLimit
headers (RFC 6585)
go get github.com/NarmadaWeb/limiter/v2
package main
import (
"time"
"github.com/gofiber/fiber/v2"
"github.com/NarmadaWeb/limiter/v2"
)
func main() {
app := fiber.New()
// Initialize with default in-memory store
limiterCfg := limiter.Config{
MaxRequests: 100,
Window: 1 * time.Minute,
Algorithm: "sliding-window",
}
l, err := limiter.New(limiterCfg)
if err != nil {
panic(err)
}
app.Use(l.Middleware())
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Listen(":3000")
}
package main
import (
"time"
"github.com/NarmadaWeb/limiter/v2"
"github.com/gofiber/fiber/v2"
"github.com/redis/go-redis/v9"
)
func main() {
app := fiber.New()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
limiterCfg := limiter.Config{
RedisClient: rdb,
MaxRequests: 200,
Window: 5 * time.Minute,
Algorithm: "token-bucket",
}
l, err := limiter.New(limiterCfg)
if err != nil {
panic(err)
}
app.Use(l.Middleware())
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello with Redis!")
})
app.Listen(":3000")
}
Option | Type | Description |
---|---|---|
RedisClient |
*redis.Client |
Redis client instance (optional) |
RedisURL |
string |
Redis connection URL (alternative to RedisClient) |
MaxRequests |
int |
Maximum allowed requests per window |
Window |
time.Duration |
Duration of the rate limit window (e.g., 1*time.Minute) |
Algorithm |
string |
Rate limiting algorithm (token-bucket , sliding-window , fixed-window ) |
KeyGenerator |
func(*fiber.Ctx) string |
Custom function to generate rate limit keys (default: client IP) |
SkipSuccessful |
bool |
Don't count successful requests (status < 400) |
LimitReachedHandler |
fiber.Handler |
Custom handler when limit is reached |
ErrorHandler |
func(*fiber.Ctx, error) error |
Custom error handler for storage/configuration errors |
The middleware adds these standard headers to responses:
X-RateLimit-Limit
: Maximum requests allowedX-RateLimit-Remaining
: Remaining requests in current windowX-RateLimit-Reset
: Unix timestamp when limit resetsRateLimit-Policy
: Formal policy description
-
Token Bucket
- Smooth bursting allowed
- Gradually refills tokens at steady rate
- Good for evenly distributed loads
-
Sliding Window
- Precise request counting
- Tracks exact request timestamps
- Prevents bursts at window edges
-
Fixed Window
- Simple implementation
- Counts requests per fixed interval
- May allow bursts at window boundaries
See the examples directory for more implementations:
- Basic usage
- Redis integration
- Custom key generation
- Error handling
- Multiple limiters
We welcome contributions! Please see our CONTRIBUTING.md for guidelines.
MIT © NarmadaWeb - See LICENSE for details.