@LIUHUAN
2019-03-12T07:14:08.000000Z
字数 1526
阅读 1248
algorithm
type CountLimit struct {timeStamp int64 // 窗口起始时间count int32 // 窗口内请求量limitRate int32 // 限速大小deltaTime int64 // 窗口大小}func (c *CountLimit) limitrate() bool {now := time.Now().Unix()if now < c.timeStamp+c.deltaTime { // 在时间窗口内c.count += 1return c.count < c.limitRate // 是否超过限流大小} else { // 不在窗口内,直接返回限流c.timeStamp = nowc.count = 0return true}}
type LeakyBucket struct {limitRate int32 // 限速大小nextTime int64 // 下一滴水可以流出的最早时间,如果在这个时间点内,那么不能流出,限流interval int64 // 时间间隔 = 1000 / limitRate}func (l *LeakyBucket)limitRate() bool {now := time.Now().Unix()timeGap := now - l.nextTimeif now > l.nextTime {l.nextTime = now + 1000/limitRate // 更新下次通过的时间,可以是当前通过的时间到return false // 可以通过}else {// 限速return false;}}func main(){l := LeakyBucket{}l.limitRate = 100l.interval = 1000 / l.limitRatel.nextTime = time.Now().Unix() + l.interval// if l.limitRate() {} // do something}
type TokenBucket struct {timeStamp int64 // 桶起作用时间count int32 // 桶内请求量limitRate int32 // 限速大小totalTokens int32 // 令牌桶大小}func (t *TokenBucket) limitrate() bool {now := time.Now().Unix()passData := int32(now-t.timeStamp) * t.limitRatet.count = min(t.totalTokens, t.count+passData) // 桶的容量是固定的,放入的令牌要不大于桶的容量t.timeStamp = now // 更新时间if t.count < 1 { // 从令牌桶内去令牌,没有令牌了,限速return true} else { // 还有令牌t.count -= 1 // 取令牌return false}}