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/update retry logic in OSV #860

Merged
merged 5 commits into from
Mar 20, 2024
Merged

Conversation

another-rex
Copy link
Collaborator

@another-rex another-rex commented Mar 15, 2024

Fixes #856

Fixes a couple of things with our OSV API retry logic:

  • Now also retry non 200 responses, in addition to retrying on network errors. (is there a need to only retry for 500 >= responses? Current logic retrying all non 200)
  • Fix batchquery code so that retries are not sending empty data.
  • Increase maxRetryAttempts from 3 to 4
  • Implement exponential backoff (actually polynomial/quadratic backoff)
  • Correctly close the body after reading error message.
  • Implement retries for determineversion API call

@another-rex another-rex marked this pull request as ready for review March 15, 2024 03:35
@codecov-commenter
Copy link

codecov-commenter commented Mar 15, 2024

Codecov Report

Attention: Patch coverage is 46.87500% with 17 lines in your changes are missing coverage. Please review.

Project coverage is 62.19%. Comparing base (de95630) to head (df89acb).

Files Patch % Lines
pkg/osv/osv.go 46.87% 17 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #860      +/-   ##
==========================================
+ Coverage   62.16%   62.19%   +0.03%     
==========================================
  Files         144      144              
  Lines       11808    11817       +9     
==========================================
+ Hits         7340     7350      +10     
- Misses       3997     3998       +1     
+ Partials      471      469       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@G-Rath
Copy link
Collaborator

G-Rath commented Mar 15, 2024

(is there a need to only retry for 500 >= responses? Current logic retrying all non 200)

You should not be re-trying 4xx errors as they explicitly mean "client error" and I'm pretty sure they all mean "do not retry without adjustment" but that might not technically be true (though this says "most")

I'd say if there's ever a case the API expects to return a 4xx that should be fine to retry, that's a mistake

(dashes off to double check his 4xx error codes to make sure he's actually correct)

@another-rex
Copy link
Collaborator Author

yeah that's true, but I don't think it's possible to call the osv.go functions with values that could cause a 400 though (other than maybe 429 because you sent too many queries), so I'm not sure if it's worth putting in extra logic there.

@G-Rath
Copy link
Collaborator

G-Rath commented Mar 15, 2024

"stick an issue in the backlog" 🤷

Copy link
Contributor

@cuixq cuixq left a comment

Choose a reason for hiding this comment

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

Sorry for the late review!

Copy link
Collaborator

@oliverchang oliverchang left a comment

Choose a reason for hiding this comment

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

LGTM with some minor comments.

pkg/osv/osv.go Outdated
func makeRetryRequest(action func() (*http.Response, error)) (*http.Response, error) {
var resp *http.Response
var err error
for i := 0; i < maxRetryAttempts; i++ {
time.Sleep(time.Duration(i*i) * time.Second)
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we add some random jitter to this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: might also make a little more logical sense to have the sleep after the actual action. particularly if we add jitter.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added some random jitter. I think it makes more sense at the start than the end, as this avoids having to add a if statement to check if this is the last loop, and avoids having to add i+1 as well.

pkg/osv/osv.go Outdated Show resolved Hide resolved
@another-rex another-rex merged commit bb1739a into google:main Mar 20, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

upstream request timeout
5 participants