https://github.com/rvflash/backoff
Fibonacci backoff implementation
https://github.com/rvflash/backoff
backoff-strategy fibonacci-backoff golang
Last synced: 2 months ago
JSON representation
Fibonacci backoff implementation
- Host: GitHub
- URL: https://github.com/rvflash/backoff
- Owner: rvflash
- License: mit
- Created: 2019-09-08T12:10:56.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-09-14T11:25:38.000Z (over 5 years ago)
- Last Synced: 2025-01-28T16:16:28.071Z (4 months ago)
- Topics: backoff-strategy, fibonacci-backoff, golang
- Language: Go
- Size: 22.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fibonacci backoff implementation
[](https://godoc.org/github.com/rvflash/backoff)
[](https://travis-ci.org/rvflash/backoff)
[](http://codecov.io/github/rvflash/backoff?branch=master)
[](https://goreportcard.com/report/github.com/rvflash/backoff)The package `backoff` implements a Fibonacci backoff algorithm.
### Installation
To install it, you need to install Go and set your Go workspace first.
Then, download and install it:```bash
$ go get -u github.com/rvflash/backoff
```
Import it in your code:
```go
import "github.com/rvflash/backoff"
```### Prerequisite
`backoff` uses the Go modules that required Go 1.11 or later.
The `go.mod` file refers to Go 1.13 because the tests uses the new error method `As`.## Features
Based on the Fibonacci suite (1, 1, 2, 3, 5, 8, 13, 21, etc.), the `backoff` strategy do or retry a given task.
By default, `DefaultInterval` is used as interval, so the sleep duration is the `current Fibonacci value * 500 * time.Millisecond`.
With the `New` method, you can create your own Backoff strategy but by default, the following implementation are available:
See the documentation for more details and samples.### Do
`Do` guarantees to execute at least once the task if the context is not already cancelled.
As long as the task return in success and the context not done, BackOff will continue to call it, with a sleep duration based the Fibonacci suite and the BackOff's interval.* `DoN`: does the same job as Do but limits the number of attempt.
* `DoUntil`: does the same job as Do but limits the execution to the given deadline.### Retry
`Retry` retries the task until it does not return error or BackOff stops.
* `RetryN`: does the same job as Do but limits the number of attempt.
* `RetryUntil`: does the same job as Do but limits the execution to the given deadline.## Quick start
Assuming the following code that retry 3 times to run the task if it returns in error.
```go
import (
"context"
"log""github.com/rvflash/backoff"
)func main() {
// task implements the backoff.Func interface.
task := func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
return nil
}
n, err := backoff.RetryN(context.Background(), 3, task)
if err != nil {
log.Fatal(err)
}
log.Printf("Nice boy: %d retry, first try in success", n)
// Output: Nice boy: 0 retry, first try in success
}
```