高并发下的流量控制

lryong ·
漏桶算法中,有一段代码有问题 ``` func (rl *RateLimiter) Grant() bool { // calculating water output now := time.Now() out := int64(now.Sub(rl.startAt).Milliseconds()) * rl.rate // remain water after the leak rl.water = max(0, rl.water-out) rl.startAt = now if rl.water+1 < rl.size { rl.water++ return true } return false } ``` ``` rl.startAt=now应该放在 if rl.water+1 < rl.size { rl.startAt=now rl.water++ return true } ``` 否则如果请求一直按照按照固定的速率发过来,后面就会一直被拒绝
#1