-
Notifications
You must be signed in to change notification settings - Fork 0
/
ratelimit.go
44 lines (37 loc) · 855 Bytes
/
ratelimit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package middleware
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
lru "github.com/hashicorp/golang-lru/v2"
"golang.org/x/time/rate"
)
type KeyFuc func(*gin.Context) string
var (
cache, _ = lru.New[string, *rate.Limiter](128)
)
// RateLimiter is a middleware that limits the request rate based on a given key
func GinRateLimiter(key KeyFuc, maxToken int, interval time.Duration) gin.HandlerFunc {
return func(c *gin.Context) {
k := key(c)
v, ok := cache.Get(k)
if !ok {
v = rate.NewLimiter(rate.Every(interval), maxToken)
cache.Add(k, v)
} else {
if v.Allow() {
c.Next()
} else {
c.AbortWithStatus(http.StatusTooManyRequests)
}
}
}
}
func RateLimiter(first, every int, interval time.Duration, f func()) {
s := rate.Sometimes{
First: first,
Every: every,
Interval: interval,
}
s.Do(f)
}