diff --git a/README.md b/README.md index 6c6ab7d..167e06c 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,14 @@ A simple backoff algorithm in Go (Golang) $ go get -v github.com/jpillora/backoff ``` -# Usage +### Usage 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. +--- + +**Simple example** + ``` go b := &backoff.Backoff{ @@ -41,6 +45,33 @@ Reset! 100ms ``` +--- + +**Example using `net` package** + +``` go +b := &backoff.Backoff{ + Max: 5 * time.Minute, +} + +for { + conn, err := net.Dial("tcp", "example.com:5309") + if err != nil { + d := b.Duration() + fmt.Printf("%s, reconnecting in %s", err, d) + time.Sleep(d) + continue + } + //connected + b.Reset() + conn.Write([]byte("hello world!")) + // ... Read ... Write ... etc + conn.Close() + //disconnected +} + +``` + #### Credits Ported from some JavaScript written by [@tj](https://github.com/tj)