Skip to content

Commit

Permalink
Use more go-like duration types for retries (#16)
Browse files Browse the repository at this point in the history
Makes interacting w/ the lib as a caller a bit more natural, esp with `30 * time.Milliseconds` inputs
  • Loading branch information
michaeljguarino authored Apr 4, 2023
1 parent f5b8532 commit 4dbf023
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions retry/algorithms.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ type BackoffAlgorithm interface {

type Exponential struct {
mult float64
max float64
start float64
max time.Duration
start time.Duration
}

func (exp *Exponential) Backoff(iter int) time.Duration {
dur := algorithms.Max(exp.start*exp.mult, exp.max)
exp.start = dur
return time.Duration(exp.start)
dur := algorithms.Max(float64(exp.start)*exp.mult, float64(exp.max))
exp.start = time.Duration(dur)
return exp.start
}

func (exp *Exponential) Continue() bool {
Expand All @@ -33,13 +33,13 @@ func (exp *Exponential) Continue() bool {

type Constant struct {
max int
dur int
dur time.Duration
count int
}

func (con *Constant) Backoff(iter int) time.Duration {
con.count++
return time.Duration(con.dur)
return con.dur
}

func (con *Constant) Continue() bool {
Expand Down
6 changes: 3 additions & 3 deletions retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package retry

import "time"

func NewExponential(start, max int64, mult float64) *Exponential {
return &Exponential{start: float64(start), max: float64(max), mult: mult}
func NewExponential(start, max time.Duration, mult float64) *Exponential {
return &Exponential{start: start, max: max, mult: mult}
}

func NewConstant(duration, count int) *Constant {
func NewConstant(duration time.Duration, count int) *Constant {
return &Constant{dur: duration, max: count}
}

Expand Down

0 comments on commit 4dbf023

Please sign in to comment.