ratelimit

package
v0.0.0-...-be629c2 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	LimiterAlreadyExists         = errors.New("limiter already exists")
	LimiterTypeNotSupportedError = errors.New("limiter type not supported")
	NoReaminTokenError           = errors.New("The token has been used up")
)

Functions

func GetLimiterType

func GetLimiterType() (res []string)

Types

type ConcurrentLimit

type ConcurrentLimit struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*ConcurrentLimit) GetSpeed

func (limiter *ConcurrentLimit) GetSpeed() int64

func (*ConcurrentLimit) GetTimeout

func (limiter *ConcurrentLimit) GetTimeout() time.Duration

func (*ConcurrentLimit) GetVolumn

func (limiter *ConcurrentLimit) GetVolumn() int

func (*ConcurrentLimit) SetRate

func (limiter *ConcurrentLimit) SetRate(volume int, _ int64)

func (*ConcurrentLimit) SetTimeout

func (limiter *ConcurrentLimit) SetTimeout(timeout time.Duration)

func (*ConcurrentLimit) Take

func (limiter *ConcurrentLimit) Take() error

func (*ConcurrentLimit) TakeWithTimeout

func (limiter *ConcurrentLimit) TakeWithTimeout() error

type LimiterType

type LimiterType string

type QpsRateLimiter

type QpsRateLimiter struct {
	sync.RWMutex
	Limiter *rate.Limiter
	Timeout time.Duration
}

func (*QpsRateLimiter) GetSpeed

func (r *QpsRateLimiter) GetSpeed() int64

func (*QpsRateLimiter) GetTimeout

func (r *QpsRateLimiter) GetTimeout() time.Duration

-1代表没有配置超时

func (*QpsRateLimiter) GetVolumn

func (r *QpsRateLimiter) GetVolumn() int

func (*QpsRateLimiter) SetRate

func (r *QpsRateLimiter) SetRate(init int, speed int64)

init:初始化的个数 speed:一秒生成多少个 token

func (*QpsRateLimiter) SetTimeout

func (r *QpsRateLimiter) SetTimeout(time time.Duration)

func (*QpsRateLimiter) Take

func (r *QpsRateLimiter) Take() error

func (*QpsRateLimiter) TakeWithTimeout

func (r *QpsRateLimiter) TakeWithTimeout(timeout time.Duration) error

type RateLimiter

type RateLimiter interface {
	Take() error
	TakeWithTimeout(time.Duration) error
	SetRate(int, int64) // 设置速率
	GetVolumn() int
	GetSpeed() int64
	GetTimeout() time.Duration
	SetTimeout(time.Duration)
}

* 限流器在整个程序内部中会被频繁创建,由此采用工厂设计模式

func Build

func Build(t LimiterType) (RateLimiter, error)

func NewQpsRateLimiter

func NewQpsRateLimiter() RateLimiter

type RateLimiterFactory

type RateLimiterFactory func() RateLimiter

Jump to

Keyboard shortcuts

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