Documentation ¶
Index ¶
- type Bucket
- func (b *Bucket) Add(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
- func (b *Bucket) Del(key string) error
- func (b *Bucket) Get(key string) (int64, error)
- func (b *Bucket) Init(...interface{}) error
- func (b *Bucket) Set(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
- type CacheFixWindow
- func (c *CacheFixWindow) Add(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
- func (c *CacheFixWindow) Del(key string) error
- func (c *CacheFixWindow) Get(key string) (int64, error)
- func (c *CacheFixWindow) Init(...interface{}) error
- func (c *CacheFixWindow) Set(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
- type CacheSlideWindow
- func (c *CacheSlideWindow) Add(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
- func (c *CacheSlideWindow) Del(key string) error
- func (c *CacheSlideWindow) Get(key string) (int64, error)
- func (c *CacheSlideWindow) Init(...interface{}) error
- func (c *CacheSlideWindow) Set(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
- type Token
- func (t *Token) Add(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
- func (t *Token) Del(key string) error
- func (t *Token) Get(key string) (int64, error)
- func (t *Token) Init(...interface{}) error
- func (t *Token) Set(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bucket ¶
type Bucket struct {
// contains filtered or unexported fields
}
Bucket 桶限流
func (*Bucket) Add ¶
func (b *Bucket) Add(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
Add Bucket限流中新增一条规则,注意:如果规则已经存在,只会更新相关规则信息。注意:为实现请求数目
的限制,Bucket限流中有一个专门用于计数的sync.map Input : key : 规则的唯一标识 limits : 当前规则在规定时间内限制请求的数目 tmDuriation : 当前规则对应的时间段,与limits共同构成 tmDuriation时间段内限流limits次的语义 others : bucket限流中最大缓存请求数目 Output : error : 成功则返回nil,否则为相关错误信息
func (*Bucket) Get ¶
Get 获取Bucket限流中规则剩余请求数目。宏观上所有的Get请求间会以固定的速率返回
同时如果请求数目超过了bucket的max限制,请求会被抛弃。Get的以channel实现缓存队列,以sleep实现请求间的固定间隔 Input : key : 规则的唯一标识 Output : int64 : 规则剩余请求数目,<-1000为错误,具体参见common.ErrorReturn* error : 成功则为nil,否则为对应错误
func (*Bucket) Set ¶
func (b *Bucket) Set(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
Set Bucket限流中更新或者新增一条规则,注意:如果规则不存在,会新增一条规则
Input : key : 规则的唯一标识 limits : 当前规则在规定时间内限制请求的数目 tmDuriation : 当前规则对应的时间段,与limits共同构成 tmDuriation时间段内限流limits次的语义 others : bucket限流中最大缓存请求数目 Output : error : 成功则返回nil,否则为相关错误信息
type CacheFixWindow ¶
type CacheFixWindow struct {
// contains filtered or unexported fields
}
CacheFixWindow 固定窗口限流,以map存储所有限流规则,map的key为限流规则key
func (*CacheFixWindow) Add ¶
func (c *CacheFixWindow) Add(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
Add 固定窗口限流中新增一条限流规则。同样,如果相应key存在则会更新相关信息。固定窗口限流新增规则时,
窗口索引值初始为0,StartAt为当前时间 Input : key : 限流标识,用于唯一标识一条限流规则 limit : tmSpan时间段内的限流数,与tmSpan同时实现tmSpan时间段内限流limit次的语义 tmDuriation : 时间段, 与limit同时实现tmSpan时间段内限流limit次的语义 others : 未启用 Output : error : 成功为nil,否则为具体错误信息
func (*CacheFixWindow) Del ¶
func (c *CacheFixWindow) Del(key string) error
Del 删除固定窗口限流中一条限流规则
Input : key : 限流标识,用于唯一标识一条限流规则 Output : error : 成功为nil,否则为具体错误信息
func (*CacheFixWindow) Get ¶
func (c *CacheFixWindow) Get(key string) (int64, error)
Get 获得固定窗口限流中key对应规则的剩余请求数目。根据当前时间和规则创建或者
重置的时间间隔,Get会计算当前请求所在的窗口的索引,如果该索引和上次请求的索引相同,则判断为旧窗口 和旧请求数目 同时进行计数,否则则判断为新窗口 重新开始计数 Input : key : 限流标识,用于标识要获取限流相关信息的规则 Output : int64 : 当前窗口中剩余请求的数目, <-1000产生了错误,具体参见common.ErrorReturn* error : 成功为nil,否则为具体错误信息
func (*CacheFixWindow) Init ¶
func (c *CacheFixWindow) Init(...interface{}) error
Init 创建CacheFixWindow中规则map
func (*CacheFixWindow) Set ¶
func (c *CacheFixWindow) Set(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
Set 固定窗口限流中重置或新增一条限流规则。注意,如果相应key存在则会更新相关信息,不管规则是否存在
Idx和CurCnt都会置为0 Input : key : 限流标识,用于唯一标识一条限流规则 limits : tmDuriation时间段内的限流数,与tmDuriation同时实现tmDuriation时间段内限流limits次的语义 tmDuriation : 时间段, 与limits同时实现tmSpan时间段内限流limit次的语义 others : 未启用 Output : error : 成功为nil,否则为具体错误信息
type CacheSlideWindow ¶
type CacheSlideWindow struct {
// contains filtered or unexported fields
}
CacheSlideWindow 滑动敞口限流,以map存储所有限流规则,map的key为限流规则key
func (*CacheSlideWindow) Add ¶
func (c *CacheSlideWindow) Add(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
Add 滑动窗口限流中新增一条限流规则。注意,如果相应key存在则会更新相关信息
Input : key : 限流标识,用于唯一标识一条限流规则 limit : tmDuriation时间段内的限流数,与tmDuriation同时实现tmDuriation时间段内限流limit次的语义 tmDuriation : 时间段, 与limit同时实现tmDuriation时间段内限流limit次的语义 others : 未启用 Output : error : 成功为nil,否则为具体错误信息
func (*CacheSlideWindow) Del ¶
func (c *CacheSlideWindow) Del(key string) error
Del 滑动窗口限流中删除key对应限流规则
Input : key : 限流标识,用于标识要删除的限流规则 Output : error : 成功为nil,否则为具体错误信息
func (*CacheSlideWindow) Get ¶
func (c *CacheSlideWindow) Get(key string) (int64, error)
Get 滑动窗口限流中获取key对应剩余请求数。滑动窗口的限流维护了基于
时间的请求队列,每次请求会根据当前时间和规则对应的时间段(Duration)重新计算滑动窗口的起始时间,并根据 滑动窗口时间段内请求数目判断和返回相关限流信息 Input : key : 限流标识,用于标识要获取限流相关信息的规则 Output : int64 : 当前敞口中剩余请求数目,<-1000为错误,具体参见common.ErrorReturn* error : 成功为nil,否则为具体错误信息
func (*CacheSlideWindow) Init ¶
func (c *CacheSlideWindow) Init(...interface{}) error
Init 滑动限流窗口的初始化,创建相关map
func (*CacheSlideWindow) Set ¶
func (c *CacheSlideWindow) Set(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
Set 滑动窗口限流中重置或新增一条限流规则。注意,如果相应key存在则会更新相关信息
Input : key : 限流标识,用于唯一标识一条限流规则 limits : tmDuriation时间段内的限流数,与tmDuriation同时实现tmDuriation时间段内限流limit次的语义 tmDuriation : 时间段, 与limit同时实现tmSpan时间段内限流limit次的语义 others : 未启用 Output : error : 成功为nil,否则为具体错误信息
type Token ¶
type Token struct {
// contains filtered or unexported fields
}
Token token限流
func (*Token) Add ¶
func (t *Token) Add(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
Add Token限流中新增或者更新一条规则
Input : key : 规则的唯一标识 limits : tmDuriation时间段内 访问限制数目 tmDuriation : 规则对应的时间段,与limits共同构成 tmDuriation时间段内限流limits次的语义 other : int64, 最大token数目,如果当前剩余的+新产生的token数目>other,则抛弃 Output : error : 成功则为nil,否则为对应错误
func (*Token) Get ¶
Get 获取Token限流中规则对应剩余请求数目。每次Get请求都会根据token
产生速度(CreateRateNs),距离上次请求已经逝去时间(time.Now().Sub(StartAt)),上次请求剩余的token数目(TkLeft),计算当前 剩余token数目,并返回限流相关信息 Input : key : 规则的唯一标识 Output : int64 : 规则剩余请求数目,<-1000为错误,具体参见common.ErrorReturn* error : 成功则为nil,否则为对应错误
func (*Token) Set ¶
func (t *Token) Set(key string, limits int64, tmDuriation time.Duration, others ...interface{}) error
Set Token限流中更新或新增一条规则
Input : key : 规则的唯一标识 limits : tmDuriation时间段内 访问限制数目 tmDuriation : 规则对应的时间段,与limits共同构成 tmDuriation时间段内限流limits次的语义 others : int64, 最大token数目,如果当前剩余的+新产生的token数目>other,则抛弃 Output : error : 成功则为nil,否则为对应错误