https://github.com/coder/retry
A tiny retry package for Go.
https://github.com/coder/retry
Last synced: 15 days ago
JSON representation
A tiny retry package for Go.
- Host: GitHub
- URL: https://github.com/coder/retry
- Owner: coder
- License: cc0-1.0
- Created: 2017-11-27T18:01:43.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2023-11-11T23:27:02.000Z (over 1 year ago)
- Last Synced: 2025-04-02T22:35:29.994Z (23 days ago)
- Language: Go
- Homepage:
- Size: 86.9 KB
- Stars: 41
- Watchers: 11
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# retry
An exponentially backing off retry package for Go.
[](https://godoc.org/github.com/coder/retry)
```
go get github.com/coder/retry@latest
````retry` promotes control flow using `for`/`goto` instead of callbacks.
## Examples
Wait for connectivity to google.com, checking at most once every
second:```go
func pingGoogle(ctx context.Context) error {
var err errorr := retry.New(time.Second, time.Second*10);
// Jitter is useful when the majority of clients to a service use
// the same backoff policy.
//
// It is provided as a standard deviation.
r.Jitter = 0.1retry:
_, err = http.Get("https://google.com")
if err != nil {
if r.Wait(ctx) {
goto retry
}
return err
}return nil
}
```Wait for connectivity to google.com, checking at most 10 times:
```go
func pingGoogle(ctx context.Context) error {
var err error
for r := retry.New(time.Second, time.Second*10); n := 0; r.Wait(ctx) && n < 10; n++ {
_, err = http.Get("https://google.com")
if err != nil {
continue
}
break
}
return err
}
```