-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy paththrottler.go
117 lines (96 loc) · 2.98 KB
/
throttler.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package throttler
import (
"errors"
"math"
"net/http"
"strconv"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"github.com/throttled/throttled/v2"
)
func init() {
context.SetHandlerName("github.com/iris-contrib/middleware/throttler.*", "iris-contrib.throttler")
}
var (
// DefaultDeniedHandler is the default DeniedHandler for an
// RateLimiter. It returns a 429 status code with a generic
// message.
DefaultDeniedHandler = func(ctx iris.Context) {
ctx.StopWithText(http.StatusTooManyRequests, "limit exceeded")
}
// DefaultError is the default Error function for an RateLimiter.
// It returns a 500 status code with a generic message.
DefaultError = func(ctx iris.Context, err error) {
ctx.StopWithError(http.StatusInternalServerError, err)
}
)
// RateLimiter faciliates using a Limiter to limit HTTP requests.
type RateLimiter struct {
// DeniedHandler is called if the request is disallowed. If it is
// nil, the DefaultDeniedHandler variable is used.
DeniedHandler iris.Handler
// Error is called if the RateLimiter returns an error. If it is
// nil, the DefaultErrorFunc is used.
Error func(ctx iris.Context, err error)
// Limiter is call for each request to determine whether the
// request is permitted and update internal state. It must be set.
RateLimiter throttled.RateLimiterCtx
// VaryBy is called for each request to generate a key for the
// limiter. If it is nil, all requests use an empty string key.
VaryBy interface {
Key(*http.Request) string
}
}
// RateLimit is an Iris middleware that limits incoming requests.
// Requests that are not limited will be passed to the handler
// unchanged. Limited requests will be passed to the DeniedHandler.
// X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset and
// Retry-After headers will be written to the response based on the
// values in the RateLimitResult.
func (t *RateLimiter) RateLimit(ctx iris.Context) {
if t.RateLimiter == nil {
t.error(ctx, errors.New("you must set a RateLimiter on RateLimiter"))
}
var k string
if t.VaryBy != nil {
k = t.VaryBy.Key(ctx.Request())
}
limited, context, err := t.RateLimiter.RateLimitCtx(ctx, k, 1)
if err != nil {
t.error(ctx, err)
return
}
setRateLimitHeaders(ctx, context)
if limited {
dh := t.DeniedHandler
if dh == nil {
dh = DefaultDeniedHandler
}
dh(ctx)
return
}
ctx.Next()
}
func (t *RateLimiter) error(ctx iris.Context, err error) {
e := t.Error
if e == nil {
e = DefaultError
}
e(ctx, err)
}
func setRateLimitHeaders(ctx iris.Context, context throttled.RateLimitResult) {
if v := context.Limit; v >= 0 {
ctx.Header("X-RateLimit-Limit", strconv.Itoa(v))
}
if v := context.Remaining; v >= 0 {
ctx.Header("X-RateLimit-Remaining", strconv.Itoa(v))
}
if v := context.ResetAfter; v >= 0 {
vi := int(math.Ceil(v.Seconds()))
ctx.Header("X-RateLimit-Reset", strconv.Itoa(vi))
}
if v := context.RetryAfter; v >= 0 {
vi := int(math.Ceil(v.Seconds()))
ctx.Header("Retry-After", strconv.Itoa(vi))
}
}