Documentation
¶
Index ¶
- type RingBufferRateLimiter
- func (r *RingBufferRateLimiter) Allow() bool
- func (r *RingBufferRateLimiter) MaxEvents() int
- func (r *RingBufferRateLimiter) SetMaxEvents(maxEvents int)
- func (r *RingBufferRateLimiter) SetWindow(window time.Duration)
- func (r *RingBufferRateLimiter) Stop()
- func (r *RingBufferRateLimiter) Wait(ctx context.Context) error
- func (r *RingBufferRateLimiter) Window() time.Duration
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RingBufferRateLimiter ¶
type RingBufferRateLimiter struct {
// contains filtered or unexported fields
}
RingBufferRateLimiter uses a ring to enforce rate limits consisting of a maximum number of events within a single sliding window of a given duration. An empty value is not valid; use NewRateLimiter to get one.
func NewRateLimiter ¶
func NewRateLimiter(maxEvents int, window time.Duration) *RingBufferRateLimiter
NewRateLimiter returns a rate limiter that allows up to maxEvents in a sliding window of size window. If maxEvents and window are both 0, or if maxEvents is non-zero and window is 0, rate limiting is disabled. This function panics if maxEvents is less than 0 or if maxEvents is 0 and window is non-zero, which is considered to be an invalid configuration, as it would never allow events.
func (*RingBufferRateLimiter) Allow ¶
func (r *RingBufferRateLimiter) Allow() bool
Allow returns true if the event is allowed to happen right now. It does not wait. If the event is allowed, a ticket is claimed.
func (*RingBufferRateLimiter) MaxEvents ¶
func (r *RingBufferRateLimiter) MaxEvents() int
MaxEvents returns the maximum number of events that are allowed within the sliding window.
func (*RingBufferRateLimiter) SetMaxEvents ¶
func (r *RingBufferRateLimiter) SetMaxEvents(maxEvents int)
SetMaxEvents changes the maximum number of events that are allowed in the sliding window. If the new limit is lower, the oldest events will be forgotten. If the new limit is higher, the window will suddenly have capacity for new reservations. It panics if maxEvents is 0 and window size is not zero.
func (*RingBufferRateLimiter) SetWindow ¶
func (r *RingBufferRateLimiter) SetWindow(window time.Duration)
SetWindow changes r's sliding window duration to window. Goroutines that are already blocked on a call to Wait() will not be affected. It panics if window is non-zero but the max event limit is 0.
func (*RingBufferRateLimiter) Stop ¶
func (r *RingBufferRateLimiter) Stop()
Stop cleans up r's scheduling goroutine.
func (*RingBufferRateLimiter) Wait ¶
func (r *RingBufferRateLimiter) Wait(ctx context.Context) error
Wait blocks until the event is allowed to occur. It returns an error if the context is cancelled.
func (*RingBufferRateLimiter) Window ¶
func (r *RingBufferRateLimiter) Window() time.Duration
Window returns the size of the sliding window.