forked from mirror/backoff
more docs
This commit is contained in:
parent
793ea4ae06
commit
ff2ca325ba
33
README.md
33
README.md
|
@ -10,10 +10,14 @@ A simple backoff algorithm in Go (Golang)
|
||||||
$ go get -v github.com/jpillora/backoff
|
$ 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.
|
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
|
``` go
|
||||||
|
|
||||||
b := &backoff.Backoff{
|
b := &backoff.Backoff{
|
||||||
|
@ -41,6 +45,33 @@ Reset!
|
||||||
100ms
|
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
|
#### Credits
|
||||||
|
|
||||||
Ported from some JavaScript written by [@tj](https://github.com/tj)
|
Ported from some JavaScript written by [@tj](https://github.com/tj)
|
||||||
|
|
Loading…
Reference in New Issue