-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimit.go
162 lines (134 loc) · 3.26 KB
/
limit.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
package main
import (
"fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"golang.org/x/time/rate"
)
func apiLimitIP() gin.HandlerFunc {
return func(c *gin.Context) {
ip := getIP(c)
ipLimiter := getVisitor(ipVisitors, ip)
if !ipLimiter.Allow() {
c.String(http.StatusTooManyRequests, http.StatusText(http.StatusTooManyRequests))
fmt.Println("Ip over rate limit", ip)
apiRateLimitedIP.Inc()
c.Abort()
return
}
c.Next()
}
}
func apiLimitAuth() gin.HandlerFunc {
return func(c *gin.Context) {
interval := rate.Every(30 * time.Second)
defaultLimiter := rate.NewLimiter(interval, 1)
ip := getIP(c)
limiter := getVisitorWithLimiter(authVisitors, ip, defaultLimiter)
if !limiter.Allow() {
c.String(http.StatusTooManyRequests, http.StatusText(http.StatusTooManyRequests))
fmt.Println("Ip over rate limit", ip)
apiRateLimitedIP.Inc()
c.Abort()
return
}
c.Next()
}
}
func apiLimitKey() gin.HandlerFunc {
return func(c *gin.Context) {
key := c.GetHeader("api-key")
apiLimiter := getVisitor(apiVisitors, key)
if !apiLimiter.Allow() {
c.String(http.StatusTooManyRequests, http.StatusText(http.StatusTooManyRequests))
fmt.Println("Api key over rate limit", key)
apiRateLimitedKey.Inc()
c.Abort()
return
}
c.Next()
}
}
func apiLclLimit(limit rate.Limit) gin.HandlerFunc {
limiter := rate.NewLimiter(limit, 1)
return func(c *gin.Context) {
if !limiter.Allow() {
c.String(http.StatusTooManyRequests, http.StatusText(http.StatusTooManyRequests))
c.Abort()
return
}
c.Next()
}
}
func apiRmtLimit(limit rate.Limit) gin.HandlerFunc {
limiter := rate.NewLimiter(limit, 1)
return func(c *gin.Context) {
if !limiter.Allow() {
c.String(http.StatusOK, "Server rate limited :(")
c.Abort()
return
}
c.Next()
}
}
func apiNoLimit() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
}
}
// Stolen from here https://www.alexedwards.net/blog/how-to-rate-limit-http-requests
type visitor struct {
limiter *rate.Limiter
lastSeen time.Time
}
type visitors struct {
visitors map[string]*visitor
mu sync.Mutex
}
var apiVisitors = &visitors{}
var ipVisitors = &visitors{}
var authVisitors = &visitors{}
func getVisitor(vs *visitors, key string) *rate.Limiter {
return getVisitorWithLimiter(vs, key, rate.NewLimiter(2, 3))
}
func getVisitorWithLimiter(vs *visitors, key string, limiter *rate.Limiter) *rate.Limiter {
vs.mu.Lock()
defer vs.mu.Unlock()
v, exists := vs.visitors[key]
if !exists {
vs.visitors[key] = &visitor{limiter, time.Now()}
return limiter
}
v.lastSeen = time.Now()
return v.limiter
}
func setupVisitors() {
apiVisitors.visitors = make(map[string]*visitor)
ipVisitors.visitors = make(map[string]*visitor)
authVisitors.visitors = make(map[string]*visitor)
}
func cleanupVisitors(vs *visitors) {
vs.mu.Lock()
for key, v := range vs.visitors {
if time.Since(v.lastSeen) > 3*time.Minute {
delete(vs.visitors, key)
}
}
vs.mu.Unlock()
}
func cleanupVisitorsRoutine() {
for {
time.Sleep(time.Minute)
cleanupVisitors(apiVisitors)
cleanupVisitors(ipVisitors)
cleanupVisitors(authVisitors)
}
}
func getIP(c *gin.Context) string {
forwarded := c.GetHeader("X-Forwarded-For")
ips := strings.Split(forwarded, ",")
return ips[len(ips)-1]
}