Skip to content
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

fix: backoff function error handling #98

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/snyk/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,15 @@ func GetPackageVulnerabilities(cfg *Config, purl *packageurl.PackageURL, auth *s
func getRetryClient(logger *zerolog.Logger) *http.Client {
rc := retryablehttp.NewClient()
rc.Logger = nil
rc.ErrorHandler = retryablehttp.PassthroughErrorHandler
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed to handle resp == nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, instead what this does is propagate the last error message to the user. i.e instead of just seeing giving up after x attempts they'll get more details

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

rc.Backoff = func(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
if sleep, ok := parseRateLimitHeader(resp.Header.Get("X-RateLimit-Reset")); ok {
logger.Warn().
Dur("Retry-After", sleep).
Msg("Getting rate-limited, waiting...")
return sleep
if resp != nil {
if sleep, ok := parseRateLimitHeader(resp.Header.Get("X-RateLimit-Reset")); ok {
logger.Warn().
Dur("Retry-After", sleep).
Msg("Getting rate-limited, waiting...")
return sleep
}
}
return retryablehttp.DefaultBackoff(min, max, attemptNum, resp)
}
Expand Down
31 changes: 31 additions & 0 deletions lib/snyk/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,34 @@ func TestGetPackageVulnerabilities_RetryRateLimited(t *testing.T) {
assert.Equal(t, 2, numRequests, "retries failed requests")
assert.NotNil(t, issues, "should retrieve issues")
}

func TestGetPackageVulnerabilities_HandlesNilResponses(t *testing.T) {
logger := zerolog.Nop()
var numRequests int
var srv *httptest.Server
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
numRequests++
if numRequests < 5 {
w.Header().Set("X-RateLimit-Reset", "0")
w.WriteHeader(http.StatusTooManyRequests)
return
}
// Induce a client error which results in a nil response
srv.CloseClientConnections()
}))

cfg := DefaultConfig()
cfg.SnykAPIURL = srv.URL

auth, err := AuthFromToken("asdf")
require.NoError(t, err)

purl, err := packageurl.FromString("pkg:golang/github.com/snyk/parlay")
require.NoError(t, err)

orgID := uuid.New()
issues, err := GetPackageVulnerabilities(cfg, &purl, auth, &orgID, &logger)

require.Error(t, err)
assert.Nil(t, issues)
}
Loading