A high-performance IP-based rate limiter middleware for Gin framework using atomic operations and token bucket algorithm.
- ⚡ Microsecond precision for token refill intervals
- 🔒 Concurrent-safe storage with atomic operations
- 🚀 Single IP throughput up to 100,000+ QPS
- 📡 Event hooks for monitoring rate limit events
- 🛠 Customizable storage backends (in-memory/Redis)
- ⚖️ Weak rejection mode for edge cases
Minimal Go version: 1.18
go get github.com/BouncyElf/bucket
package main
import (
"github.com/BouncyElf/bucket"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
conf := bucket.NewDefaultConfig()
r.Use(bucket.BucketHandler(conf))
r.GET("/api", func(c *gin.Context) {
c.String(200, "Request allowed")
})
r.Run(":8080")
}
Parameter | Type | Default | Description |
---|---|---|---|
TokenNumber |
int64 |
10000 | Maximum tokens per bucket |
RefillMicrosecond |
int64 |
100 | Token refill interval in microseconds |
WeakRejectionMode |
bool |
false | Allow non-IP requests to pass |
EventHook |
Gin HandlerFunc |
nil | Callback for rate limit events |
- Single IP Throughput: 120,000+ req/sec (8-core CPU)
- P99 Latency: < 0.3ms
- Memory Usage: 50MB per 1M IPs
Benchmarked using wrk
on AWS c5.2xlarge instance.
Monitor rate limiting events through Gin context:
conf.EventHook = func(c *gin.Context) {
switch c.Get(bucket.BucketEventKey) {
case bucket.EventPass:
// Handle allowed request
case bucket.EventRejected:
// Handle rate-limited request
}
}
// Uses concurrent-map with sharded locking
conf.Storage = &bucket.defaultStorage{m: cmap.New()}
Implement the Storage
interface:
type RedisStorage struct {
client *redis.Client
}
func (r *RedisStorage) GetOrCreate(key string, creator func() *bucket.AtomicBucket) *bucket.AtomicBucket {
// Implement Redis-based storage logic
}
Run unit tests
go test -v ./...
Run benchmarks
go test -bench=. -cpu=8
Implementation inspired by:
- Token bucket algorithm in golang.org/x/time/rate
- Atomic optimization patterns from Go official docs
- Distributed storage patterns
PRs welcome! Please ensure:
- Add test coverage for new features
- Update documentation accordingly
- Maintain 100% atomic operation safety
MIT LICENSE Copyright (c) 2025 BouncyElf