-
Notifications
You must be signed in to change notification settings - Fork 362
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
Improve Retry Logic to Only Retry on Server-Side HTTP Errors #1390
base: main
Are you sure you want to change the base?
Changes from 4 commits
d8d00bc
55f448a
c91540b
03db161
a30e28e
e297913
8c369d6
e81d59d
f8676b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ require ( | |
github.com/package-url/packageurl-go v0.1.3 | ||
github.com/pandatix/go-cvss v0.6.2 | ||
github.com/spdx/tools-golang v0.5.5 | ||
github.com/stretchr/testify v1.9.0 | ||
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 still necessary here? It looks like this is no longer being used. |
||
github.com/tidwall/gjson v1.18.0 | ||
github.com/tidwall/pretty v1.2.1 | ||
github.com/tidwall/sjson v1.2.5 | ||
|
@@ -57,6 +58,7 @@ require ( | |
github.com/containerd/stargz-snapshotter/estargz v0.15.1 // indirect | ||
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect | ||
github.com/cyphar/filepath-securejoin v0.2.5 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/dlclark/regexp2 v1.11.0 // indirect | ||
github.com/docker/distribution v2.8.3+incompatible // indirect | ||
github.com/docker/docker-credential-helpers v0.8.1 // indirect | ||
|
@@ -84,6 +86,7 @@ require ( | |
github.com/opencontainers/go-digest v1.0.0 // indirect | ||
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect | ||
github.com/pjbgf/sha1cd v0.3.0 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/rivo/uniseg v0.4.7 // indirect | ||
github.com/rogpeppe/go-internal v1.12.0 // indirect | ||
github.com/russross/blackfriday/v2 v2.1.0 // indirect | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import ( | |
"encoding/json" | ||
"fmt" | ||
"io" | ||
"math/rand" | ||
|
||
"net/http" | ||
"time" | ||
|
||
|
@@ -16,7 +16,7 @@ import ( | |
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
const ( | ||
var ( | ||
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 change intentional? 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. I changed these from const to var so previously I could adjust the endpoint, I will revert back to const |
||
// QueryEndpoint is the URL for posting queries to OSV. | ||
QueryEndpoint = "https://api.osv.dev/v1/querybatch" | ||
// GetEndpoint is the URL for getting vulnerabilities from OSV. | ||
|
@@ -158,7 +158,7 @@ func chunkBy[T any](items []T, chunkSize int) [][]T { | |
|
||
// checkResponseError checks if the response has an error. | ||
func checkResponseError(resp *http.Response) error { | ||
if resp.StatusCode == http.StatusOK { | ||
if resp.StatusCode >= 200 && resp.StatusCode < 300 { | ||
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. suggestion: I think we shouldn't change this line, since it's not needed for addressing #861 even though 2xx are success codes, we're only expecting the API currently to return a 200 and since this change isn't strictly needed for the rest of the change to work, I would lean towards not changing it for now 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. I've been cargo-culting the HTTP retry logic from the examples in https://github.com/sethvargo/go-retry/blob/main/retry_test.go, for what it's worth, which treats 5xx errors as retryable, only. That said, I also recently saw https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429 in the wild, which could be retried with an exponential backoff approach. |
||
return nil | ||
} | ||
|
||
|
@@ -306,26 +306,35 @@ func HydrateWithClient(resp *BatchedResponse, client *http.Client) (*HydratedBat | |
return &hydrated, nil | ||
} | ||
|
||
// makeRetryRequest will return an error on both network errors, and if the response is not 200 | ||
// makeRetryRequest will return an error on both network errors, and if the response is not 2xx | ||
func makeRetryRequest(action func() (*http.Response, error)) (*http.Response, error) { | ||
var resp *http.Response | ||
var err error | ||
|
||
for i := range maxRetryAttempts { | ||
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. issue: I think you've changed this loop more than you need to
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. Yeah, you were right. Sorry for removing Jitter in the function. Added back! |
||
// rand is initialized with a random number (since go1.20), and is also safe to use concurrently | ||
// we do not need to use a cryptographically secure random jitter, this is just to spread out the retry requests | ||
// #nosec G404 | ||
jitterAmount := (rand.Float64() * float64(jitterMultiplier) * float64(i)) | ||
time.Sleep(time.Duration(i*i)*time.Second + time.Duration(jitterAmount*1000)*time.Millisecond) | ||
|
||
for i := 0; i < maxRetryAttempts; i++ { | ||
resp, err = action() | ||
if err == nil { | ||
// Check the response for HTTP errors | ||
err = checkResponseError(resp) | ||
if err == nil { | ||
break | ||
} | ||
if err != nil { | ||
|
||
sleepDuration := time.Duration(i*jitterMultiplier) * time.Second | ||
time.Sleep(sleepDuration) | ||
continue | ||
} | ||
|
||
if resp.StatusCode >= 500 { | ||
|
||
resp.Body.Close() | ||
sleepDuration := time.Duration(i*jitterMultiplier) * time.Second | ||
time.Sleep(sleepDuration) | ||
continue | ||
} | ||
|
||
// Success or client error, do not retry | ||
break | ||
} | ||
|
||
if resp != nil && resp.StatusCode >= 500 { | ||
resp.Body.Close() | ||
return nil, fmt.Errorf("received %d status code after %d attempts", resp.StatusCode, maxRetryAttempts) | ||
} | ||
|
||
return resp, err | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package osv | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestRetryOn5xx(t *testing.T) { | ||
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. issue: we should be testing for a couple of different status codes and orders I'd recommend refactoring this into a table-based test, and we should have cases for at least:
|
||
attempt := 0 | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
attempt++ | ||
w.WriteHeader(http.StatusInternalServerError) // 500 | ||
})) | ||
defer server.Close() | ||
|
||
// Override the QueryEndpoint for testing | ||
originalQueryEndpoint := QueryEndpoint | ||
QueryEndpoint = server.URL | ||
defer func() { QueryEndpoint = originalQueryEndpoint }() | ||
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. issue: there's no reason to do this, because (that'll then mean you can make this test a parallel one) |
||
|
||
client := &http.Client{ | ||
Timeout: 2 * time.Second, | ||
} | ||
|
||
resp, err := makeRetryRequest(func() (*http.Response, error) { | ||
req, _ := http.NewRequest(http.MethodPost, QueryEndpoint, nil) | ||
req.Header.Set("Content-Type", "application/json") | ||
return client.Do(req) | ||
}) | ||
|
||
log.Printf("TestRetryOn5xx: resp = %v, err = %v", resp, err) | ||
|
||
// Assertion: resp should be nil | ||
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. nit: these comments feel pretty redundant |
||
if resp != nil { | ||
t.Errorf("Expected response to be nil after retries on 5xx errors, but got: %v", resp) | ||
} | ||
|
||
// Assertion: err should not be nil | ||
if err == nil { | ||
t.Errorf("Expected an error after retries on 5xx errors, but got none") | ||
} | ||
|
||
// Assertion: number of attempts should equal maxRetryAttempts | ||
if attempt != maxRetryAttempts { | ||
t.Errorf("Expected number of attempts to equal maxRetryAttempts (%d), but got: %d", maxRetryAttempts, attempt) | ||
} | ||
} |
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.
issue: this needs to be reverted
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 ran the
scripts/run_lints.sh
script, and the only issues flagged were spacing warnings outside of the code I wrote. Let me know if you'd like me to address those too.