-
Notifications
You must be signed in to change notification settings - Fork 23
/
bucket.go
40 lines (32 loc) · 1.02 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
package leakybucket
import (
"errors"
"time"
)
var (
// ErrorFull is returned when the amount requested to add exceeds the remaining space in the bucket.
ErrorFull = errors.New("add exceeds free capacity")
)
// Bucket interface for interacting with leaky buckets: https://en.wikipedia.org/wiki/Leaky_bucket
type Bucket interface {
// Capacity of the bucket.
Capacity() uint
// Remaining space in the bucket.
Remaining() uint
// Reset returns when the bucket will be drained.
Reset() time.Time
// Add to the bucket. MUST return bucket state after adding, even if an error was encountered
Add(uint) (BucketState, error)
}
// BucketState is a snapshot of a bucket's properties.
type BucketState struct {
Capacity uint
Remaining uint
Reset time.Time
}
// Storage interface for generating buckets keyed by a string.
type Storage interface {
// Create a bucket with a name, capacity, and rate.
// rate is how long it takes for full capacity to drain.
Create(name string, capacity uint, rate time.Duration) (Bucket, error)
}