forked from rudderlabs/rudder-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_gorate.go
50 lines (39 loc) · 1.11 KB
/
memory_gorate.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
package throttling
import (
"sync"
"time"
gorate "golang.org/x/time/rate"
"github.com/rudderlabs/rudder-server/internal/throttling/cachettl"
)
type goRate struct {
mu sync.Mutex
store *cachettl.Cache[string, *gorate.Limiter]
}
func (r *goRate) limit(key string, cost, rate, periodInSecs int64) *goRateReservation {
r.mu.Lock()
defer r.mu.Unlock()
if r.store == nil {
r.store = cachettl.New[string, *gorate.Limiter]()
}
window := time.Duration(periodInSecs) * time.Second
l := r.store.Get(key)
if l == nil {
l = gorate.NewLimiter(gorate.Every(window), int(rate))
r.store.Put(key, l, window)
}
resWindow := time.Now().Add(window)
res := l.ReserveN(resWindow, int(cost))
return &goRateReservation{
reservation: res,
window: resWindow,
}
}
type goRateReservation struct {
reservation *gorate.Reservation
window time.Time
}
func (r *goRateReservation) Cancel() { r.reservation.Cancel() }
func (r *goRateReservation) CancelFuture() { r.reservation.CancelAt(r.window) }
func (r *goRateReservation) Allowed() bool {
return r.reservation.OK() && r.reservation.DelayFrom(r.window) == 0
}