Skip to content

Commit

Permalink
fibonacci + golang.org/x/sync v0.3.0 => v0.5.0 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
s0rg authored Dec 29, 2023
1 parent 0f51374 commit 577f86d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
32 changes: 23 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ import (
type mode byte

const (
Simple mode = 0
Linear mode = 1
// Simple mode - time increases by sleep + jitter*attempt.
Simple mode = 0
// Linear mode - time increases by sleep*attempt + jitter.
Linear mode = 1
// Exponential mode - time increases by sleep*2^attempt + jitter.
Exponential mode = 2
// Fibonacci mode - time increases by sleep*fibonacci(attempt) + jitter.
Fibonacci mode = 3
)

const (
minParallel = 0
minCount = 1
minSleep = time.Second
two = 2
minSleep = time.Second / 2
minDuration = time.Duration(0)
)

Expand Down Expand Up @@ -148,19 +154,27 @@ func (c *Config) isFatal(err error) (yes bool) {
return false
}

func ipow2(v int) (rv int64) {
const two = 2

return int64(math.Pow(two, float64(v)))
}

func (c *Config) stepDuration(n int) (d time.Duration) {
switch c.mode {
case Linear:
return c.sleep*time.Duration(n) + c.jitter
case Exponential:
return c.sleep*time.Duration(ipow2(n)) + c.jitter
case Fibonacci:
return c.sleep*time.Duration(fibonacci(n)) + c.jitter
}

return c.sleep + c.jitter*time.Duration(n)
}

func ipow2(v int) (rv int64) {
return int64(math.Pow(two, float64(v)))
}

func fibonacci(n int) int64 {
if n < two {
return int64(n)
}

return fibonacci(n-1) + fibonacci(n-two)
}
34 changes: 34 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,39 @@ func TestValidate(t *testing.T) {
}
}

func TestFibonacci(t *testing.T) {
t.Parallel()

var counter int

const (
tryMax = 5
errCount = 4
)

try := retry.New(
retry.Count(tryMax),
retry.Mode(retry.Fibonacci),
)

err := try.Single("test-fibonacci", func() (err error) {
counter++

if counter < errCount {
return errFail
}

return nil
})
if err != nil {
t.FailNow()
}

if counter != errCount {
t.FailNow()
}
}

func TestFatal(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -303,6 +336,7 @@ func TestFatal(t *testing.T) {
try := retry.New(
retry.Count(maxTries),
retry.Fatal(errFatal),
retry.Mode(retry.Fibonacci),
)

steps := []retry.Step{
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/s0rg/retry

go 1.20
go 1.21.5

require golang.org/x/sync v0.3.0
require golang.org/x/sync v0.5.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=

0 comments on commit 577f86d

Please sign in to comment.