Documentation ¶
Index ¶
- Variables
- func LeakyBucketMiddleware(rate int) gin.HandlerFunc
- func Reader(r io.Reader, bucket *Bucket) io.Reader
- func TokenBucketMiddleware(fillInterval time.Duration, cap, quantum int64) gin.HandlerFunc
- func Writer(w io.Writer, bucket *Bucket) io.Writer
- type Bucket
- func NewBucket(fillInterval time.Duration, capacity int64) *Bucket
- func NewBucketWithClock(fillInterval time.Duration, capacity int64, clock Clock) *Bucket
- func NewBucketWithQuantum(fillInterval time.Duration, capacity, quantum int64) *Bucket
- func NewBucketWithQuantumAndClock(fillInterval time.Duration, capacity, quantum int64, clock Clock) *Bucket
- func NewBucketWithRate(rate float64, capacity int64) *Bucket
- func NewBucketWithRateAndClock(rate float64, capacity int64, clock Clock) *Bucket
- func (tb *Bucket) Available() int64
- func (tb *Bucket) Capacity() int64
- func (tb *Bucket) Rate() float64
- func (tb *Bucket) Take(count int64) time.Duration
- func (tb *Bucket) TakeAvailable(count int64) int64
- func (tb *Bucket) TakeMaxDuration(count int64, maxWait time.Duration) (time.Duration, bool)
- func (tb *Bucket) Wait(count int64)
- func (tb *Bucket) WaitMaxDuration(count int64, maxWait time.Duration) bool
- type Clock
- type Limiter
- type Option
Constants ¶
This section is empty.
Variables ¶
var DefaultLeakyBucketMap *leakyMap
var DefaultTokenBucketMap *tokenMap
Functions ¶
func 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 个令牌,而不是一个令牌。
Types ¶
type Bucket ¶
type Bucket struct {
// contains filtered or unexported fields
}
func NewBucketWithClock ¶
func NewBucketWithQuantum ¶
func NewBucketWithRate ¶
func (*Bucket) TakeAvailable ¶
func (*Bucket) TakeMaxDuration ¶
type Limiter ¶
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 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 ¶
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.