Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(github): retry CreateStatus (#1542)
- Loading branch information
Jian Zeng
committed
Dec 30, 2020
1 parent
27d10d7
commit 6699e9c
Showing
2 changed files
with
30 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,21 @@ | ||
package retry | ||
|
||
import ( | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
) | ||
|
||
// OnError executes the provided function repeatedly, retrying if the server returns an error. | ||
func OnError(backoff wait.Backoff, fn func() error) error { | ||
var lastErr error | ||
err := wait.ExponentialBackoff(backoff, func() (bool, error) { | ||
err := fn() | ||
switch { | ||
case err == nil: | ||
return true, nil | ||
default: | ||
if err := fn(); err != nil { | ||
lastErr = err | ||
return false, nil | ||
} | ||
return true, nil | ||
}) | ||
if err == wait.ErrWaitTimeout { | ||
err = lastErr | ||
} | ||
return err | ||
} | ||
|
||
// OnExceededQuota executes the provided function repeatedly, retrying if the server returns an exceeded quota error. | ||
func OnExceededQuota(backoff wait.Backoff, fn func() error) error { | ||
var lastErr error | ||
err := wait.ExponentialBackoff(backoff, func() (bool, error) { | ||
err := fn() | ||
switch { | ||
case err == nil: | ||
return true, nil | ||
case errors.IsForbidden(err): | ||
if strings.Contains(err.Error(), "exceeded quota:") { | ||
lastErr = err | ||
return false, nil | ||
} | ||
return false, err | ||
default: | ||
return false, err | ||
} | ||
}) | ||
if err == wait.ErrWaitTimeout { | ||
err = lastErr | ||
return lastErr | ||
} | ||
return err | ||
} |