Skip to content

Commit

Permalink
fix: Retry with exponential backoff result in time to wait of zero(ar…
Browse files Browse the repository at this point in the history
…goproj#7173) (argoproj#7339)

* fix: Retry with exponential backoff result in time to wait of zero(argoproj#7173)

Signed-off-by: iam-veeramalla <[email protected]>

* fix issue reported by golintci

Signed-off-by: iam-veeramalla <[email protected]>
  • Loading branch information
iam-veeramalla authored Oct 1, 2021
1 parent a6039e1 commit d790e96
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
7 changes: 4 additions & 3 deletions pkg/apis/application/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,11 +766,12 @@ func (r *RetryStrategy) NextRetryAt(lastAttempt time.Time, retryCounts int64) (t
}
// Formula: timeToWait = duration * factor^retry_number
// Note that timeToWait should equal to duration for the first retry attempt.
timeToWait := duration * time.Duration(math.Pow(float64(factor), float64(retryCounts)))
// When timeToWait is more than maxDuration retry should be performed at maxDuration.
timeToWait := float64(duration) * (math.Pow(float64(factor), float64(retryCounts)))
if maxDuration > 0 {
timeToWait = time.Duration(math.Min(float64(maxDuration), float64(timeToWait)))
timeToWait = math.Min(float64(maxDuration), timeToWait)
}
return lastAttempt.Add(timeToWait), nil
return lastAttempt.Add(time.Duration(timeToWait)), nil
}

// Backoff is the backoff strategy to use on subsequent retries for failing syncs
Expand Down
15 changes: 9 additions & 6 deletions pkg/apis/application/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2109,19 +2109,22 @@ func TestProjectNormalize(t *testing.T) {
func TestRetryStrategy_NextRetryAtDefaultBackoff(t *testing.T) {
retry := RetryStrategy{}
now := time.Now()
expectedTimes := []time.Time{
now.Add(5 * time.Second),
now.Add(10 * time.Second),
now.Add(20 * time.Second),
now.Add(40 * time.Second),
now.Add(80 * time.Second),
expectedTimes := map[int]time.Time{
0: now.Add(5 * time.Second),
1: now.Add(10 * time.Second),
2: now.Add(20 * time.Second),
3: now.Add(40 * time.Second),
4: now.Add(80 * time.Second),
80: now.Add(DefaultSyncRetryMaxDuration),
100: now.Add(DefaultSyncRetryMaxDuration),
}

for i, expected := range expectedTimes {
retryAt, err := retry.NextRetryAt(now, int64(i))
assert.NoError(t, err)
assert.Equal(t, expected.Format(time.RFC850), retryAt.Format(time.RFC850))
}

}

func TestRetryStrategy_NextRetryAtCustomBackoff(t *testing.T) {
Expand Down

0 comments on commit d790e96

Please sign in to comment.