Commit cae641e9 authored by protolambda's avatar protolambda Committed by protolambda

op-service/backoff: Refactor to use time.Duration instead of float/int. (#6823)

changed backoff/strategies.go and backoff/strategies_test.go, only types and a function changed

**Description**

No features added. Just refactored backoff package to allow only types from time.Duration on time related variables.

**Tests**

Only modified a single test to use time.Duration instead of int.

**Metadata**

- Fixes #[[Link to Issue](https://github.com/ethereum-optimism/optimism/issues/6730#issuecomment-1678950667)]
Co-authored-by: default avatarRcontre360 <rcontreraspimentel@gmail.com>
parent 5d1a445e
......@@ -131,9 +131,9 @@ func (s *SyncClient) eventLoop() {
s.log.Info("Starting sync client event loop")
backoffStrategy := &backoff.ExponentialStrategy{
Min: 1000,
Max: 20_000,
MaxJitter: 250,
Min: 1000 * time.Millisecond,
Max: 20_000 * time.Millisecond,
MaxJitter: 250 * time.Millisecond,
}
for {
......
......@@ -16,35 +16,34 @@ type Strategy interface {
// ExponentialStrategy performs exponential backoff. The exponential backoff
// function is min(e.Min + (2^attempt * 1000) + randBetween(0, e.MaxJitter), e.Max)
type ExponentialStrategy struct {
// Min is the minimum amount of time to wait between attempts in ms.
Min float64
// Min is the minimum amount of time to wait between attempts.
Min time.Duration
// Max is the maximum amount of time to wait between attempts in ms.
Max float64
// Max is the maximum amount of time to wait between attempts.
Max time.Duration
// MaxJitter is the maximum amount of random jitter to insert between
// attempts in ms.
MaxJitter int
// MaxJitter is the maximum amount of random jitter to insert between attempts.
MaxJitter time.Duration
}
func (e *ExponentialStrategy) Duration(attempt int) time.Duration {
var jitter int
var jitter time.Duration
if e.MaxJitter > 0 {
jitter = rand.Intn(e.MaxJitter)
jitter = time.Duration(rand.Int63n(e.MaxJitter.Nanoseconds()))
}
dur := e.Min + (math.Pow(2, float64(attempt)) * 1000)
dur += float64(jitter)
dur := e.Min + time.Duration(int(math.Pow(2, float64(attempt))*1000))*time.Millisecond
dur += jitter
if dur > e.Max {
return time.Millisecond * time.Duration(e.Max)
return e.Max
}
return time.Millisecond * time.Duration(dur)
return dur
}
func Exponential() Strategy {
return &ExponentialStrategy{
Max: 10000,
MaxJitter: 250,
Max: time.Duration(10000 * time.Millisecond),
MaxJitter: time.Duration(250 * time.Millisecond),
}
}
......
......@@ -9,13 +9,13 @@ import (
func TestExponential(t *testing.T) {
strategy := &ExponentialStrategy{
Min: 3000,
Max: 10000,
Min: 3000 * time.Millisecond,
Max: 10000 * time.Millisecond,
MaxJitter: 0,
}
durations := []int{4, 5, 7, 10, 10}
durations := []time.Duration{4, 5, 7, 10, 10}
for i, dur := range durations {
require.Equal(t, time.Millisecond*time.Duration(dur*1000), strategy.Duration(i))
require.Equal(t, dur*time.Second, strategy.Duration(i))
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment