use util/retry instead of wait fork directly

This commit is contained in:
2022-12-16 14:21:11 +02:00
parent e73c30689c
commit 0f33c8a544
9 changed files with 144 additions and 57 deletions

View File

@@ -1,6 +1,8 @@
package retry
import (
"context"
"k8s.io/apimachinery/pkg/util/wait"
)
@@ -11,7 +13,18 @@ func OnError(backoff wait.Backoff, fn func() (retryable bool, err error)) error
return wait.ExponentialBackoff(backoff, func() (done bool, _ error) {
retryable, err := fn()
if err == nil || !retryable {
return true, err
return true, nil
}
return false, nil
})
}
// error is only returned by context
func OnErrorManagedBackoff(ctx context.Context, backoff wait.Backoff, fn func() (retryable bool, err error)) error {
return wait.ManagedExponentialBackoffWithContext(ctx, backoff, func() (done bool, _ error) {
retryable, err := fn()
if err == nil || !retryable {
return true, nil
}
return false, nil
})

21
vendor/github.com/jtagcat/util/std/error.go generated vendored Normal file
View File

@@ -0,0 +1,21 @@
package std
import "errors"
// for errors.Is(err, ERr)
type GenericErr struct {
Err error
Wrapped error
}
func (a GenericErr) Is(target error) bool {
return errors.Is(a.Err, target)
}
func (a GenericErr) Unwrap() error {
return a.Wrapped
}
func (a GenericErr) Error() string {
return a.Err.Error() + ": " + a.Wrapped.Error()
}

32
vendor/github.com/jtagcat/util/std/exec.go generated vendored Normal file
View File

@@ -0,0 +1,32 @@
package std
import (
"context"
"fmt"
"os/exec"
)
func RunCmdWithCtx(ctx context.Context, cmd *exec.Cmd) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("starting command: %w", err)
}
wait := make(chan error)
go func() {
wait <- cmd.Wait()
close(wait)
}()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-wait:
return err
}
}

60
vendor/github.com/jtagcat/util/std/url.go generated vendored Normal file
View File

@@ -0,0 +1,60 @@
package std
import (
"net/url"
"path"
"strings"
)
// k8s.io/helm/pkg/urlutil
// mod: supports // and /
// in case of multiple absolute paths, last is used
func URLJoin(baseURL string, paths ...string) (string, error) {
// mod:
// base is replaced by first with //
newBase := -1
for i, p := range paths {
if strings.HasPrefix(p, "//") {
newBase = i
}
}
u, err := url.Parse(baseURL)
if err != nil {
return "", err
}
if newBase > -1 {
old := u
u, err = url.Parse(paths[newBase])
if err != nil {
return "", err
}
u.Scheme = old.Scheme
if u.User == nil {
u.User = old.User
}
paths = paths[newBase+1:]
}
// mod:
// allow rooting to domain with /
absPath := -1
for i, p := range paths {
if strings.HasPrefix(p, "/") {
absPath = i
}
}
// We want path instead of filepath because path always uses /.
if absPath > -1 {
u.Path = path.Join(paths[absPath:]...)
} else {
all := []string{u.Path}
all = append(all, paths...)
u.Path = path.Join(all...)
}
return u.String(), nil
}

View File

@@ -276,7 +276,7 @@ func (b *Backoff) Step() time.Duration {
duration := b.Duration
// calculate the next step
if b.Factor != 0 && b.Steps != 0 {
if b.Factor != 0 {
b.Duration = time.Duration(float64(b.Duration) * b.Factor)
if b.Cap > 0 && b.Duration > b.Cap {
b.Duration = b.Cap
@@ -431,17 +431,6 @@ func ExponentialBackoff(backoff Backoff, condition ConditionFunc) error {
return ErrWaitTimeout
}
// ManagedExponentialBackoff, unlike ExponentialBackoff does not return ErrWaitTimeout.
// Instead the loop continues indefinitely with Sleep being maxDuration (by Steps or Cap) + Jitter
func ManagedExponentialBackoff(backoff Backoff, condition ConditionFunc) error {
for {
if ok, err := runConditionWithCrashProtection(condition); err != nil || ok {
return err
}
time.Sleep(backoff.Step())
}
}
// Poll tries a condition func until it returns true, an error, or the timeout
// is reached.
//
@@ -766,26 +755,3 @@ func ExponentialBackoffWithContext(ctx context.Context, backoff Backoff, conditi
return ErrWaitTimeout
}
// ManagedExponentialBackoffWithContext works with a request context and a Backoff. It ensures that the retry wait never
// exceeds the deadline specified by the request context.
func ManagedExponentialBackoffWithContext(ctx context.Context, backoff Backoff, condition ConditionFunc) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if ok, err := runConditionWithCrashProtection(condition); err != nil || ok {
return err
}
waitBeforeRetry := backoff.Step()
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(waitBeforeRetry):
}
}
}

5
vendor/modules.txt vendored
View File

@@ -20,7 +20,7 @@ github.com/golang/protobuf/ptypes/timestamp
# github.com/golang/snappy v0.0.4
## explicit
github.com/golang/snappy
# github.com/jtagcat/util v0.0.0-20221112215320-924d264211be
# github.com/jtagcat/util v0.0.0-20221216121757-90da8d3bc885
## explicit; go 1.18
github.com/jtagcat/util/batch
github.com/jtagcat/util/retry
@@ -170,7 +170,7 @@ google.golang.org/protobuf/runtime/protoiface
google.golang.org/protobuf/runtime/protoimpl
google.golang.org/protobuf/types/descriptorpb
google.golang.org/protobuf/types/known/timestamppb
# k8s.io/apimachinery v0.25.4 => github.com/jtagcat/kubernetes/staging/src/k8s.io/apimachinery v0.0.0-20221027124836-581f57977fff
# k8s.io/apimachinery v0.25.4
## explicit; go 1.19
k8s.io/apimachinery/pkg/util/runtime
k8s.io/apimachinery/pkg/util/wait
@@ -185,4 +185,3 @@ k8s.io/klog/v2/internal/severity
# k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2
## explicit; go 1.18
k8s.io/utils/clock
# k8s.io/apimachinery => github.com/jtagcat/kubernetes/staging/src/k8s.io/apimachinery v0.0.0-20221027124836-581f57977fff