-
Notifications
You must be signed in to change notification settings - Fork 0
/
quota.go
46 lines (39 loc) · 876 Bytes
/
quota.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
package ratelimiter
import (
"math"
"strconv"
"time"
"golang.org/x/time/rate"
)
var InfQuota Quota = Quota{
reqs: math.MaxInt32,
interval: rate.InfDuration,
_limit: rate.Inf,
_rpm: "unlimited",
_retryAfter: "",
}
type Quota struct {
reqs int
interval time.Duration
_rpm string
_limit rate.Limit
_retryAfter string
}
func (q Quota) limit() rate.Limit {
return q._limit
}
func (q Quota) rpm() string {
return q._rpm
}
func (q Quota) retryAfter() string {
return q._retryAfter
}
func NewQuota(reqs int, interval time.Duration) Quota {
return Quota{
reqs: reqs,
interval: interval,
_limit: rate.Limit(float64(reqs) / interval.Seconds()),
_rpm: strconv.Itoa(int(math.Floor(float64(reqs) / interval.Minutes()))),
_retryAfter: strconv.FormatInt(int64(interval.Seconds()), 10),
}
}