Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Configurable retry backoff settings when retrying API calls #4979

Merged
merged 3 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions util/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env

import (
"os"
"strconv"
"time"

log "github.com/sirupsen/logrus"
Expand All @@ -19,3 +20,29 @@ func LookupEnvDurationOr(key string, o time.Duration) time.Duration {
}
return o
}

func LookupEnvIntOr(key string, o int) int {
v, found := os.LookupEnv(key)
if found {
d, err := strconv.Atoi(v)
if err != nil {
log.WithField(key, v).WithError(err).Panic("failed to convert to int")
} else {
return d
}
}
return o
}

func LookupEnvFloatOr(key string, o float64) float64 {
v, found := os.LookupEnv(key)
if found {
d, err := strconv.ParseFloat(v, 64)
if err != nil {
log.WithField(key, v).WithError(err).Panic("failed to convert to float")
} else {
return d
}
}
return o
}
18 changes: 18 additions & 0 deletions util/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ func TestLookupEnvDurationOr(t *testing.T) {
_ = os.Setenv("FOO", "1h")
assert.Equal(t, time.Hour, LookupEnvDurationOr("FOO", time.Second), "env var value")
}

func TestLookupEnvIntOr(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

defer func() { _ = os.Unsetenv("FOO") }()
assert.Equal(t, 1, LookupEnvIntOr("", 1), "default value")
_ = os.Setenv("FOO", "not-int")
assert.Panics(t, func() { LookupEnvIntOr("FOO", 1) }, "bad value")
_ = os.Setenv("FOO", "2")
assert.Equal(t, 2, LookupEnvIntOr("FOO", 1), "env var value")
}

func TestLookupEnvFloatOr(t *testing.T) {
defer func() { _ = os.Unsetenv("FOO") }()
assert.Equal(t, 1., LookupEnvFloatOr("", 1.), "default value")
_ = os.Setenv("FOO", "not-float")
assert.Panics(t, func() { LookupEnvFloatOr("FOO", 1.) }, "bad value")
_ = os.Setenv("FOO", "2.0")
assert.Equal(t, 2., LookupEnvFloatOr("FOO", 1.), "env var value")
}
2 changes: 1 addition & 1 deletion util/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ func TestIsTransientErr(t *testing.T) {

_ = os.Unsetenv(transientEnvVarKey)
})
}
}
8 changes: 5 additions & 3 deletions util/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"time"

"k8s.io/apimachinery/pkg/util/wait"

envutil "github.com/argoproj/argo/v2/util/env"
)

// DefaultRetry is a default retry backoff settings when retrying API calls
Expand All @@ -14,7 +16,7 @@ import (
// 4 0.15
// 5 0.31
var DefaultRetry = wait.Backoff{
Steps: 5,
Duration: 10 * time.Millisecond,
Factor: 2,
Steps: envutil.LookupEnvIntOr("RETRY_BACKOFF_STEPS", 5),
Duration: envutil.LookupEnvDurationOr("RETRY_BACKOFF_DURATION", 10*time.Millisecond),
Factor: envutil.LookupEnvFloatOr("RETRY_BACKOFF_FACTOR", 2.),
}