Skip to content

Commit

Permalink
Adding re-try for getting a Vuln for the given ID (google#141)
Browse files Browse the repository at this point in the history
* Adding re-try for getting a Vuln for the given ID

* Indentation correction

* Moving the request retries to another function

* Update internal/osv/osv.go

Co-authored-by: Rex P <106129829+another-rex@users.noreply.github.com>

* Update internal/osv/osv.go

Co-authored-by: Rex P <106129829+another-rex@users.noreply.github.com>

* Update internal/osv/osv.go

Co-authored-by: Rex P <106129829+another-rex@users.noreply.github.com>

* Update internal/osv/osv.go

Co-authored-by: Rex P <106129829+another-rex@users.noreply.github.com>
  • Loading branch information
2 people authored and julieqiu committed May 2, 2023
1 parent 4e3b66a commit dd2bfb1
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions internal/osv/osv.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"time"

"github.com/google/osv-scanner/pkg/lockfile"
"github.com/google/osv-scanner/pkg/models"
Expand Down Expand Up @@ -119,6 +120,19 @@ func checkResponseError(resp *http.Response) error {
return fmt.Errorf("server response error: %s", string(respBuf))
}

func makeRetryRequest(action func() (*http.Response, error)) (*http.Response, error) {
var resp *http.Response
var err error
retries := 3
for i := 0; i < retries; i++ {
resp, err = action()
if err == nil {
break
}
time.Sleep(time.Second)
}
return resp, err
}
func MakeRequest(request BatchedQuery) (*BatchedResponse, error) {
// API has a limit of 1000 bulk query per request
queryChunks := chunkBy(request.Queries, MaxQueriesPerRequest)
Expand All @@ -131,7 +145,9 @@ func MakeRequest(request BatchedQuery) (*BatchedResponse, error) {
}
requestBuf := bytes.NewBuffer(requestBytes)

resp, err := http.Post(QueryEndpoint, "application/json", requestBuf)
resp, err := makeRetryRequest(func() (*http.Response, error) {
return http.Post(QueryEndpoint, "application/json", requestBuf)
})
if err != nil {
return nil, err
}
Expand All @@ -156,7 +172,9 @@ func MakeRequest(request BatchedQuery) (*BatchedResponse, error) {

// Get a Vulnerabiltiy for the given ID.
func Get(id string) (*models.Vulnerability, error) {
resp, err := http.Get(GetEndpoint + "/" + id)
resp, err := makeRetryRequest(func() (*http.Response, error) {
return http.Get(QueryEndpoint + "/" + id)
})
if err != nil {
return nil, err
}
Expand Down

0 comments on commit dd2bfb1

Please sign in to comment.