-
Notifications
You must be signed in to change notification settings - Fork 5
/
limiter.go
60 lines (51 loc) · 1.38 KB
/
limiter.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package ginlimiter
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/juju/ratelimit"
"time"
"fmt"
)
type RateKeyFunc func(ctx *gin.Context) (string, error)
type RateLimiterMiddleware struct {
fillInterval time.Duration
capacity int64
ratekeygen RateKeyFunc
limiters map[string]*ratelimit.Bucket
}
func (r *RateLimiterMiddleware) get(ctx *gin.Context) (*ratelimit.Bucket, error) {
key, err := r.ratekeygen(ctx)
if err != nil {
return nil, err
}
if limiter, existed := r.limiters[key]; existed {
return limiter, nil
}
limiter := ratelimit.NewBucketWithQuantum(r.fillInterval, r.capacity, r.capacity)
r.limiters[key] = limiter
return limiter, nil
}
func (r *RateLimiterMiddleware) Middleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
limiter, err := r.get(ctx)
if err != nil || limiter.TakeAvailable(1) == 0 {
if err == nil {
err = errors.New("Too many requests")
}
ctx.AbortWithError(429, err)
} else {
ctx.Writer.Header().Set("X-RateLimit-Remaining", fmt.Sprintf("%d", limiter.Available()))
ctx.Writer.Header().Set("X-RateLimit-Limit", fmt.Sprintf("%d", limiter.Capacity()))
ctx.Next()
}
}
}
func NewRateLimiter(interval time.Duration, capacity int64, keyGen RateKeyFunc) *RateLimiterMiddleware {
limiters := make(map[string]*ratelimit.Bucket)
return &RateLimiterMiddleware{
interval,
capacity,
keyGen,
limiters,
}
}