https://github.com/moznion/jicker
A jittered-ticker library for go
https://github.com/moznion/jicker
Last synced: 10 months ago
JSON representation
A jittered-ticker library for go
- Host: GitHub
- URL: https://github.com/moznion/jicker
- Owner: moznion
- License: mit
- Created: 2021-04-29T10:04:58.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-25T02:42:34.000Z (over 1 year ago)
- Last Synced: 2025-04-07T07:12:30.845Z (about 1 year ago)
- Language: Go
- Size: 10.7 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jicker [](https://github.com/moznion/jicker/actions/workflows/check.yml) [](https://codecov.io/gh/moznion/jicker)
A jittered-ticker library for go.
[](https://pkg.go.dev/github.com/moznion/jicker)
## Usage
```go
package main
import (
"context"
"log"
"time"
"github.com/moznion/jicker"
)
func main() {
// if this `ctx` has done, ticking stops and it closes the ticker channel.
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
// it ticks by jittered duration (i.e. 1±5% sec); it evaluates the duration with the jitter factor every time.
c := jicker.NewJicker().Tick(ctx, 1*time.Second, 0.05)
for t := range c {
log.Printf("tick: %v", t)
}
}
```
```go
package main
import (
"context"
"log"
"time"
"github.com/moznion/jicker"
)
func main() {
// if this `ctx` has done, ticking stops and it closes the ticker channel.
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
// it ticks by jittered interval duration; random duration is between [minimumDuration, maximumDuration].
// it evaluates the duration with the jitter factor every time.
//
// in this case, the interval duration would be the random value between 1 sec and 2 secs.
c, err := jicker.NewJicker().TickBetween(ctx, 1*time.Second, 2*time.Second)
if err != nil {
log.Fatal(err)
}
for t := range c {
log.Printf("tick: %v", t)
}
}
```
## See also
- https://golang.org/pkg/time/#Tick
## Author
moznion ()