https://github.com/asaf-shitrit/go-wait
A tiny util for patient programs
https://github.com/asaf-shitrit/go-wait
backoff golang jitter polling tiny util wait
Last synced: 4 months ago
JSON representation
A tiny util for patient programs
- Host: GitHub
- URL: https://github.com/asaf-shitrit/go-wait
- Owner: asaf-shitrit
- License: apache-2.0
- Created: 2021-12-08T13:01:46.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-12-22T10:48:18.000Z (over 4 years ago)
- Last Synced: 2024-06-19T20:50:54.426Z (almost 2 years ago)
- Topics: backoff, golang, jitter, polling, tiny, util, wait
- Language: Go
- Homepage:
- Size: 19.5 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-wait
[](https://github.com/asaf-shitrit/go-wait/actions/workflows/run-tests.yml)
A tiny util library for programs that require a little patience ⏰
## Usage
### Wait
Useful for simple cases where a predictable `bool` check should be ran
in a set interval and continue when it is satisfied.
#### Basic usage
```go
import (
wait "github.com/asaf-shitrit/go-wait"
)
checkFunc := func() (bool, error) {
// any bool based logic that changes over a
// given period of time
}
ctx := context.Background() // or pass any ctx you would like
if err := wait.Until(ctx, checkFunc); err != nil {
// handle logical/timeout err
}
// logic that should happen after check is satisfied
```
#### With explicit options
```go
import (
wait "github.com/asaf-shitrit/go-wait"
)
checkFunc := func() (bool, error) {
// any bool based logic that changes over a
// given period of time
}
options := &wait.UntilOptions{
Timeout: time.Minute
Interval: time.Second
}
ctx := context.Background() // or pass any ctx you would like
if err := wait.Until(ctx, checkFunc, options); err != nil {
// handle logical/timeout err
}
// logic that should happen after check is satisfied
```
### Backoff
Really useful in cases that low CPU overhead a constraint and the check intervals
should be backed off after each run.
It was inspired by Go's own `http.Server` `Shutdown` implementation ❤️
#### Basic usage
```go
import (
wait "github.com/asaf-shitrit/go-wait"
)
checkFunc := func() (bool, error) {
// any bool based logic that changes over a
// given period of time
}
ctx := context.Background() // or pass any ctx you would like
if err := wait.Backoff(ctx, checkFunc); err != nil {
// handle logical/timeout err
}
// logic that should happen after check is satisfied
```
#### With explicit options
```go
import (
wait "github.com/asaf-shitrit/go-wait"
)
checkFunc := func() (bool, error) {
// any bool based logic that changes over a
// given period of time
}
options := &wait.BackoffOptions{
BaselineDuration: time.Millisecond,
Limit: 500 * time.Millisecond,
Multiplier: 2,
}
ctx := context.Background() // or pass any ctx you would like
if err := wait.Backoff(ctx, checkFunc, options); err != nil {
// handle logical/timeout err
}
// logic that should happen after check is satisfied
```
### Capabilities
### Timeout & Cancel ⏰
It is aligned with Golang concept of context so explicit cancels & timeout will work
out of the box.
#### Jitter
Allows you to set an amount of jitter percentage that will apply
for the calculation of each interval.