Chronos provides an easy way to limit X operations per Y time in accuracy of nanoseconds. Chronos is a crucial tool when calling external, remote or even internal APIs with rate limits.
The only requirement is the Go Programming Language.
$ go get -u github.com/kataras/chronos
chronos
has zero third-party dependencies rathen than the Go's standard library.
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.
The ext package provides two subpackages; function
and http
. Navigate there to learn how they can help you.
The awesome chronos
community is always adding new content to learn from, _examples is a great place to get started!
Read the godocs for a better understanding.
Join the welcoming community of fellow chronos
developers in rocket.chat
- Post a feature request or report a bug
- ⭐ and watch the public repository, will keep you up to date
- 🌎 publish an article or share a tweet about your personal experience with
chronos
.
Current: 0.0.1
Read more about Semantic Versioning 2.0.0
- https://semver.org/
- https://en.wikipedia.org/wiki/Software_versioning
- https://wiki.debian.org/UpstreamGuide#Releases_and_Versions
The original author of chronos
is @kataras, you can reach him via;
This software is licensed under the open source 3-Clause BSD License.
You can find the license file here, for any questions regarding the license please contact with me.