Request level utility wrapper for github.com/ulule/limiter.
- Limit for a particular route
- Custom limit rules per route
- Limit requests based on headers, cookies or any context present in the request (or plug custom ml models)
- Supports optional global limiting, additionally
- Graceful degradation
- Tied to Redis, for now
Using HTTP StdLib
- Install package using Go Modules
$ go get github.com/alter123/go-ratelimit
- Globally initialize the limiter by providing Redis config (& other optional config)
func RateLimiterInit(config RateLimitConfig) {
goratelimit.Init(limiter.LimiterOptions{
Redis: *redis.Options,
// global params to process (e.g. limit on user id, instead of ipAddr)
FuncFetchFromContext: limiter.FuncFetchParamFromContext,
// request level params (based on query params etc, passed via `SetAdditionalContextParam`)
FuncAdditionalContext: limiter.FuncFetchParamFromContext,
})
}
- Create middleware implementation (based upon the framework)
func LimitHandler(limiter *limiter.Limiter) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if lmtCtx, err := goratelimit.LimitByRequest(limiter, r); err == nil && lmtCtx.Reached {
http.Error(w, "Limit exceeded", http.StatusTooManyRequests)
return
} else if err != nil {
// log error, returned from the limiter
}
h.ServeHTTP(w, r)
})
}
- Plug limiter config into route
rateLimitHandler := LimitHandler(goratelimit.NewLimiter(2, 10*time.Second))
http.Handle("/", rateLimitHandler(http.HandlerFunc(index)))
- Running tests
$ go test
-
Report issues at GitHub issue tracker
-
To add a new feature, create a new GitHub pull request