Documentation
¶
Index ¶
Constants ¶
View Source
const ( // GCInterval is the interval at which per-key limiters are garbage // collected. Keys not accessed within two GC intervals are reclaimed. GCInterval = time.Minute )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Limiter ¶
type Limiter interface {
AllowN(now time.Time, n int) bool
WaitN(ctx context.Context, n int) error
Limit() rate.Limit
Burst() int
}
Limiter covers both blocking (WaitN) and non-blocking (AllowN) rate limiting. *rate.Limiter satisfies this interface.
type PerKeyLimiter ¶
type PerKeyLimiter struct {
// contains filtered or unexported fields
}
PerKeyLimiter maintains per-key rate limiters using a two-generation garbage collection pattern. Keys that are not accessed within two GC intervals have their limiters reclaimed.
func NewPerKeyLimiter ¶
func NewPerKeyLimiter(newLimiter func() Limiter, opts ...Option) *PerKeyLimiter
NewPerKeyLimiter creates a new PerKeyLimiter with the given limiter factory and options.
func (*PerKeyLimiter) GetLimiter ¶
func (l *PerKeyLimiter) GetLimiter(key string) Limiter
GetLimiter returns the rate limiter for the given key, creating one if necessary and performing garbage collection as needed.
func (*PerKeyLimiter) Now ¶
func (l *PerKeyLimiter) Now() time.Time
Now returns the current time according to the configured clock.
Click to show internal directories.
Click to hide internal directories.