slidingWindow

package
v1.6.5 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LocalWindow

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

func NewLocalWindow

func NewLocalWindow() *LocalWindow

func (*LocalWindow) AddCount

func (w *LocalWindow) AddCount(n int64)

func (*LocalWindow) Count

func (w *LocalWindow) Count() int64

func (*LocalWindow) Reset

func (w *LocalWindow) Reset(s time.Time, c int64)

func (*LocalWindow) Start

func (w *LocalWindow) Start() time.Time

type NewWindow

type NewWindow func() Window

type RateLimiter

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

func NewRateLimiter

func NewRateLimiter(size time.Duration, limit int64, newWindow NewWindow) *RateLimiter

func (*RateLimiter) Grant

func (rl *RateLimiter) Grant() bool

Grant shorthand for GrantN(time.Now(), 1)

func (*RateLimiter) GrantN

func (rl *RateLimiter) GrantN(now time.Time, n int64) bool

GrantN reports whether n events may happen at time now

func (*RateLimiter) Limit

func (rl *RateLimiter) Limit() int64

Limit returns the maximum events permitted to happen during one window size

func (*RateLimiter) SetLimit

func (rl *RateLimiter) SetLimit(limit int64)

func (*RateLimiter) Size

func (rl *RateLimiter) Size() time.Duration

Size returns the time duration of one window size

type Window

type Window interface {
	// Start returns the start boundary
	Start() time.Time

	// Count returns the accumulated count
	Count() int64

	// AddCount increments the accumulated count by n
	AddCount(n int64)

	// Reset sets the state of the window with the given settings
	Reset(s time.Time, c int64)
}

Window represents a fixed-window

Jump to

Keyboard shortcuts

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