漏桶算法
go.uber.org/ratelimit
令牌桶算法
github.com/juju/ratelimit
func main() {
// 漏桶限流
// b := ratelimit.NewLeakyBucket(5)
// 令牌桶限流
b := ratelimit.NewTokenBucket(5, 10)
ad := ratelimit.NewAdapter(b)
if ad.Allow() {
fmt.Println("allow", time.Now())
} else {
fmt.Println("too many requests")
}
}