https://github.com/da440dil/go-trier
Re-execution for functions within configurable limits
https://github.com/da440dil/go-trier
go golang retry retryer retrying
Last synced: 2 months ago
JSON representation
Re-execution for functions within configurable limits
- Host: GitHub
- URL: https://github.com/da440dil/go-trier
- Owner: da440dil
- License: mit
- Created: 2019-08-08T09:41:06.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-01-06T13:28:27.000Z (over 4 years ago)
- Last Synced: 2025-05-18T14:48:18.490Z (about 1 year ago)
- Topics: go, golang, retry, retryer, retrying
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-trier
[](https://travis-ci.com/da440dil/go-trier)
[](https://coveralls.io/github/da440dil/go-trier?branch=master)
[](https://pkg.go.dev/github.com/da440dil/go-trier)
[](https://goreportcard.com/report/github.com/da440dil/go-trier)
Re-execution for functions within configurable limits.
[Example](./examples/jitter/main.go) usage:
```go
import (
"context"
"fmt"
"sync"
"time"
"github.com/da440dil/go-trier"
)
func main() {
// Create trier.
tr := trier.NewTrier(
// Use linear algorithm to create delay between retries.
trier.Linear(time.Millisecond*10),
// Set maximum number of retries.
trier.WithMaxRetries(5),
// Set maximum duration randomly added to or extracted from delay
// between retries to improve performance under high contention
trier.WithJitter(time.Millisecond*5),
)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*200)
defer cancel()
for i := 0; i < 3; i++ {
ok, err := tr.Try(ctx, func(ctx context.Context) (bool, error) {
return i == 0, nil
})
fmt.Printf("{ i: %v, ok: %v, err: %v }\n", i, ok, err)
}
// Output:
// { i: 0, ok: true, err: }
// { i: 1, ok: false, err: }
// { i: 2, ok: false, err: context deadline exceeded }
}
```