ratelimit

package
v1.23.1-rc4 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2023 License: Apache-2.0, MIT Imports: 2 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRateLimitExceeded = errors.New("rate limit exceeded")

Functions

This section is empty.

Types

type Window

type Window struct {
	// contains filtered or unexported fields
}

Window is a time windows for counting events within a span of time. The windows slides forward in time so that it spans from the most recent event to size time in the past.

func NewWindow

func NewWindow(capacity int, size time.Duration) *Window

NewWindow creates a new Window that limits the number of events to maximum count of events within a duration of time. The capacity sets the maximum number of events, and size sets the span of time over which the events are counted.

func (*Window) Add

func (w *Window) Add() error

Add attempts to append a new timestamp into the current window. Previously added values that are not not within `size` difference from the value being added are first removed. Add fails if adding the value would cause the window to exceed capacity.

func (*Window) Cap

func (w *Window) Cap() int

Cap returns the maximum number of items the window can hold.

func (*Window) Len

func (w *Window) Len() int

Len returns the number of elements currently in the window.

func (*Window) Newest

func (w *Window) Newest() time.Time

Newest returns the newest timestamp in the window.

func (*Window) Oldest

func (w *Window) Oldest() time.Time

Oldest returns the oldest timestamp in the window.

func (*Window) Span

func (w *Window) Span() time.Duration

Span returns the distance from the first to the last item in the window.

Jump to

Keyboard shortcuts

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