Skip to content

linkdata/rate

Repository files navigation

build coverage goreport Docs

An efficient rate limiter for Go

Because too much of the CPU consumed was golang.org/x/time/rate.Limiter.Wait() calling time.Now().

Differences from golang.org/x/time/rate and time.Ticker

This package uses ticks-per-second rather than time.Duration, and is suitable for high-tickrate applications. It allows you to change the rate by simply atomically changing an int32. It has no practical limitation on the upper rate. If you need a slower rate than once per second, you're better off using time.Ticker. Ticker.Rate() is advisory telemetry sampled over time, so it can temporarily lag and briefly exceed MaxRate().

Sample usage

One of the more common non-trivial usages of rate limiting is restricting some underlying operation(s) being utilized by more complex worker goroutines. This package supports those with Ticker.Worker(). Ticker.Worker() gates worker creation; the worker itself should later consume ticker capacity.

Assume we have a channel taskCh where we read tasks to process, and then want spawn worker goroutines that handle them. We want to spawn enough of them to stay as close to the max rate as possible without starting too many of them.

for task := range taskCh {
  if !ticker.Worker(func() { workerFn(ticker, task) }) {
    // if ticker.Worker() fails to start the worker, it means the Ticker is closed.
    break
  }
}

About

Efficient rate limiter for Go

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages