-
Notifications
You must be signed in to change notification settings - Fork 1
/
limiter.go
209 lines (187 loc) · 5.11 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package ksmux
import (
"net"
"net/http"
"regexp"
"strings"
"time"
"github.com/kamalshkeir/kmap"
"golang.org/x/time/rate"
)
// LimitStrategy defines how we identify requests for rate limiting
type LimitStrategy int
const (
LimitByIP LimitStrategy = iota
LimitByPath
LimitByHeader
LimitByIPAndPath
)
type limiterClient struct {
limiter *rate.Limiter
lastSeen time.Time
totalHits int64
}
var (
limited = kmap.New[string, *limiterClient]()
limiterQuit chan struct{}
limiterUsed = false
defCheckEvery = 5 * time.Minute
defBlockDuration = 10 * time.Minute
defRateEvery = 10 * time.Minute
defBurstsN = 100
defMessage = "TOO MANY REQUESTS"
)
type PathConfig struct {
Pattern string // Path pattern to match (supports wildcards with *)
RateEvery time.Duration // Rate limit for this path
BurstsN int // Burst limit for this path
}
type ConfigLimiter struct {
Message string // default "TOO MANY REQUESTS"
RateEvery time.Duration // default 10 min
BurstsN int // default 100
CheckEvery time.Duration // default 5 min
BlockDuration time.Duration // default 10 min
OnLimitReached func(c *Context) // Custom handler for when limit is reached
Strategy LimitStrategy // Strategy for rate limiting
HeaderName string // Header name for LimitByHeader strategy
PathConfigs []PathConfig // Path-specific configurations
}
func getPathConfig(path string, configs []PathConfig) *PathConfig {
for _, conf := range configs {
if conf.Pattern == path {
return &conf
}
// Handle wildcard patterns
if strings.Contains(conf.Pattern, "*") {
pattern := strings.ReplaceAll(conf.Pattern, "*", ".*")
matched, err := regexp.MatchString("^"+pattern+"$", path)
if err == nil && matched {
return &conf
}
}
}
return nil
}
func getLimiterKey(c *Context, strategy LimitStrategy, headerName string) string {
switch strategy {
case LimitByPath:
return c.Request.URL.Path
case LimitByHeader:
return c.Request.Header.Get(headerName)
case LimitByIPAndPath:
ip, _, _ := net.SplitHostPort(c.Request.RemoteAddr)
return ip + ":" + c.Request.URL.Path
default: // LimitByIP
ip, _, _ := net.SplitHostPort(c.Request.RemoteAddr)
return ip
}
}
func Limiter(conf *ConfigLimiter) func(http.Handler) http.Handler {
if conf == nil {
conf = &ConfigLimiter{
CheckEvery: defCheckEvery,
BlockDuration: defBlockDuration,
RateEvery: defRateEvery,
BurstsN: defBurstsN,
Message: defMessage,
Strategy: LimitByIP,
}
} else {
if conf.CheckEvery == 0 {
conf.CheckEvery = defCheckEvery
}
if conf.BlockDuration == 0 {
conf.BlockDuration = defBlockDuration
}
if conf.RateEvery == 0 {
conf.RateEvery = defRateEvery
}
if conf.BurstsN == 0 {
conf.BurstsN = defBurstsN
}
if conf.Message == "" {
conf.Message = defMessage
}
if conf.OnLimitReached == nil {
conf.OnLimitReached = func(c *Context) {
c.Status(http.StatusTooManyRequests).Text(conf.Message)
}
}
}
ticker := time.NewTicker(conf.CheckEvery)
limiterQuit = make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
limited.Range(func(key string, value *limiterClient) bool {
if time.Since(value.lastSeen) > conf.BlockDuration {
go limited.Delete(key)
}
return true
})
case <-limiterQuit:
ticker.Stop()
return
}
}
}()
limiterUsed = true
return func(handler http.Handler) http.Handler {
return Handler(func(c *Context) {
key := getLimiterKey(c, conf.Strategy, conf.HeaderName)
if key == "" {
c.SetStatus(http.StatusInternalServerError)
return
}
var ll *rate.Limiter
rateEvery := conf.RateEvery
burstsN := conf.BurstsN
// Check for path-specific configuration
if len(conf.PathConfigs) > 0 {
if pathConf := getPathConfig(c.Request.URL.Path, conf.PathConfigs); pathConf != nil {
rateEvery = pathConf.RateEvery
burstsN = pathConf.BurstsN
}
}
if lim, found := limited.Get(key); !found {
ll = rate.NewLimiter(rate.Every(rateEvery), burstsN)
} else {
ll = lim.limiter
}
limited.Set(key, &limiterClient{
limiter: ll,
lastSeen: time.Now(),
})
if !ll.Allow() {
conf.OnLimitReached(c)
return
}
handler.ServeHTTP(c.ResponseWriter, c.Request)
})
}
}
// // Path-based rate limiting example
// router.Use(ksmux.Limiter(&ksmux.ConfigLimiter{
// Strategy: ksmux.LimitByPath,
// PathConfigs: []ksmux.PathConfig{
// {
// Pattern: "/api/*", // All API routes
// RateEvery: time.Second,
// BurstsN: 10, // 10 requests per second
// },
// {
// Pattern: "/static/*", // Static files
// RateEvery: time.Minute,
// BurstsN: 1000, // 1000 requests per minute
// },
// },
// }))
// // API key based limiting
// router.Use(ksmux.Limiter(&ksmux.ConfigLimiter{
// Strategy: ksmux.LimitByHeader,
// HeaderName: "X-API-Key",
// RateEvery: time.Hour,
// BurstsN: 1000, // 1000 requests per hour per API key
// }))