Skip to content

Commit

Permalink
fix(http): Correctly handle EOF timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
elldritch committed Feb 23, 2018
1 parent 6300fa4 commit d577588
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
14 changes: 10 additions & 4 deletions cmd/fossa/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,33 @@ package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"

logging "github.com/op/go-logging"
)

var commonLogger = logging.MustGetLogger("common")

// Common utilities among commands
func makeAPIRequest(method, url, apiKey string, payload []byte) ([]byte, error) {
commonLogger.Debugf("Making API request %#v %#v %#v %#v", method, url, apiKey, string(payload))
req, err := http.NewRequest(method, url, bytes.NewReader(payload))
req.Close = true
func makeAPIRequest(method, endpoint, apiKey string, payload []byte) ([]byte, error) {
commonLogger.Debugf("Making API request %#v %#v %#v %#v", method, endpoint, apiKey, string(payload))
req, err := http.NewRequest(method, endpoint, bytes.NewReader(payload))
if err != nil {
return nil, fmt.Errorf("could not construct API HTTP request: %s", err.Error())
}
req.Close = true
req.Header.Set("Authorization", "token "+apiKey)
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
if err.(*url.Error).Err == io.EOF {
commonLogger.Debugf("API request timed out")
return nil, err.(*url.Error).Err
}
return nil, fmt.Errorf("failed to send API HTTP request: %s", err.Error())
}
defer resp.Body.Close()
Expand Down
8 changes: 8 additions & 0 deletions cmd/fossa/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -45,6 +46,9 @@ func getBuild(endpoint, apiKey, project, revision string) (buildResponse, error)

testLogger.Debugf("Making Builds API request to: %#v", url)
res, err := makeAPIRequest(http.MethodPut, url, apiKey, nil)
if err == io.EOF {
return buildResponse{}, err
}
if err != nil {
return buildResponse{}, fmt.Errorf("could not make FOSSA build request: %s", err)
}
Expand Down Expand Up @@ -104,6 +108,10 @@ func doTest(s *spinner.Spinner, race chan testResult, endpoint, apiKey, project,
buildLoop:
for {
build, err := getBuild(endpoint, apiKey, project, revision)
if err == io.EOF {
time.Sleep(pollRequestDelay)
continue
}
if err != nil {
race <- testResult{err: err, issues: nil}
return
Expand Down
1 change: 1 addition & 0 deletions cmd/fossa/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func doUpload(config cliConfig, results []normalizedModule) (string, error) {

req, _ := http.NewRequest("POST", postURL, bytes.NewReader(buildData))
req.Close = true

req.Header.Set("Authorization", "token "+config.apiKey)
req.Header.Set("Content-Type", "application/json")

Expand Down

0 comments on commit d577588

Please sign in to comment.