limit

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PerDay

func PerDay(maxAttempts int) http.Limit

func PerDays

func PerDays(decayDays, maxAttempts int) http.Limit

func PerHour

func PerHour(maxAttempts int) http.Limit

func PerHours

func PerHours(decayHours, maxAttempts int) http.Limit

func PerMinute

func PerMinute(maxAttempts int) http.Limit

func PerMinutes

func PerMinutes(decayMinutes, maxAttempts int) http.Limit

Types

type Bucket

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

Bucket is an internal wrapper around a taker.

func NewBucket

func NewBucket(tokens uint64, interval time.Duration) *Bucket

NewBucket creates a new Bucket from the given tokens and interval.

type Limit

type Limit struct {
	// The rate limit signature key.
	Key string
	// The store instance.
	Store Store
	// The response generator callback.
	ResponseCallback func(ctx http.Context)
}

func NewLimit

func NewLimit(maxAttempts, decayMinutes int) *Limit

func (*Limit) By

func (r *Limit) By(key string) http.Limit

func (*Limit) Response

func (r *Limit) Response(callable func(ctx http.Context)) http.Limit

type Store

type Store interface {
	// Take takes a token from the given key if available, returning:
	//
	// - the configured limit size
	// - the number of remaining tokens in the interval
	// - the server time when new tokens will be available
	// - whether the take was successful
	// - any errors that occurred while performing the take - these should be
	//   backend errors (e.g. connection failures); Take() should never return an
	//   error for a bucket.
	//
	// If "ok" is false, the take was unsuccessful and the caller should NOT
	// service the request.
	//
	// See the note about keys on the interface documentation.
	Take(ctx context.Context, key string) (tokens, remaining, reset uint64, ok bool, err error)

	// Get gets the current limit and remaining tokens for the provided key. It
	// does not change any of the values.
	Get(ctx context.Context, key string) (tokens, remaining uint64, err error)

	// Set configures the limit at the provided key. If a limit already exists, it
	// is overwritten. This also sets the number of tokens in the bucket to the
	// limit.
	Set(ctx context.Context, key string, tokens uint64, interval time.Duration) error

	// Burst adds more tokens to the key's current bucket until the next interval
	// tick. This will allow the current bucket tick to exceed the maximum number
	// maximum ticks until the next interval.
	Burst(ctx context.Context, key string, tokens uint64) error
}

Store is an interface for limiter storage backends.

Keys should be hash, sanitized, or otherwise scrubbed of identifiable information they will be given to the store in plaintext. If you're rate limiting by IP address, for example, the IP address would be stored in the storage system in plaintext. This may be undesirable in certain situations, like when the store is a public database. In those cases, you should hash or HMAC the key before passing giving it to the store. If you want to encrypt the value, you must use homomorphic encryption to ensure the value always encrypts to the same ciphertext.

func NewStore

func NewStore(tokens uint64, interval time.Duration) (Store, error)

Jump to

Keyboard shortcuts

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