-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit.go
More file actions
35 lines (28 loc) · 726 Bytes
/
Copy pathlimit.go
File metadata and controls
35 lines (28 loc) · 726 Bytes
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
package rate
import (
"time"
)
type Limit struct {
period time.Duration
durationPerToken time.Duration
}
type LimitFunc[TInput any] func(input TInput) Limit
// NewLimit creates a new rate with the given count and period.
// For example, to create a rate of 10 requests per second, use:
//
// limit := rate.NewLimit(10, time.Second)
func NewLimit(tokens int64, period time.Duration) Limit {
return Limit{
period: period,
durationPerToken: period / time.Duration(tokens),
}
}
func (l Limit) Count() int64 {
return int64(l.period / l.durationPerToken)
}
func (l Limit) Period() time.Duration {
return l.period
}
func (l Limit) DurationPerToken() time.Duration {
return l.durationPerToken
}