ratelimit

package
v0.0.0-...-81dc835 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BucketSpecDisabled = BucketSpec{Enabled: false}
View Source
var RateLimited = apierrors.TooManyRequest.WithReason("RateLimited")

Functions

func ErrRateLimited

func ErrRateLimited(bucketName BucketName) error

func IsRateLimitErrorWithBucketName

func IsRateLimitErrorWithBucketName(err error, bucketName BucketName) bool

Types

type BucketName

type BucketName string

type BucketSpec

type BucketSpec struct {
	Name      BucketName
	Arguments []string
	IsGlobal  bool

	Enabled bool
	Period  time.Duration
	Burst   int
}

func NewBucketSpec

func NewBucketSpec(config *config.RateLimitConfig, name BucketName, args ...string) BucketSpec

func NewCooldownSpec

func NewCooldownSpec(name BucketName, period time.Duration, args ...string) BucketSpec

func (BucketSpec) Key

func (s BucketSpec) Key() string

type FailedReservation

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

func (*FailedReservation) Error

func (r *FailedReservation) Error() error

func (*FailedReservation) GetTimeToAct

func (r *FailedReservation) GetTimeToAct() time.Time

type Limiter

type Limiter struct {
	Logger  Logger
	Storage Storage
	AppID   config.AppID
	Config  *config.RateLimitsFeatureConfig
}

Limiter implements rate limiting using a simple token bucket algorithm. Consumers take token from a bucket every operation, and tokens are refilled periodically.

func (*Limiter) Allow

func (l *Limiter) Allow(ctx context.Context, spec BucketSpec) (*FailedReservation, error)

Allow is a shortcut of Reserve, when you do not plan to cancel the reservation.

func (*Limiter) Cancel

func (l *Limiter) Cancel(ctx context.Context, r *Reservation)

Cancel cancels a reservation.

func (*Limiter) GetTimeToAct

func (l *Limiter) GetTimeToAct(ctx context.Context, spec BucketSpec) (*time.Time, error)

GetTimeToAct allows you to check what is the earliest time you can retry.

func (*Limiter) Reserve

func (l *Limiter) Reserve(ctx context.Context, spec BucketSpec) (*Reservation, *FailedReservation, error)

Reserve is a shortcut of ReserveN(1).

func (*Limiter) ReserveN

func (l *Limiter) ReserveN(ctx context.Context, spec BucketSpec, n int) (*Reservation, *FailedReservation, error)

ReserveN is the general entry point. If you ever need to pass n=0, you should use GetTimeToAct() instead.

type LimiterGlobal

type LimiterGlobal struct {
	Logger  Logger
	Storage Storage
}

func (*LimiterGlobal) Allow

Allow is a shortcut of Reserve, when you do not plan to cancel the reservation.

func (*LimiterGlobal) Cancel

func (l *LimiterGlobal) Cancel(ctx context.Context, r *Reservation)

Cancel cancels a reservation.

func (*LimiterGlobal) GetTimeToAct

func (l *LimiterGlobal) GetTimeToAct(ctx context.Context, spec BucketSpec) (*time.Time, error)

GetTimeToAct allows you to check what is the earliest time you can retry.

func (*LimiterGlobal) Reserve

Reserve is a shortcut of ReserveN(1).

func (*LimiterGlobal) ReserveN

ReserveN is the general entry point. If you ever need to pass n=0, you should use GetTimeToAct() instead.

type Logger

type Logger struct{ *log.Logger }

func NewLogger

func NewLogger(lf *log.Factory) Logger

type Reservation

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

func (*Reservation) PreventCancel

func (r *Reservation) PreventCancel()

PreventCancel prevents r from being Cancel(). The typical usage is like r := ... defer Cancel(r) ... Discover a situation that r must not be canceled. r.PreventCancel()

type Storage

type Storage interface {
	Update(ctx context.Context, key string, period time.Duration, burst int, delta int) (ok bool, timeToAct time.Time, err error)
}

type StorageRedis

type StorageRedis struct {
	Redis *redis.Handle
}

func NewAppStorageRedis

func NewAppStorageRedis(redis *appredis.Handle) *StorageRedis

func NewGlobalStorageRedis

func NewGlobalStorageRedis(redis *globalredis.Handle) *StorageRedis

func (*StorageRedis) Update

func (s *StorageRedis) Update(ctx context.Context, key string, period time.Duration, burst int, delta int) (ok bool, timeToAct time.Time, err error)

Jump to

Keyboard shortcuts

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