limiter

package
v0.0.0-...-4eb781d Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

type Limiter interface {
	// Allow reports whether an event with the given token can happen
	// within the configured maximum rate. Returned is the current
	// event count and whether the event can happen.
	Allow(token string) (count int64, ok bool)
}

Limiter is an interface implemented by all rate limiting schemes.

type Sliding

type Sliding struct {
	// WindowDuration defines the width of the sliding window
	// where events are counted against the maximum.
	WindowDuration time.Duration

	// WindowMaximum is the maximum number of events that can
	// happen within the configured window.
	WindowMaximum int64

	// Redis is the storage backend for per-token rate limit counts.
	Redis redis.Cmdable

	// RedisPrefix will prefix all keys used by the Sliding limiter.
	RedisPrefix string
}

Sliding implements a Redis-backed Limiter for a sliding window.

The sliding window is implemented using Redis sorted sets, where each event is a new entry in the sorted set with its timestamp as a score. Events older than the window are evicted and the set eventually expires.

func (*Sliding) Allow

func (s *Sliding) Allow(token string) (count int64, ok bool)

Allow reports whether an even with the given token can happen within the configured maximum rate. Returned is the current event count and whether the event can happen.

If redis is unavailable, Allow will allow all tokens temporarily.

type Tiered

type Tiered []Limiter

Tiered is a limiter where multiple Limits can be applied over a range of configurations for a single token.

func (Tiered) Allow

func (t Tiered) Allow(token string) (count int64, ok bool)

Allow reports when an event can occur with regards to all configured rate limiters. The count returned will be the highest of all counts reported.

Jump to

Keyboard shortcuts

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