forked from 0xProject/0x-mesh
/
fake_rate_limiter.go
45 lines (38 loc) · 1.17 KB
/
fake_rate_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
package ratelimit
import (
"context"
"sync"
"time"
)
// fakeLimiter is a fake RateLimiter that always allows a request through
type fakeLimiter struct {
currentUTCCheckpoint time.Time // Start of current UTC 24hr period
grantedInLast24hrsUTC int // Number of granted requests issued in last 24hr UTC
mu sync.Mutex
}
// NewUnlimited returns a new RateLimiter without any limits. It will always
// allow requests immediately. It still keeps track of the number of requests
// that are allowed.
func NewUnlimited() RateLimiter {
return &fakeLimiter{
currentUTCCheckpoint: GetUTCMidnightOfDate(time.Now()),
grantedInLast24hrsUTC: 0,
}
}
// Start starts the fake rateLimiter
func (f *fakeLimiter) Start(ctx context.Context, checkpointInterval time.Duration) error {
return nil
}
// Wait blocks until the rateLimiter allows for another request to be sent
func (f *fakeLimiter) Wait(ctx context.Context) error {
f.mu.Lock()
f.grantedInLast24hrsUTC++
f.mu.Unlock()
return nil
}
func (f *fakeLimiter) getGrantedInLast24hrsUTC() int {
return f.grantedInLast24hrsUTC
}
func (f *fakeLimiter) getCurrentUTCCheckpoint() time.Time {
return f.currentUTCCheckpoint
}