/
ratelimiter.go
47 lines (42 loc) · 1.32 KB
/
ratelimiter.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
package common
import (
"fmt"
"sync"
"time"
)
type RateLimiter struct {
maxRequests int
interval int
currentRequests int
lastRequest time.Time
lock sync.Mutex
}
func NewRateLimiter(maxRequests int, perSecond int) *RateLimiter {
return &RateLimiter{
maxRequests: maxRequests,
interval: perSecond,
lastRequest: time.Now()}
}
func (rateLimiter *RateLimiter) RespectRateLimit() {
var resetCounter bool
duration := time.Now().UTC().Sub(rateLimiter.lastRequest)
coolOffPeriod := time.Duration(rateLimiter.interval) * time.Second
if rateLimiter.currentRequests >= rateLimiter.maxRequests && duration <= coolOffPeriod {
//rateLimiter.ctx.GetLogger().Debugf("[RateLimiter] Waiting %s for rate limit cool-off: \n", coolOffPeriod)
fmt.Printf("[RateLimiter] Waiting %s for rate limit cool-off\n", coolOffPeriod)
time.Sleep(coolOffPeriod)
resetCounter = true
}
rateLimiter.lock.Lock()
if resetCounter {
rateLimiter.currentRequests = 1
} else {
rateLimiter.currentRequests++
}
rateLimiter.lastRequest = time.Now().UTC()
rateLimiter.lock.Unlock()
}
func (rateLimiter *RateLimiter) String() string {
return fmt.Sprintf("maxRequests: %d, interval: %ds, lastRequest: %s, currentRequests: %d",
rateLimiter.maxRequests, rateLimiter.interval, rateLimiter.lastRequest, rateLimiter.currentRequests)
}