ratelimit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DimensionConfig

type DimensionConfig struct {
	// qps conifg
	QPS      float64
	QPSBurst int
	// concurrency config
	ConcurrencyLimit uint64
}

DimensionConfig is the limit dimension config of one label

type Limiter

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

Limiter is a controller for the request rate.

func NewLimiter

func NewLimiter() *Limiter

NewLimiter returns a global limiter which can be updated in the later.

func (*Limiter) Allow

func (l *Limiter) Allow(label string) bool

Allow is used to check whether it has enough token.

func (*Limiter) ConcurrencyUnlimit

func (l *Limiter) ConcurrencyUnlimit(label string)

ConcurrencyUnlimit deletes concurrency limiter of the given label

func (*Limiter) GetConcurrencyLimiterStatus

func (l *Limiter) GetConcurrencyLimiterStatus(label string) (limit uint64, current uint64)

GetConcurrencyLimiterStatus returns the status of a given label's concurrency limiter.

func (*Limiter) GetQPSLimiterStatus

func (l *Limiter) GetQPSLimiterStatus(label string) (limit rate.Limit, burst int)

GetQPSLimiterStatus returns the status of a given label's QPS limiter.

func (*Limiter) IsInAllowList

func (l *Limiter) IsInAllowList(label string) bool

IsInAllowList returns whether this label is in allow list. If returns true, the given label won't be limited

func (*Limiter) QPSUnlimit

func (l *Limiter) QPSUnlimit(label string)

QPSUnlimit deletes QPS limiter of the given label

func (*Limiter) Release

func (l *Limiter) Release(label string)

Release is used to refill token. It may be not uesful for some limiters because they will refill automatically

func (*Limiter) Update

func (l *Limiter) Update(label string, opts ...Option) UpdateStatus

Update is used to update Ratelimiter with Options

type Option

type Option func(string, *Limiter) UpdateStatus

Option is used to create a limiter with the optional settings. these setting is used to add a kind of limiter for a service

func AddLabelAllowList

func AddLabelAllowList() Option

AddLabelAllowList adds a label into allow list. It means the given label will not be limited

func UpdateConcurrencyLimiter

func UpdateConcurrencyLimiter(limit uint64) Option

UpdateConcurrencyLimiter creates a concurrency limiter for a given label if it doesn't exist.

func UpdateDimensionConfig

func UpdateDimensionConfig(cfg *DimensionConfig) Option

UpdateDimensionConfig creates QPS limiter and concurrency limiter for a given label by config if it doesn't exist.

func UpdateQPSLimiter

func UpdateQPSLimiter(limit float64, burst int) Option

UpdateQPSLimiter creates a QPS limiter for a given label if it doesn't exist.

type RateLimiter

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

RateLimiter is a rate limiter based on `golang.org/x/time/rate`. It implements `Available` function which is not included in `golang.org/x/time/rate`. Note: AvailableN will increase the wait time of WaitN.

func NewRateLimiter

func NewRateLimiter(r float64, b int) *RateLimiter

NewRateLimiter returns a new Limiter that allows events up to rate r (it means limiter refill r token per second) and permits bursts of at most b tokens.

func (*RateLimiter) Allow

func (l *RateLimiter) Allow() bool

Allow is same as `rate.Limiter.Allow`.

func (*RateLimiter) AllowN

func (l *RateLimiter) AllowN(n int) bool

AllowN is same as `rate.Limiter.AllowN`.

func (*RateLimiter) Available

func (l *RateLimiter) Available(n int) bool

Available returns whether limiter has enough tokens. Note: Available will increase the wait time of WaitN.

func (*RateLimiter) Burst

func (l *RateLimiter) Burst() int

Burst returns the maximum burst size. Burst is the maximum number of tokens that can be consumed in a single call to Allow, Reserve, or Wait, so higher Burst values allow more events to happen at once. A zero Burst allows no events, unless limit == Inf.

func (*RateLimiter) Limit

func (l *RateLimiter) Limit() rate.Limit

Limit returns the maximum overall event rate.

func (*RateLimiter) SetBurst

func (l *RateLimiter) SetBurst(burst int)

SetBurst is shorthand for SetBurstAt(time.Now(), newBurst).

func (*RateLimiter) SetLimit

func (l *RateLimiter) SetLimit(limit rate.Limit)

SetLimit is shorthand for SetLimitAt(time.Now(), newLimit).

func (*RateLimiter) WaitN

func (l *RateLimiter) WaitN(ctx context.Context, n int) error

WaitN blocks until lim permits n events to happen. It returns an error if n exceeds the Limiter's burst size, the Context is canceled, or the expected wait time exceeds the Context's Deadline. The burst limit is ignored if the rate limit is Inf.

type UpdateStatus

type UpdateStatus uint32

UpdateStatus is flags for updating limiter config.

const (

	// QPSNoChange shows that limiter's config isn't changed.
	QPSNoChange UpdateStatus = 1 << iota
	// QPSChanged shows that limiter's config is changed and not deleted.
	QPSChanged
	// QPSDeleted shows that limiter's config is deleted.
	QPSDeleted
	// ConcurrencyNoChange shows that limiter's config isn't changed.
	ConcurrencyNoChange
	// ConcurrencyChanged shows that limiter's config is changed and not deleted.
	ConcurrencyChanged
	// ConcurrencyDeleted shows that limiter's config is deleted.
	ConcurrencyDeleted
	// InAllowList shows that limiter's config isn't changed because it is in in allow list.
	InAllowList
)

Flags for limiter.

Jump to

Keyboard shortcuts

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