-
Notifications
You must be signed in to change notification settings - Fork 49
/
limiters.go
61 lines (49 loc) · 1.44 KB
/
limiters.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
57
58
59
60
61
// Package limiters provides general purpose rate limiter implementations.
package limiters
import (
"errors"
"log"
"time"
)
var (
// ErrLimitExhausted is returned by the Limiter in case the number of requests overflows the capacity of a Limiter.
ErrLimitExhausted = errors.New("requests limit exhausted")
// ErrRaceCondition is returned when there is a race condition while saving a state of a rate limiter.
ErrRaceCondition = errors.New("race condition detected")
)
// Logger wraps the Log method for logging.
type Logger interface {
// Log logs the given arguments.
Log(v ...interface{})
}
// StdLogger implements the Logger interface.
type StdLogger struct{}
// NewStdLogger creates a new instance of StdLogger.
func NewStdLogger() *StdLogger {
return &StdLogger{}
}
// Log delegates the logging to the std logger.
func (l *StdLogger) Log(v ...interface{}) {
log.Println(v...)
}
// Clock encapsulates a system Clock.
// Used
type Clock interface {
// Now returns the current system time.
Now() time.Time
}
// SystemClock implements the Clock interface by using the real system clock.
type SystemClock struct {
}
// NewSystemClock creates a new instance of SystemClock.
func NewSystemClock() *SystemClock {
return &SystemClock{}
}
// Now returns the current system time.
func (c *SystemClock) Now() time.Time {
return time.Now()
}
// Sleep blocks (sleeps) for the given duration.
func (c *SystemClock) Sleep(d time.Duration) {
time.Sleep(d)
}