-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.go
56 lines (46 loc) · 1.39 KB
/
bucket.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
51
52
53
54
55
56
package rates
import (
"time"
)
type BucketConfig struct {
MaxSize float64 `json:"maxSize"`
FillRate float64 `json:"fillRate"`
}
type Bucket interface {
// Add will add the provided number of tickets to this
// bucket, capping at the configured maximum bucket size.
// This method is thread safe.
Add(tickets float64)
// Remove will remove the provided number of tickets
// from this bucket, never falling below 0.
// This method is thread safe.
Remove(tickets float64)
// Take will attempt to retrieve a ticket from this
// bucket, returning false if there were no tickets
// available.
// This method is thread safe.
Take() bool
// TakeWhenAvailable acts similarly to Take, however it
// will return a channel which will emit a single value
// when a ticket has been successfully retrieved from
// the bucket.
TakeWhenAvailable() <-chan struct{}
// Refilled informs you with a message on the channel when
// a new ticket will have been added to the bucket. This
// ticket might not be available for your use if another
// goroutine claims it first, however it is guaranteed to
// exist for someone.
Refilled() <-chan struct{}
}
// NewBucket will create a rate bucket for the given
// config
func NewBucket(config *BucketConfig) Bucket {
if config == nil {
return &infiniteBucket{}
}
return &ticketBucket{
Tickets: config.MaxSize,
Config: config,
lastUpdate: time.Now(),
}
}