-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
store.go
109 lines (89 loc) 路 2.25 KB
/
store.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
package ratelimiter
import (
"fmt"
"math"
"sync"
"time"
"goyave.dev/goyave/v3"
)
type limiter struct {
resetsAt time.Time
config Config
counter int
mx sync.Mutex
}
func newLimiter(config Config) *limiter {
return &limiter{
config: config,
counter: 0,
resetsAt: time.Now().Add(config.QuotaDuration),
}
}
func (l *limiter) validateAndUpdate(response *goyave.Response) bool {
l.mx.Lock()
defer l.mx.Unlock()
valid := !l.hasExceededRequestQuota()
l.counter++
l.updateResponseHeaders(response)
return valid
}
func (l *limiter) updateResponseHeaders(response *goyave.Response) {
response.Header().Set(
"RateLimit-Limit",
fmt.Sprintf("%v, %v;w=%v", l.config.RequestQuota, l.config.RequestQuota, l.config.QuotaDuration.Seconds()),
)
response.Header().Set(
"RateLimit-Remaining",
fmt.Sprintf("%v", l.getRemainingRequestQuota()),
)
response.Header().Set(
"RateLimit-Reset",
fmt.Sprintf("%v", l.getSecondsToQuotaReset()),
)
}
func (l *limiter) hasExceededRequestQuota() bool {
return l.counter >= l.config.RequestQuota
}
func (l *limiter) getRemainingRequestQuota() int {
count := l.config.RequestQuota - l.counter
if count < 0 {
count = 0
}
return count
}
func (l *limiter) getSecondsToQuotaReset() float64 {
return -math.Round(time.Since(l.resetsAt).Seconds())
}
type limiterStore struct {
store map[interface{}]*limiter
mx sync.Mutex
}
func newLimiterStore() limiterStore {
return limiterStore{
store: make(map[interface{}]*limiter),
}
}
func (ls *limiterStore) set(key interface{}, limiter *limiter) {
ls.store[key] = limiter
// Remove expired entries from the map to avoid store map growing too much
// Warning though, go maps aren't shrunk after key deletion,
// see https://github.com/golang/go/issues/20135
time.AfterFunc(limiter.config.QuotaDuration, func() {
ls.mx.Lock()
defer ls.mx.Unlock()
delete(ls.store, key)
})
}
func (ls *limiterStore) get(key interface{}, config Config) *limiter {
ls.mx.Lock()
defer ls.mx.Unlock()
if l, ok := ls.store[key]; ok {
return l
}
// If doesn't exist, return a new one
// This prevents potential concurrent replacement when two requests from the
// same client occur at the same time
limiter := newLimiter(config)
ls.set(key, limiter)
return limiter
}