limit

package
v0.0.0-...-512269d Latest Latest
Warning

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

Go to latest
Published: May 29, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

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

func NewLimiter

func NewLimiter(name string, interval time.Duration, burst int) *Limiter

NewLimiter interval 没隔多少时间添加一个令牌, 初始桶的容量 // interval 表示放桶频率,是每秒放入的令牌数量 burst 表示桶的大小 Limiter burst size 构建一个限流器

func (*Limiter) Allow

func (l *Limiter) Allow() bool

使用时,每次都调用了 Allow() 方法 Allow is shorthand for AllowN(time.Now(), 1). 就是 AllowN(now,1) 的别名

func (*Limiter) AllowN

func (l *Limiter) AllowN(now time.Time, n int) bool

AllowN reports whether n events may happen at time now. Use this method if you intend to drop / skip events that exceed the rate limit. Otherwise use Reserve or Wait. AllowN 表示截止到 now 这个时间点,是否存在 n 个 token,如果存在那么就返回 true 反之返回 false,如果我们限流比较严格,没有资源就直接丢弃可以使用这个方法。

func (*Limiter) Wait

func (l *Limiter) Wait(ctx context.Context) error

Wait 是最常用的, Wait 是 WaitN(ctx, 1) 的别名

func (*Limiter) WaitN

func (l *Limiter) WaitN(ctx context.Context, n int) error

WaitN(ctx, n) 表示如果存在 n 个令牌就直接转发,不存在我们就等,等待存在为止,传入的 ctx 的 Deadline 就是等待的 Deadline

Jump to

Keyboard shortcuts

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