From a6647af774df148995dca761852b0828895bb7e0 Mon Sep 17 00:00:00 2001 From: Jaime Pillora Date: Tue, 3 Mar 2015 09:01:21 +1100 Subject: [PATCH] added more docs --- README.md | 6 +++--- backoff.go | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ca52f05..8bbfc58 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ 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 -Starts at `Min`, multiplied by `Factor` every call to -`Duration()` where it is capped at `Max`. Commonly used -in conjunction with `time.Sleep(duration)`. +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. ``` go diff --git a/backoff.go b/backoff.go index 6e42d6d..3ae595f 100644 --- a/backoff.go +++ b/backoff.go @@ -5,13 +5,22 @@ import ( "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 { + //Factor is the multiplying factor for each increment step 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 { - //abit hacky though, if zero-value, apply defaults + //Zero-values are nonsensical, so we use + //them to apply defaults if b.Min == 0 { b.Min = 100 * time.Millisecond } @@ -33,6 +42,7 @@ func (b *Backoff) Duration() time.Duration { return time.Duration(math.Min(ms, float64(b.Max))) } +//Resets the current value of the counter back to Min func (b *Backoff) Reset() { b.attempts = 0 }