Chronos
is very simple to use tool, you just declare the maximum operations(max) and in every Y time
that those operations are allowed to be executed(per). Chronos
has the accuracy of nanoseconds.
You can use chronos#Acquire
anywhere you want, but be careful because it waits/blocks for
availability to be executed.
import (
"time"
"github.com/kataras/chronos"
)
func main() {
// Allow maximum of 5 MyActions calls
// per 3 seconds.
c := chronos.New(5, 3 * time.Second)
// The below actions will execute immediately.
MyAction(c)
MyAction(c)
MyAction(c)
MyAction(c)
MyAction(c)
// The below action will execute after 3 seconds from the last call
// or when 3 seconds passed without any c.Acquire call.
MyAction(c)
}
func MyAction(c *chronos.C) {
<- c.Acquire()
// Do something here...
}
The <- c.Acquire()
is blocking when needed, no any further actions neeeded by the end-developer.