Skip to content

Commit

Permalink
fix: Backoff retry should not swallow errors. Fixes argoproj#1166 (ar…
Browse files Browse the repository at this point in the history
…goproj#1167)

Signed-off-by: Derek Wang <[email protected]>
  • Loading branch information
whynowy authored Apr 7, 2021
1 parent ad6337d commit 1eba6fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
15 changes: 12 additions & 3 deletions common/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package common

import (
"fmt"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -94,10 +95,18 @@ func Connect(backoff *apicommon.Backoff, conn func() error) error {
if err != nil {
return errors.Wrap(err, "invalid backoff configuration")
}
return wait.ExponentialBackoff(*b, func() (bool, error) {
if err := conn(); err != nil {
if waitErr := wait.ExponentialBackoff(*b, func() (bool, error) {
if err = conn(); err != nil {
// return "false, err" will cover waitErr
return false, nil
}
return true, nil
})
}); waitErr != nil {
if err != nil {
return fmt.Errorf("%v: %v", waitErr, err)
} else {
return waitErr
}
}
return nil
}
15 changes: 15 additions & 0 deletions common/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.
package common

import (
"fmt"
"strings"
"testing"

"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
Expand All @@ -36,3 +38,16 @@ func TestRetryableKubeAPIError(t *testing.T) {
assert.False(t, IsRetryableKubeAPIError(errInvalid))
assert.False(t, IsRetryableKubeAPIError(errMethodNotSupported))
}

func TestConnect(t *testing.T) {
err := Connect(nil, func() error {
return fmt.Errorf("new error")
})
assert.NotNil(t, err)
assert.True(t, strings.Contains(err.Error(), "new error"))

err = Connect(nil, func() error {
return nil
})
assert.Nil(t, err)
}

0 comments on commit 1eba6fa

Please sign in to comment.