This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
limiter.go
176 lines (144 loc) · 3.84 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package limiter
import (
"errors"
"io"
"net/http"
"strconv"
"sync"
"time"
"github.com/aofei/air"
"github.com/patrickmn/go-cache"
)
// BodySizeGasConfig is a set of configurations for the `BodySizeGas`.
type BodySizeGasConfig struct {
MaxBytes int64
ErrRequestEntityTooLarge error
Skippable func(*air.Request, *air.Response) bool
}
// BodySizeGas returns an `air.Gas` that is used to limit ervery request's body
// size based on the bsgc. It prevents clients from accidentally or maliciously
// sending a large request and wasting server resources.
func BodySizeGas(bsgc BodySizeGasConfig) air.Gas {
if bsgc.ErrRequestEntityTooLarge == nil {
bsgc.ErrRequestEntityTooLarge = errors.New(
http.StatusText(http.StatusRequestEntityTooLarge),
)
}
return func(next air.Handler) air.Handler {
return func(req *air.Request, res *air.Response) error {
if bsgc.Skippable != nil && bsgc.Skippable(req, res) {
return next(req, res)
}
if req.ContentLength > bsgc.MaxBytes {
res.Status = http.StatusRequestEntityTooLarge
return bsgc.ErrRequestEntityTooLarge
}
req.Body = &maxBytesBody{
bsgc: bsgc,
reqBody: req.Body,
res: res,
}
return next(req, res)
}
}
}
// maxBytesReader is similar to the `io.LimitReader` but is intended for
// limiting the size of incoming request bodies.
type maxBytesBody struct {
sync.Mutex
bsgc BodySizeGasConfig
reqBody io.ReadCloser
res *air.Response
cl int64
}
// Read implements the `io.Reader`.
func (mbb *maxBytesBody) Read(b []byte) (n int, err error) {
mbb.Lock()
defer mbb.Unlock()
if rl := mbb.bsgc.MaxBytes - mbb.cl; rl > 0 {
if int64(len(b)) > rl {
b = b[:rl]
}
n, err = mbb.reqBody.Read(b)
} else {
return 0, mbb.bsgc.ErrRequestEntityTooLarge
}
mbb.cl += int64(n)
if err == nil && mbb.bsgc.MaxBytes-mbb.cl <= 0 {
if mbb.res.Written {
mbb.res.Status = http.StatusRequestEntityTooLarge
}
err = mbb.bsgc.ErrRequestEntityTooLarge
}
return
}
// Close implements the `io.Closer`.
func (mbb *maxBytesBody) Close() error {
return mbb.reqBody.Close()
}
// RateGasConfig is a set of configurations for the `RateGas`.
type RateGasConfig struct {
MaxRequests int64
ResetInterval time.Duration
UseClientAddress bool
ErrTooManyRequests error
Skippable func(*air.Request, *air.Response) bool
counterCache *cache.Cache
}
// RateGas returns an `air.Gas` that is used to limit request's rate based on
// the rgc.
func RateGas(rgc RateGasConfig) air.Gas {
if rgc.ErrTooManyRequests == nil {
rgc.ErrTooManyRequests = errors.New(
http.StatusText(http.StatusTooManyRequests),
)
}
rgc.counterCache = cache.New(rgc.ResetInterval, time.Minute)
return func(next air.Handler) air.Handler {
return func(req *air.Request, res *air.Response) error {
if rgc.Skippable != nil && rgc.Skippable(req, res) {
return next(req, res)
}
if rgc.MaxRequests <= 0 || rgc.ResetInterval <= 0 {
return next(req, res)
}
host := ""
if rgc.UseClientAddress {
host = req.ClientHost()
} else {
host = req.RemoteHost()
}
_, e, ok := rgc.counterCache.GetWithExpiration(host)
if !ok || e.Before(time.Now()) {
rgc.counterCache.SetDefault(host, int64(0))
e = time.Now().Add(rgc.ResetInterval)
}
count, err := rgc.counterCache.IncrementInt64(host, 1)
if err != nil {
return err
}
remaining := rgc.MaxRequests - count
reached := remaining < 0
if reached {
remaining = 0
}
res.Header.Set(
"X-RateLimit-Limit",
strconv.FormatInt(rgc.MaxRequests, 10),
)
res.Header.Set(
"X-RateLimit-Remaining",
strconv.FormatInt(remaining, 10),
)
res.Header.Set(
"X-RateLimit-Reset",
strconv.FormatInt(e.Unix(), 10),
)
if reached {
res.Status = http.StatusTooManyRequests
return rgc.ErrTooManyRequests
}
return next(req, res)
}
}
}