ratelimiter

package
v0.0.0-...-e4d3d8e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

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

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL