This package is a fork of go-redis/redis_rate.
This go-redis/redis_rate
was based on rwz/redis-gcra and
implements GCRA (aka leaky bucket) for
rate limiting based on Redis. The code requires Redis version 3.2 or newer since it relies on
replicate_commands
feature.
- Pipeling: Check if you can allow multiple limits or concurrency checks all at once in a single Redis pipelined call.
- Concurrency Limits: Limit the number of concurrent requests.
- General cleanup for modern Go.
package redis_rate_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"github.com/ductone/redis_rate/v11"
)
func ExampleNewLimiter() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
_ = rdb.FlushDB(ctx).Err()
limiter := redis_rate.New(rdb)
res, err := limiter.Allow(ctx, "project:123", redis_rate.PerSecond(10))
if err != nil {
panic(err)
}
fmt.Println("allowed", res.Allowed, "remaining", res.Remaining)
// Output: allowed 1 remaining 9
}