ratelimiter is a library to limit request rate
go get -u github.com/atuowgo/ratelimiter
Next, include in your application:
import "github.com/atuowgo/ratelimiter"
r := ratelimiter.Rule{
Name: "rule1",
IntervalInMs: 1000,
CellNum: 2,
LimitQps: 200,
}
ratelimiter.Load(r)
//before call the resource,call the Entry()
succ,ctx := Entry()
if succ {
resource()
}
//finally call the Exit()
Exit(ctx)
func resource() {
time.Sleep(time.Millisecond * time.Duration(rand.Intn(50)))
resCnt++
}