Documentation ¶
Index ¶
- Variables
- func ErrRateLimited(bucketName BucketName) error
- func IsRateLimitErrorWithBucketName(err error, bucketName BucketName) bool
- type BucketName
- type BucketSpec
- type FailedReservation
- type Limiter
- func (l *Limiter) Allow(ctx context.Context, spec BucketSpec) (*FailedReservation, error)
- func (l *Limiter) Cancel(ctx context.Context, r *Reservation)
- func (l *Limiter) GetTimeToAct(ctx context.Context, spec BucketSpec) (*time.Time, error)
- func (l *Limiter) Reserve(ctx context.Context, spec BucketSpec) (*Reservation, *FailedReservation, error)
- func (l *Limiter) ReserveN(ctx context.Context, spec BucketSpec, n int) (*Reservation, *FailedReservation, error)
- type LimiterGlobal
- func (l *LimiterGlobal) Allow(ctx context.Context, spec BucketSpec) (*FailedReservation, error)
- func (l *LimiterGlobal) Cancel(ctx context.Context, r *Reservation)
- func (l *LimiterGlobal) GetTimeToAct(ctx context.Context, spec BucketSpec) (*time.Time, error)
- func (l *LimiterGlobal) Reserve(ctx context.Context, spec BucketSpec) (*Reservation, *FailedReservation, error)
- func (l *LimiterGlobal) ReserveN(ctx context.Context, spec BucketSpec, n int) (*Reservation, *FailedReservation, error)
- type Logger
- type Reservation
- type Storage
- type StorageRedis
Constants ¶
This section is empty.
Variables ¶
var BucketSpecDisabled = BucketSpec{Enabled: false}
var DependencySet = wire.NewSet( NewLogger, wire.Struct(new(Limiter), "*"), wire.Bind(new(Storage), new(*StorageRedis)), NewAppStorageRedis, )
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 NewGlobalBucketSpec ¶
func NewGlobalBucketSpec(e config.RateLimitsEnvironmentConfigEntry, name BucketName, 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 ¶
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 ¶
func (*LimiterGlobal) Allow ¶
func (l *LimiterGlobal) Allow(ctx context.Context, spec BucketSpec) (*FailedReservation, error)
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 ¶
func (l *LimiterGlobal) Reserve(ctx context.Context, spec BucketSpec) (*Reservation, *FailedReservation, error)
Reserve is a shortcut of ReserveN(1).
func (*LimiterGlobal) ReserveN ¶
func (l *LimiterGlobal) 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 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 StorageRedis ¶
func NewAppStorageRedis ¶
func NewAppStorageRedis(redis *appredis.Handle) *StorageRedis
func NewGlobalStorageRedis ¶
func NewGlobalStorageRedis(redis *globalredis.Handle) *StorageRedis