-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
batches: retry changeset spec upload by default
Fixes sourcegraph/sourcegraph#30431.
- Loading branch information
Showing
6 changed files
with
183 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package retrier | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/sourcegraph/sourcegraph/lib/errors" | ||
) | ||
|
||
// Retry retries the given function up to max times if it returns an error. | ||
// base and multiplier may be specified to use an exponential backoff between | ||
// retries; if no backoff is required, set both to 0. | ||
// | ||
// The first time the function returns nil, nil will immediately be returned | ||
// from Retry. | ||
func Retry(f func() error, max int, base time.Duration, multiplier float64) error { | ||
var errs errors.MultiError | ||
wait := base | ||
|
||
for { | ||
err := f() | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
errs = errors.Append(errs, err) | ||
if len(errs.Errors()) > max { | ||
return errs | ||
} | ||
time.Sleep(wait) | ||
wait = time.Duration(multiplier * float64(wait)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package retrier | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/sourcegraph/sourcegraph/lib/errors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRetry(t *testing.T) { | ||
knownErr := errors.New("error!") | ||
|
||
t.Run("no backoff", func(t *testing.T) { | ||
for i := 0; i < 100; i += 10 { | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
t.Run("immediate success", func(t *testing.T) { | ||
retries := -1 | ||
err := Retry(func() error { | ||
retries += 1 | ||
return nil | ||
}, i, 0, 0) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, 0, retries) | ||
}) | ||
|
||
t.Run("delayed success", func(t *testing.T) { | ||
retries := -1 | ||
want := i / 2 | ||
err := Retry(func() error { | ||
retries += 1 | ||
if retries >= want { | ||
return nil | ||
} | ||
return knownErr | ||
}, i, 0, 0) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, want, retries) | ||
}) | ||
|
||
t.Run("no success", func(t *testing.T) { | ||
retries := -1 | ||
err := Retry(func() error { | ||
retries += 1 | ||
return knownErr | ||
}, i, 0, 0) | ||
|
||
assert.NotNil(t, err) | ||
assert.ErrorIs(t, err, knownErr) | ||
assert.Equal(t, i, retries) | ||
}) | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("backoff", func(t *testing.T) { | ||
base := 1 * time.Millisecond | ||
want := []time.Duration{ | ||
0, // We don't test anything on the first try. | ||
base, | ||
2 * base, | ||
4 * base, | ||
8 * base, | ||
16 * base, | ||
} | ||
|
||
retries := -1 | ||
var last time.Time | ||
err := Retry(func() error { | ||
retries += 1 | ||
if retries > 0 { | ||
now := time.Now() | ||
assert.GreaterOrEqual(t, now.Sub(last), want[retries]) | ||
last = now | ||
} else { | ||
last = time.Now() | ||
} | ||
|
||
return knownErr | ||
}, 5, base, 2) | ||
|
||
assert.NotNil(t, err) | ||
assert.ErrorIs(t, err, knownErr) | ||
assert.Equal(t, 5, retries) | ||
}) | ||
} |