Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrResourceNotAvailable = errors.New("naza.ratelimit: resource not available")
View Source
var ErrTokenNotEnough = errors.New("naza.ratelimit: token not enough")
Functions ¶
This section is empty.
Types ¶
type LeakyBucket ¶ added in v0.8.0
type LeakyBucket struct {
// contains filtered or unexported fields
}
漏桶
func NewLeakyBucket ¶ added in v0.8.0
func NewLeakyBucket(intervalMs int) *LeakyBucket
@param intervalMs 多长时间以上,允许获取到一个资源,单位毫秒
func (*LeakyBucket) MaybeAvailableIntervalMs ¶ added in v0.19.0
func (lb *LeakyBucket) MaybeAvailableIntervalMs() int64
最快可获取到资源距离当前的时长, 但是不保证获取时一定能抢到 返回0,说明可以获取,返回非0,则是对应的时长,单位毫秒
func (*LeakyBucket) TryAquire ¶ added in v0.8.0
func (lb *LeakyBucket) TryAquire() error
尝试获取资源,获取成功返回nil,获取失败返回ErrResourceNotAvailable 如果获取失败,上层可自由选择多久后重试或丢弃本次任务
func (*LeakyBucket) WaitUntilAquire ¶ added in v0.8.0
func (lb *LeakyBucket) WaitUntilAquire()
阻塞直到获取到资源
type RateLimiter ¶ added in v0.8.0
type RateLimiter interface { TryAquire() error WaitUntilAquire() }
type TokenBucket ¶ added in v0.8.0
type TokenBucket struct {
// contains filtered or unexported fields
}
令牌桶
func NewTokenBucket ¶ added in v0.8.0
func NewTokenBucket(capacity int, prodTokenIntervalMs int, prodTokenNumEveryInterval int) *TokenBucket
@param capacity: 桶容量大小 @param prodTokenIntervalMs: 生产令牌的时间间隔,单位毫秒 @param prodTokenNumEveryInterval: 每次生产多少个令牌
func (*TokenBucket) TryAquire ¶ added in v0.8.0
func (tb *TokenBucket) TryAquire() error
func (*TokenBucket) TryAquireWithNum ¶ added in v0.8.0
func (tb *TokenBucket) TryAquireWithNum(num int) error
尝试获取相应数量的令牌,获取成功返回nil,获取失败返回ErrTokenNotEnough 如果获取失败,上层可自由选择多久后重试或丢弃本次任务
func (*TokenBucket) WaitUntilAquire ¶ added in v0.8.0
func (tb *TokenBucket) WaitUntilAquire()
func (*TokenBucket) WaitUntilAquireWithNum ¶ added in v0.8.0
func (tb *TokenBucket) WaitUntilAquireWithNum(num int)
阻塞直到获取到相应数量的令牌
Click to show internal directories.
Click to hide internal directories.