Skip to content

Commit 130d15c

Browse files
author
Dean Karn
committed
Initial Commit
0 parents  commit 130d15c

File tree

7 files changed

+217
-0
lines changed

7 files changed

+217
-0
lines changed

.github/workflows/go.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Lint & Test
2+
on: [push, pull_request]
3+
jobs:
4+
test:
5+
strategy:
6+
matrix:
7+
go-version: [1.14.x]
8+
platform: [ubuntu-latest, macos-latest, windows-latest]
9+
runs-on: ${{ matrix.platform }}
10+
steps:
11+
- name: Install Go
12+
uses: actions/setup-go@v1
13+
with:
14+
go-version: ${{ matrix.go-version }}
15+
16+
- name: Priming Cache
17+
uses: actions/cache@v1
18+
with:
19+
path: ~/go/pkg/mod
20+
key: ${{ runner.os }}-v1-go-${{ hashFiles('**/go.sum') }}
21+
restore-keys: |
22+
${{ runner.os }}-v1-go-
23+
24+
- name: Checkout code
25+
uses: actions/checkout@v2
26+
27+
- name: Lint
28+
if: matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.14.x'
29+
run: |
30+
export PATH=$PATH:$(go env GOPATH)/bin # temporary fix. See https://github.com/actions/setup-go/issues/14
31+
go get -u golang.org/x/lint/golint
32+
golint -set_exit_status ./...
33+
34+
- name: Test
35+
run: go test ./...

.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Go template
3+
# Binaries for programs and plugins
4+
*.exe
5+
*.exe~
6+
*.dll
7+
*.so
8+
*.dylib
9+
10+
# Test binary, built with `go test -c`
11+
*.test
12+
13+
# Output of the go coverage tool, specifically when used with LiteIDE
14+
*.out
15+
16+
# Dependency directories (remove the comment below to include it)
17+
# vendor/
18+

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 Go Playground
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## Package backoff-sys
2+
3+
<img align="right" src="https://raw.githubusercontent.com/go-playground/backoff-sys/master/logo.jpg">![Project status](https://img.shields.io/badge/version-1.0.0-green.svg)
4+
[![Actions Status](https://github.com/go-playground/backoff-sys/workflows/Lint%20%26%20Test/badge.svg)](https://github.com/go-playground/backoff-sys/actions)
5+
[![GoDoc](https://godoc.org/github.com/go-playground/backoff-sys?status.svg)](https://pkg.go.dev/github.com/go-playground/backoff-sys)
6+
![License](https://img.shields.io/dub/l/vibe-d.svg)
7+
8+
Package backoff-sys provides the bare building blocks for backing off and can be used to build more complex backoff packages, but this is likely enough.
9+
This includes:
10+
- [x] Exponential backoff, with jitter
11+
- [ ] Linear backoff, with jitter
12+
13+
Example
14+
-------
15+
```go
16+
// go run _examples/exponential/main.go
17+
package main
18+
19+
import (
20+
"errors"
21+
"fmt"
22+
"time"
23+
24+
"github.com/go-playground/backoff-sys"
25+
)
26+
27+
func main() {
28+
bo := backoff.NewExponential().Init()
29+
for i := 0; i < 5; i++ {
30+
err := fallible()
31+
if err != nil {
32+
d := bo.Duration(i)
33+
fmt.Printf("Waiting: %s\n", d)
34+
time.Sleep(d)
35+
continue
36+
}
37+
}
38+
}
39+
40+
func fallible() error {
41+
return errors.New("failed")
42+
}
43+
```
44+
45+
License
46+
------
47+
Distributed under MIT License, please see license file in code for more details.

_examples/exponential/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"time"
7+
8+
"github.com/go-playground/backoff-sys"
9+
)
10+
11+
func main() {
12+
bo := backoff.NewExponential().Init()
13+
for i := 0; i < 5; i++ {
14+
err := fallible()
15+
if err != nil {
16+
d := bo.Duration(i)
17+
fmt.Printf("Waiting: %s\n", d)
18+
time.Sleep(d)
19+
continue
20+
}
21+
}
22+
}
23+
24+
func fallible() error {
25+
return errors.New("failed")
26+
}

exponential.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package backoff
2+
3+
import (
4+
"math"
5+
"math/rand"
6+
"time"
7+
)
8+
9+
// ExponentialBuilder helps to build the final exponential backoff entity
10+
type ExponentialBuilder struct {
11+
factor float64
12+
interval time.Duration
13+
jitter time.Duration
14+
}
15+
16+
// NewExponential create a new exponential backoff builder with sane defaults.
17+
func NewExponential() ExponentialBuilder {
18+
return ExponentialBuilder{
19+
factor: 1.75,
20+
interval: time.Second,
21+
jitter: time.Millisecond * 250,
22+
}
23+
}
24+
25+
// Factor sets a factor for the backoff algorithm.
26+
func (e ExponentialBuilder) Factor(factor float64) ExponentialBuilder {
27+
e.factor = factor
28+
return e
29+
}
30+
31+
// Interval sets base wait interval for the backoff algorithm.
32+
func (e ExponentialBuilder) Interval(interval time.Duration) ExponentialBuilder {
33+
e.interval = interval
34+
return e
35+
}
36+
37+
// Jitter sets the maximum jitter for the backoff algorithm.
38+
func (e ExponentialBuilder) Jitter(jitter time.Duration) ExponentialBuilder {
39+
e.jitter = jitter
40+
return e
41+
}
42+
43+
// Init returns a read-only(thread safe) Exponential backoff entity for use.
44+
func (e ExponentialBuilder) Init() Exponential {
45+
return Exponential{
46+
factor: e.factor,
47+
interval: float64(e.interval),
48+
jitter: int64(e.jitter),
49+
}
50+
}
51+
52+
// Exponential is the final read-only(thread safe) backoff entity
53+
type Exponential struct {
54+
factor float64
55+
interval float64
56+
jitter int64
57+
}
58+
59+
// Duration accepts attempt and returns the backoff duration o sleep for.
60+
func (e Exponential) Duration(attempt int) time.Duration {
61+
d := int64(math.Pow(e.factor, float64(attempt)) * e.interval)
62+
if e.jitter <= 0 {
63+
return time.Duration(d)
64+
}
65+
jitter := rand.Int63n(e.jitter)
66+
return time.Duration(d + jitter)
67+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/go-playground/backoff-sys
2+
3+
go 1.14

0 commit comments

Comments
 (0)