You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
742 B
27 lines
742 B
3 years ago
|
package rate
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// IDLimiter is the limiter interface to limit incoming traffics per ID
|
||
|
type IDLimiter interface {
|
||
|
AllowN(id string, n int) bool
|
||
|
WaitN(ctx context.Context, id string, n int) error
|
||
|
}
|
||
|
|
||
|
// timer is the interface of time used for testing
|
||
|
type timer interface {
|
||
|
now() time.Time
|
||
|
since(time.Time) time.Duration
|
||
|
newTicker(time.Duration) *time.Ticker
|
||
|
}
|
||
|
|
||
|
// timerImpl is the timer for production. Use package time directly
|
||
|
type timerImpl struct{}
|
||
|
|
||
|
func (t timerImpl) now() time.Time { return time.Now() }
|
||
|
func (t timerImpl) since(t2 time.Time) time.Duration { return time.Since(t2) }
|
||
|
func (t timerImpl) newTicker(d time.Duration) *time.Ticker { return time.NewTicker(d) }
|