Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewHeapLimiterConstructorFunc ¶
NewHeapLimiterConstructorFunc returns a function that creates a new HeapLimiter.
Types ¶
type HeapLimiter ¶
type HeapLimiter struct {
// contains filtered or unexported fields
}
HeapLimiter is an implementation of the Limiter interface using a min-heap. This would be used if more accuracy is needed because the heap is sorted by the timestamp value not the order of the requests like with the ring buffer. This is also more expensive than the ring buffer.
func NewHeapLimiter ¶
func NewHeapLimiter(size int, window time.Duration) *HeapLimiter
NewHeapLimiter returns a new HeapLimiter.
func (*HeapLimiter) Accept ¶
func (hl *HeapLimiter) Accept(now time.Time)
Accept implements the Limiter interface for the HeapLimiter. This is used when the request is accepted and added to the heap.
func (*HeapLimiter) LimitDetails ¶
func (hl *HeapLimiter) LimitDetails() (int, time.Duration)
LimitDetails returns the size and window of the limiter.
type Limiter ¶
type Limiter interface { // Try checks if it's within the rate limits. Try(time.Time) bool // Accept logs a new request to the limiter. Accept(time.Time) // TryAccept checks if it's within the rate limits and logs a new request to the limiter. TryAccept(time.Time) bool // LimitDetails returns the size and window of the limiter. LimitDetails() (int, time.Duration) }
Limiter is the interface that abstracts the limitations functionality.
type RingLimiter ¶
type RingLimiter struct {
// contains filtered or unexported fields
}
RingLimiter is an implementation of the Limiter interface using a ring buffer. This is more performant than the HeapLimiter as it doesn't need to sort the requests by value and it uses a fixed size array.
func NewRingLimiter ¶
func NewRingLimiter(size int, window time.Duration) *RingLimiter
NewRingLimiterInstance creates a RingLimiter.
func (*RingLimiter) Accept ¶
func (rl *RingLimiter) Accept(now time.Time)
Accept adds a new request to the ring buffer.
func (*RingLimiter) LimitDetails ¶
func (rl *RingLimiter) LimitDetails() (int, time.Duration)
LimitDetails returns the size and window of the limiter.