The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/middleware/rate_limit.go

24 lines
676 B

package middleware
import (
"time"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/web"
"golang.org/x/time/rate"
)
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) web.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
}
}
}