Skip to content

Commit 7196fd0

Browse files
author
Dean Karn
authored
update ci (#4)
1 parent 86294f3 commit 7196fd0

File tree

8 files changed

+112
-31
lines changed

8 files changed

+112
-31
lines changed

.github/workflows/go.yml

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
11
name: Lint & Test
2-
on: [push, pull_request]
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
types: [opened, edited, reopened, synchronize]
38
jobs:
49
test:
510
strategy:
611
matrix:
7-
go-version: [1.17.x]
12+
go-version: [1.20.x,1.19.x,1.18.x,1.17.x]
813
platform: [ubuntu-latest, macos-latest, windows-latest]
914
runs-on: ${{ matrix.platform }}
1015
steps:
1116
- name: Install Go
12-
uses: actions/setup-go@v1
17+
uses: actions/setup-go@v3
1318
with:
1419
go-version: ${{ matrix.go-version }}
1520

16-
- name: Priming Cache
17-
uses: actions/cache@v1
21+
- name: Checkout code
22+
uses: actions/checkout@v3
23+
24+
- name: Restore Cache
25+
uses: actions/cache@v3
1826
with:
1927
path: ~/go/pkg/mod
20-
key: ${{ runner.os }}-v2-go-${{ hashFiles('**/go.sum') }}
28+
key: ${{ runner.os }}-v3-go-${{ hashFiles('**/go.sum') }}
2129
restore-keys: |
22-
${{ runner.os }}-v2-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.17.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 ./...
30+
${{ runner.os }}-v3-go-
3331
3432
- name: Test
35-
run: go test ./...
33+
run: go test -race -cover ./...
34+
35+
golangci:
36+
name: lint
37+
runs-on: ubuntu-latest
38+
steps:
39+
- uses: actions/checkout@v3
40+
- name: golangci-lint
41+
uses: golangci/golangci-lint-action@v3
42+
with:
43+
version: latest

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [Unreleased]
8+
9+
## [1.2.0] - 2023-05-07
10+
### Added
11+
- Added MaxAttempts(..) to configuration and ErrorMaxAttemptsReached to be returned by the Sleep(...) function when configured.
12+
13+
[Unreleased]: https://github.com/go-playground/backoff-sys/compare/v1.2.0...HEAD
14+
[1.2.0]: https://github.com/go-playground/backoff-sys/commit/v1.2.0

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
GOCMD=GO111MODULE=on go
2+
3+
test:
4+
$(GOCMD) test -cover -race ./...
5+
6+
bench:
7+
$(GOCMD) test -run=NONE -bench=. -benchmem ./...
8+
9+
lint:
10+
golangci-lint run
11+
12+
.PHONY: lint test bench

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Package backoff-sys
22

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.1.1-green.svg)
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.2.0-green.svg)
44
[![Actions Status](https://github.com/go-playground/backoff-sys/workflows/Lint%20%26%20Test/badge.svg)](https://github.com/go-playground/backoff-sys/actions)
55
[![GoDoc](https://godoc.org/github.com/go-playground/backoff-sys?status.svg)](https://pkg.go.dev/github.com/go-playground/backoff-sys)
66
![License](https://img.shields.io/dub/l/vibe-d.svg)

exponential.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@ package backoff
22

33
import (
44
"context"
5+
"errors"
56
"math"
67
"math/rand"
78
"time"
89
)
910

11+
var (
12+
ErrMaxAttemptsReached = errors.New("max attempts reached")
13+
)
14+
1015
// ExponentialBuilder helps to build the final exponential backoff entity
1116
type ExponentialBuilder struct {
12-
factor float64
13-
interval time.Duration
14-
jitter time.Duration
15-
max time.Duration
17+
factor float64
18+
interval time.Duration
19+
jitter time.Duration
20+
max time.Duration
21+
maxAttempts int
1622
}
1723

1824
// NewExponential create a new exponential backoff builder with sane defaults.
@@ -49,22 +55,31 @@ func (e ExponentialBuilder) Max(max time.Duration) ExponentialBuilder {
4955
return e
5056
}
5157

58+
// MaxAttempts sets the maximum number of attempts before the Sleep(...) function begins returning
59+
// ErrMaxAttemptsReached, by default is unlimited.
60+
func (e ExponentialBuilder) MaxAttempts(max int) ExponentialBuilder {
61+
e.maxAttempts = max
62+
return e
63+
}
64+
5265
// Init returns a read-only(thread safe) Exponential backoff entity for use.
5366
func (e ExponentialBuilder) Init() Exponential {
5467
return Exponential{
55-
factor: e.factor,
56-
interval: float64(e.interval),
57-
jitter: int64(e.jitter),
58-
max: int64(e.max),
68+
factor: e.factor,
69+
interval: float64(e.interval),
70+
jitter: int64(e.jitter),
71+
max: int64(e.max),
72+
maxAttempts: e.maxAttempts,
5973
}
6074
}
6175

6276
// Exponential is the final read-only(thread safe) backoff entity
6377
type Exponential struct {
64-
factor float64
65-
interval float64
66-
jitter int64
67-
max int64
78+
factor float64
79+
interval float64
80+
jitter int64
81+
max int64
82+
maxAttempts int
6883
}
6984

7085
// Duration accepts attempt and returns the backoff duration o sleep for.
@@ -81,7 +96,12 @@ func (e Exponential) Duration(attempt int) time.Duration {
8196
}
8297

8398
// Sleep is a convenience function wrapping Duration and allowing the sleep time to be cancelled via the Context.
99+
//
100+
// This function can also return ErrMaxAttemptsReached if the max attempts have been reached.
84101
func (e Exponential) Sleep(ctx context.Context, attempt int) error {
102+
if e.maxAttempts > 0 && attempt >= e.maxAttempts {
103+
return ErrMaxAttemptsReached
104+
}
85105
t := time.NewTimer(e.Duration(attempt))
86106
defer t.Stop()
87107
select {

exponential_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package backoff
2+
3+
import (
4+
. "github.com/go-playground/assert/v2"
5+
6+
"context"
7+
"testing"
8+
"time"
9+
)
10+
11+
func TestExponentialBackoff(t *testing.T) {
12+
ctx, cancel := context.WithCancel(context.Background())
13+
bo := NewExponential().Max(time.Millisecond * 50).Interval(time.Millisecond * 25).Jitter(time.Millisecond * 10).MaxAttempts(5).Init()
14+
Equal(t, bo.Sleep(ctx, 0), nil)
15+
Equal(t, bo.Sleep(ctx, 1), nil)
16+
Equal(t, bo.Sleep(ctx, 2), nil)
17+
Equal(t, bo.Sleep(ctx, 3), nil)
18+
Equal(t, bo.Sleep(ctx, 4), nil)
19+
Equal(t, bo.Sleep(ctx, 5), ErrMaxAttemptsReached)
20+
21+
cancel()
22+
Equal(t, bo.Sleep(ctx, 0), context.Canceled)
23+
}

go.mod

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

33
go 1.14
4+
5+
require github.com/go-playground/assert/v2 v2.2.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
2+
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=

0 commit comments

Comments
 (0)