limit

package
v1.15.12 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: MIT Imports: 8 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PerDay

func PerDay(maxAttempts int) contractshttp.Limit

func PerDays

func PerDays(decayDays, maxAttempts int) contractshttp.Limit

func PerHour

func PerHour(maxAttempts int) contractshttp.Limit

func PerHours

func PerHours(decayHours, maxAttempts int) contractshttp.Limit

func PerMinute

func PerMinute(maxAttempts int) contractshttp.Limit

func PerMinutes

func PerMinutes(decayMinutes, maxAttempts int) contractshttp.Limit

Types

type Bucket added in v1.15.0

type Bucket struct {
	// AvailableTokens is the current point-in-time number of tokens remaining.
	AvailableTokens uint64

	// Interval is the time at which ticking should occur.
	Interval time.Duration

	// LastTick is the last clock tick, used to re-calculate the number of tokens on the Bucket.
	LastTick uint64

	// MaxTokens is the maximum number of tokens permitted on the Bucket at any time.
	// The number of available tokens will never exceed this value.
	MaxTokens uint64

	// StartTime is the number of nanoseconds from unix epoch when this Bucket was initially created.
	StartTime uint64
}

Bucket is an internal wrapper around a taker.

func NewBucket added in v1.15.0

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 store instance.
	Store contractshttp.Store
	// The response generator callback.
	ResponseCallback func(ctx contractshttp.Context)
	// The rate limit signature key.
	Key string
}

func NewLimit

func NewLimit(maxAttempts, decayMinutes int) *Limit

func (*Limit) By

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

func (*Limit) Response

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

type Store added in v1.15.0

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

func NewStore added in v1.15.0

func NewStore(cache cache.Cache, json foundation.Json, tokens uint64, interval time.Duration) *Store

func (*Store) Burst added in v1.15.0

func (r *Store) Burst(ctx context.Context, key string, tokens uint64) error

Burst adds the provided value to the Bucket's currently available tokens.

func (*Store) Get added in v1.15.0

func (r *Store) Get(ctx context.Context, key string) (tokens uint64, remaining uint64, err error)

Get retrieves the information about the key.

func (*Store) Set added in v1.15.0

func (r *Store) Set(ctx context.Context, key string, tokens uint64, interval time.Duration) error

Set configures the Bucket-specific tokens and interval.

func (*Store) Take added in v1.15.0

func (r *Store) Take(ctx context.Context, key string) (tokens uint64, remaining uint64, reset uint64, ok bool, err error)

Take attempts to remove a token from the named key. If the take is successful, it returns true, otherwise false. It also returns the configured limit, remaining tokens, and reset time.

Jump to

Keyboard shortcuts

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