grafana/pkg/middleware/rate_limit.go
Domas 7d9a528184
Logging: rate limit fronted logging endpoint (#29272)
Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
2020-12-09 16:22:24 +01:00

26 lines
666 B
Go

package middleware
import (
"time"
"golang.org/x/time/rate"
"gopkg.in/macaron.v1"
"github.com/grafana/grafana/pkg/models"
)
type getTimeFn func() time.Time
// RateLimit is a very basic rate limiter.
// Will allow average of "rps" requests per second over an extended period of time, with max "burst" requests at the same time.
// getTime should return the current time. For non-testing purposes use time.Now
func RateLimit(rps, burst int, getTime getTimeFn) macaron.Handler {
l := rate.NewLimiter(rate.Limit(rps), burst)
return func(c *models.ReqContext) {
if !l.AllowN(getTime(), 1) {
c.JsonApiErr(429, "Rate limit reached", nil)
return
}
}
}