forked from mirror/backoff
added more docs
This commit is contained in:
parent
34204cc40b
commit
a6647af774
|
@ -2,11 +2,11 @@
|
||||||
|
|
||||||
A simple backoff algorithm in Go (Golang)
|
A simple backoff algorithm in Go (Golang)
|
||||||
|
|
||||||
|
[![GoDoc](https://godoc.org/github.com/jpillora/backoff?status.svg)](https://godoc.org/github.com/jpillora/backoff)
|
||||||
|
|
||||||
# Usage
|
# Usage
|
||||||
|
|
||||||
Starts at `Min`, multiplied by `Factor` every call to
|
Backoff is a `time.Duration` counter. It starts at `Min`. After every call to `Duration()` it is multiplied by `Factor`. It is capped at `Max`. It returns to `Min` on every call to `Reset()`. Used in conjunction with the `time` package.
|
||||||
`Duration()` where it is capped at `Max`. Commonly used
|
|
||||||
in conjunction with `time.Sleep(duration)`.
|
|
||||||
|
|
||||||
``` go
|
``` go
|
||||||
|
|
||||||
|
|
14
backoff.go
14
backoff.go
|
@ -5,13 +5,22 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//Backoff is a time.Duration counter. It starts at Min.
|
||||||
|
//After every call to Duration() it is multiplied by Factor.
|
||||||
|
//It is capped at Max. It returns to Min on every call to Reset().
|
||||||
|
//Used in conjunction with the time package.
|
||||||
type Backoff struct {
|
type Backoff struct {
|
||||||
|
//Factor is the multiplying factor for each increment step
|
||||||
attempts, Factor int
|
attempts, Factor int
|
||||||
curr, Min, Max time.Duration
|
//Min and Max are the minimum and maximum values of the counter
|
||||||
|
curr, Min, Max time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Returns the current value of the counter and then
|
||||||
|
//multiplies it Factor
|
||||||
func (b *Backoff) Duration() time.Duration {
|
func (b *Backoff) Duration() time.Duration {
|
||||||
//abit hacky though, if zero-value, apply defaults
|
//Zero-values are nonsensical, so we use
|
||||||
|
//them to apply defaults
|
||||||
if b.Min == 0 {
|
if b.Min == 0 {
|
||||||
b.Min = 100 * time.Millisecond
|
b.Min = 100 * time.Millisecond
|
||||||
}
|
}
|
||||||
|
@ -33,6 +42,7 @@ func (b *Backoff) Duration() time.Duration {
|
||||||
return time.Duration(math.Min(ms, float64(b.Max)))
|
return time.Duration(math.Min(ms, float64(b.Max)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Resets the current value of the counter back to Min
|
||||||
func (b *Backoff) Reset() {
|
func (b *Backoff) Reset() {
|
||||||
b.attempts = 0
|
b.attempts = 0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue