A token bucket implementation based on multi goroutines which is safe to use under concurrency environments.
go get -u github.com/DavidCai1993/token-bucket
API documentation can be found here: https://godoc.org/github.com/DavidCai1993/token-bucket
import (
bucket "github.com/DavidCai1993/token-bucket"
)
tb := bucket.New(time.Second, 1000)
tb.Take(10)
ok := tb.TakeMaxDuration(1000, 20*time.Second)
fmt.Println(ok)
// -> true
tb.WaitMaxDuration(1000, 10*time.Second)
fmt.Println(ok)
// -> false
tb.Wait(1000)