ratelimit

package
v0.0.0-...-8585d0b Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2022 License: LGPL-2.1 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLeakyBucketMap *leakyMap
View Source
var DefaultTokenBucketMap *tokenMap

Functions

func LeakyBucketMiddleware

func LeakyBucketMiddleware(rate int) gin.HandlerFunc

漏桶 一秒能过多少请求,qps

func Reader

func Reader(r io.Reader, bucket *Bucket) io.Reader

Reader returns a reader that is rate limited by the given token bucket. Each token in the bucket represents one byte.

func TokenBucketMiddleware

func TokenBucketMiddleware(fillInterval time.Duration, cap, quantum int64) gin.HandlerFunc

令牌桶 NewBucketWithQuantum和普通的 NewBucket() 的区别是,每次向桶中放令牌时,是放 quantum 个令牌,而不是一个令牌。

func Writer

func Writer(w io.Writer, bucket *Bucket) io.Writer

Writer returns a reader that is rate limited by the given token bucket. Each token in the bucket represents one byte.

Types

type Bucket

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

func NewBucket

func NewBucket(fillInterval time.Duration, capacity int64) *Bucket

func NewBucketWithClock

func NewBucketWithClock(fillInterval time.Duration, capacity int64, clock Clock) *Bucket

func NewBucketWithQuantum

func NewBucketWithQuantum(fillInterval time.Duration, capacity, quantum int64) *Bucket

func NewBucketWithQuantumAndClock

func NewBucketWithQuantumAndClock(fillInterval time.Duration, capacity, quantum int64, clock Clock) *Bucket

func NewBucketWithRate

func NewBucketWithRate(rate float64, capacity int64) *Bucket

func NewBucketWithRateAndClock

func NewBucketWithRateAndClock(rate float64, capacity int64, clock Clock) *Bucket

func (*Bucket) Available

func (tb *Bucket) Available() int64

func (*Bucket) Capacity

func (tb *Bucket) Capacity() int64

func (*Bucket) Rate

func (tb *Bucket) Rate() float64

func (*Bucket) Take

func (tb *Bucket) Take(count int64) time.Duration

func (*Bucket) TakeAvailable

func (tb *Bucket) TakeAvailable(count int64) int64

func (*Bucket) TakeMaxDuration

func (tb *Bucket) TakeMaxDuration(count int64, maxWait time.Duration) (time.Duration, bool)

func (*Bucket) Wait

func (tb *Bucket) Wait(count int64)

func (*Bucket) WaitMaxDuration

func (tb *Bucket) WaitMaxDuration(count int64, maxWait time.Duration) bool

type Clock

type Clock interface {
	Now() time.Time
	Sleep(d time.Duration)
}

type Limiter

type Limiter interface {
	// Take should block to make sure that the RPS is met.
	Take() time.Time
}

Limiter is used to rate-limit some process, possibly across goroutines. The process is expected to call Take() before every iteration, which may block to throttle the goroutine.

func New

func New(rate int, opts ...Option) Limiter

New returns a Limiter that will limit to the given RPS.

func NewUnlimited

func NewUnlimited() Limiter

NewUnlimited returns a RateLimiter that is not limited.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option configures a Limiter.

var WithoutSlack Option = slackOption(0)

WithoutSlack configures the limiter to be strict and not to accumulate previously "unspent" requests for future bursts of traffic.

func Per

func Per(per time.Duration) Option

Per allows configuring limits for different time windows.

The default window is one second, so New(100) produces a one hundred per second (100 Hz) rate limiter.

New(2, Per(60*time.Second)) creates a 2 per minute rate limiter.

func WithClock

func WithClock(clock Clock) Option

WithClock returns an option for ratelimit.New that provides an alternate Clock implementation, typically a mock Clock for testing.

func WithSlack

func WithSlack(slack int) Option

WithSlack configures custom slack. Slack allows the limiter to accumulate "unspent" requests for future bursts of traffic.

Jump to

Keyboard shortcuts

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