According to Stripe's rate-limiters practice, use Redis Server
& Lua Script
to implement a rate limiter based on token bucket algorithm.
go get "github.com/wallstreetcn/rate"
import (
rate "github.com/wallstreetcn/rate/redis"
)
// initialize redis.
rate.SetRedis(&rate.ConfigRedis{
Host: "127.0.0.1",
Port: 6379,
Auth: "",
}
// setup a 1 ops/s rate limiter.
limiter := rate.NewLimiter(Every(time.Second), 1, "a-sample-operation")
if limiter.Allow() {
// serve the user request
} else {
// reject the user request
}
// setup a 1000 ops/s rate limiter.
limiter := rate.NewLimiter(Every(time.Second/time.Duration(1000)), 1000, "a-sample-operation")
if limiter.Allow() {
// serve the user request
} else {
// reject the user request
}