From ff2ca325bac1bc80539c50d830e9aae981656968 Mon Sep 17 00:00:00 2001 From: Jaime Pillora Date: Thu, 5 Mar 2015 22:06:27 +1100 Subject: [PATCH] more docs --- README.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) 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)