diff --git a/.golangci.yml b/.golangci.yml index 51f2902..7fc697c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -25,9 +25,9 @@ linters-settings: - wrapperFunc linters: - disable-all: true enable: - megacheck + - golint - govet - unconvert - megacheck @@ -42,19 +42,25 @@ linters: - typecheck - ineffassign - varcheck + - stylecheck + - gochecknoinits + - scopelint + - gocritic + - nakedret + - gosimple + - prealloc fast: false - + disable-all: true run: -# modules-download-mode: vendor + output: + format: tab skip-dirs: - vendor issues: exclude-rules: - - text: "weak cryptographic primitive" + - text: "should have a package comment, unless it's in another file for this package" linters: - - gosec - -service: - golangci-lint-version: 1.24.x \ No newline at end of file + - golint + exclude-use-default: false diff --git a/README.md b/README.md index 36051f1..421cd8e 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,8 @@ type Interface interface { Returned channels used as "ticks," i.e., for each repeat or initial operation one read from this channel needed. Closing the channel indicates "done with retries." It is pretty much the same idea as `time.Timer` or `time.Tick` implements. Note - the first (technically not-repeated-yet) call won't happen **until something sent to the channel**. For this reason, the typical strategy sends the first "tick" before the first wait/sleep. -Three strategies provided byt the pacakage: +Three strategies provided byt the package: -1. **Fixed delay**, up to max number of attempts. It is the default strategy used by `repeater.NewDefault` constructor -2. **BackOff** with jitter provides exponential backoff. It starts from `Duration` interval and goes in steps with `last * math.Pow(factor, attempt)`. Optional jitter randomizes intervals a little bit. _Factor = 1 effectively makes this strategy fixed with `Duration` delay._ -3. **Once** strategy does not do any repeats and mainly used for tests/mocks` +1. **Fixed delay**, up to max number of attempts. It is the default strategy used by `repeater.NewDefault` constructor. +2. **BackOff** with jitter provides an exponential backoff. It starts from `Duration` interval and goes in steps with `last * math.Pow(factor, attempt)`. Optional jitter randomizes intervals a little. _Factor = 1 effectively makes this strategy fixed with `Duration` delay._ +3. **Once** strategy does not do any repeats and mainly used for tests/mocks`. diff --git a/go.mod b/go.mod index 294da48..81191a1 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/go-pkgz/repeater go 1.14 -require github.com/stretchr/testify v1.3.0 +require github.com/stretchr/testify v1.5.1 diff --git a/go.sum b/go.sum index 4347755..331fa69 100644 --- a/go.sum +++ b/go.sum @@ -3,5 +3,9 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/repeater_test.go b/repeater_test.go index 980fb4f..934511e 100644 --- a/repeater_test.go +++ b/repeater_test.go @@ -128,6 +128,7 @@ func TestRepeaterBackoff(t *testing.T) { assert.Nil(t, err, "should be ok") assert.Equal(t, 6, called, "called 6 times") + //nolint:gocritic t.Log(time.Since(st)) // 100 + 100 * 2^1 + 100 * 2^2 + 100 * 2^3 + 100 * 2^4 = 3100 assert.True(t, time.Since(st) >= 3100*time.Millisecond && time.Since(st) < 4100*time.Millisecond, fmt.Sprintf("took %s", time.Since(st))) @@ -242,7 +243,7 @@ func TestRepeaterMemoryLeakFixed(t *testing.T) { } before := runtime.NumGoroutine() - num := []int{} + var num []int for i := 0; i < 25; i++ { rep() num = append(num, runtime.NumGoroutine()) @@ -271,7 +272,7 @@ func TestRepeaterMemoryLeakBackOff(t *testing.T) { } before := runtime.NumGoroutine() - num := []int{} + var num []int for i := 0; i < 25; i++ { rep() num = append(num, runtime.NumGoroutine()) diff --git a/strategy/strategy.go b/strategy/strategy.go index 75af12b..da49734 100644 --- a/strategy/strategy.go +++ b/strategy/strategy.go @@ -16,7 +16,7 @@ type Interface interface { type Once struct{} // Start returns closed channel with a single element to prevent any repeats -func (s *Once) Start(ctx context.Context) <-chan struct{} { +func (s *Once) Start(_ context.Context) <-chan struct{} { ch := make(chan struct{}) go func() { ch <- struct{}{}