Documentation ¶
Overview ¶
Package ratelimit 提供了 X-Rate-Limit 功能的中间件
store := NewMemory(...) srv := NewServer(store) h = srv.RateLimit(h, logs.ERROR())
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Bucket ¶
type Bucket struct { Capacity int64 `json:"cap"` // 上限 Rate time.Duration `json:"rate"` // 每隔 rate 添加一个令牌 Tokens int64 `json:"tokens"` // 可用令牌数量 Last time.Time `json:"last"` // 最后次添加令牌的时间 }
Bucket 令牌桶
真正的令牌桶算法应该是按时自动增加令牌数量, 此处稍作修改:去掉自动分发令牌功能,集中在每次拿令牌时, 一次性补全之前缺的令牌数量。
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server 提供操作 Bucket 的一系列服务
type Store ¶
type Store interface { // 设置或是添加指定名称的令牌桶 Set(name string, val *Bucket) error // 删除指定的令牌桶 Delete(name string) error // 获取指定的令牌桶,为空则返回 nil Get(name string) *Bucket }
Store 存储 Bucket 的接口
func NewMemcache ¶
NewMemcache 声明一个以 memcache 为存储介质的 Store
prefix 变量名的统一前缀,防止与其它变量名冲突 client memcached 客户端实例。 expiration 过期时间,单位:秒。 errlog 错误时,写的日志记录。
Click to show internal directories.
Click to hide internal directories.