limiter

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LeakyBucketTyp   = "leak_bucket"
	SildingWindowTyp = "silding_window"
)

Variables

View Source
var ErrRateLimited error = errors.New("rate limited")

Functions

This section is empty.

Types

type LeakyBucketRateLimiter

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

并发的 rate limiter。 * 基于队列的 leaky bucket 算法实现。 * 参见 https://en.wikipedia.org/wiki/Leaky_bucket leaky bucket 有两种实现方式 * As a meter: 此与 token bucket 等价 * As a queue: 此具有更严格的限速,能够避免 burst flow * * * 测试结果(MacBook Pro 13 英寸): * 1K QPS: 1 req per 1 milli sec ( 1ms) * 1w QPS: 1 req per 100 micro sec ( 0.1ms) timer 可以支持此精度。 * 2w QPS: 1 req per 50 micro sec (0.05ms) timer 可以支持此精度。 * 10w QPS: 1 req per 10 micro sec (0.01ms) timer 开始出现误差。 * 为确保精度,建议不要超过 10w QPS。 * (此问题并非无解,是有优化方案的。)

func NewLeakyBucketRateLimiter

func NewLeakyBucketRateLimiter(qpsThreshold int64) *LeakyBucketRateLimiter

func (*LeakyBucketRateLimiter) ChangeQpsThreshold

func (lbrl *LeakyBucketRateLimiter) ChangeQpsThreshold(newQpsThreshold int64)

func (*LeakyBucketRateLimiter) Close

func (lbrl *LeakyBucketRateLimiter) Close()

func (*LeakyBucketRateLimiter) Limit

func (lbrl *LeakyBucketRateLimiter) Limit() error

type Limiter

type Limiter interface {
	Limit() error
	ChangeQpsThreshold(newQpsThreshold int64)
	Close()
}

func NewLimiter

func NewLimiter(qpsThreshold int64, limiterTyp string) (Limiter, error)

type SlidingWindowRateLimiter

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

基于滑动窗口的,并发安全的限流器。

func NewSlidingWindowRateLimiter

func NewSlidingWindowRateLimiter(qpsThreshold int64) *SlidingWindowRateLimiter

func (*SlidingWindowRateLimiter) ChangeQpsThreshold

func (swrl *SlidingWindowRateLimiter) ChangeQpsThreshold(newQpsThreshold int64)

func (*SlidingWindowRateLimiter) Close

func (swrl *SlidingWindowRateLimiter) Close()

func (*SlidingWindowRateLimiter) Limit

func (swrl *SlidingWindowRateLimiter) Limit() error

如果被限流,则返回 ErrRateLimited;未被限流,则返回 nil

Jump to

Keyboard shortcuts

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