-
Notifications
You must be signed in to change notification settings - Fork 175
Switch to avast/retry-go
for HTTP retry
#2350
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
Open
saschagrunert
wants to merge
1
commit into
sigstore:main
Choose a base branch
from
saschagrunert:retry-go
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -15,10 +15,16 @@ | |
package client | ||
|
||
import ( | ||
"crypto/tls" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hashicorp/go-retryablehttp" | ||
"github.com/avast/retry-go/v4" | ||
) | ||
|
||
// Option is a functional option for customizing static signatures. | ||
|
@@ -30,7 +36,6 @@ type options struct { | |
RetryWaitMin time.Duration | ||
RetryWaitMax time.Duration | ||
InsecureTLS bool | ||
Logger interface{} | ||
NoDisableKeepalives bool | ||
Headers map[string][]string | ||
} | ||
|
@@ -81,14 +86,9 @@ func WithRetryWaitMax(t time.Duration) Option { | |
} | ||
} | ||
|
||
// WithLogger sets the logger; it must implement either retryablehttp.Logger or retryablehttp.LeveledLogger; if not, this will not take effect. | ||
func WithLogger(logger interface{}) Option { | ||
return func(o *options) { | ||
switch logger.(type) { | ||
case retryablehttp.Logger, retryablehttp.LeveledLogger: | ||
o.Logger = logger | ||
} | ||
} | ||
// WithLogger sets the logger; this method is deprecated and will not take any effect. | ||
func WithLogger(_ interface{}) Option { | ||
return func(*options) {} | ||
} | ||
|
||
// WithInsecureTLS disables TLS verification. | ||
|
@@ -113,33 +113,73 @@ func WithHeaders(h map[string][]string) Option { | |
} | ||
|
||
type roundTripper struct { | ||
http.RoundTripper | ||
UserAgent string | ||
Headers map[string][]string | ||
inner http.RoundTripper | ||
*options | ||
} | ||
|
||
// RoundTrip implements `http.RoundTripper` | ||
func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
req.Header.Set("User-Agent", rt.UserAgent) | ||
for k, v := range rt.Headers { | ||
func (rt *roundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) { | ||
req.Header.Set("User-Agent", rt.options.UserAgent) | ||
for k, v := range rt.options.Headers { | ||
for _, h := range v { | ||
req.Header.Add(k, h) | ||
} | ||
} | ||
return rt.RoundTripper.RoundTrip(req) | ||
|
||
err = retry.Do(func() (err error) { | ||
res, err = rt.inner.RoundTrip(req) | ||
return shouldRetry(res, err) | ||
}, | ||
retry.Attempts(rt.options.RetryCount), | ||
retry.Delay(rt.options.RetryWaitMin), | ||
retry.MaxDelay(rt.options.RetryWaitMax), | ||
saschagrunert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
retry.DelayType(retry.BackOffDelay), | ||
) | ||
|
||
return res, err | ||
} | ||
|
||
var tooManyRedirectyRe = regexp.MustCompile(`stopped after \d+ redirects\z`) | ||
|
||
func shouldRetry(resp *http.Response, err error) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a change in behavior or is this copied from go-retryable's implementation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not copied over, but it should reflect the same retry behavior. |
||
if err != nil { | ||
urlErr := &url.Error{} | ||
|
||
// Filter well known URL errors | ||
if errors.As(err, &urlErr) { | ||
certVerificationErr := &tls.CertificateVerificationError{} | ||
|
||
if tooManyRedirectyRe.MatchString(urlErr.Error()) || | ||
strings.Contains(urlErr.Error(), "unsupported protocol scheme") || | ||
strings.Contains(urlErr.Error(), "invalid header") || | ||
strings.Contains(urlErr.Error(), "certificate is not trusted") || | ||
errors.As(urlErr.Err, &certVerificationErr) { | ||
return nil | ||
} | ||
} | ||
|
||
// Retry any other errror | ||
return err | ||
} | ||
|
||
if resp.StatusCode == http.StatusTooManyRequests { | ||
return fmt.Errorf("retry %d: %s", resp.StatusCode, resp.Status) | ||
} | ||
|
||
if resp.StatusCode == 0 || (resp.StatusCode >= 500 && | ||
resp.StatusCode != http.StatusNotImplemented) { | ||
return fmt.Errorf("retry unexpected HTTP status %d: %s", resp.StatusCode, resp.Status) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func createRoundTripper(inner http.RoundTripper, o *options) http.RoundTripper { | ||
func wrapRoundTripper(inner http.RoundTripper, o *options) http.RoundTripper { | ||
if inner == nil { | ||
inner = http.DefaultTransport | ||
} | ||
if o.UserAgent == "" && o.Headers == nil { | ||
// There's nothing to do... | ||
return inner | ||
} | ||
return &roundTripper{ | ||
RoundTripper: inner, | ||
UserAgent: o.UserAgent, | ||
Headers: o.Headers, | ||
inner: inner, | ||
options: o, | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like these are still indirects (probably because of us supporting hashivault)... is that still an issue for CNCF?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so, but the code does not seem to be imported from kubernetes-sigs/release-sdk, we may follow-up on that, though.