Skip to content

Commit

Permalink
web: dont use context as cannot reliably cancel from within GetResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
l3uddz committed Feb 19, 2020
1 parent 3bfa624 commit eb72a2b
Showing 1 changed file with 15 additions and 28 deletions.
43 changes: 15 additions & 28 deletions utils/web/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (

var (
// Logging
log = logger.GetLogger("web")
log = logger.GetLogger("web")
httpClient = *req.Client()
)

/* Structs */
Expand Down Expand Up @@ -48,35 +49,24 @@ const (
func init() {
// dont json escape html
req.SetJSONEscapeHTML(false)
// use timeout from context
req.SetTimeout(0)
}

func getInputsWithTimeout(inputs []interface{}, timeout int) []interface{} {
// return existing inputs when no timeout provided
if timeout == 0 {
return inputs
}

// make copy of inputs to return
newInputs := make([]interface{}, 0)
for _, v := range inputs {
newInputs = append(newInputs, v)
}

// add timeout context
ctx, _ := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
newInputs = append(newInputs, ctx)

return newInputs
// use timeout from context
httpClient.Timeout = time.Duration(0)
}

/* Public */

func GetResponse(method HTTPMethod, requestUrl string, timeout int, v ...interface{}) (*req.Resp, error) {
// prepare request
reqInputs := make([]interface{}, 0)
inputs := make([]interface{}, 0)

// prepare client
client := httpClient
if timeout > 0 {
client.Timeout = time.Duration(timeout) * time.Second
}
inputs = append(inputs, &client)

// prepare request
var rl ratelimit.Limiter = nil
var retry Retry

Expand All @@ -91,7 +81,7 @@ func GetResponse(method HTTPMethod, requestUrl string, timeout int, v ...interfa
case Retry:
retry = vT
default:
reqInputs = append(reqInputs, vT)
inputs = append(inputs, vT)
}
}

Expand All @@ -101,9 +91,6 @@ func GetResponse(method HTTPMethod, requestUrl string, timeout int, v ...interfa

// Exponential backoff
for {
// set inputs
inputs := getInputsWithTimeout(reqInputs, timeout)

// do request
switch method {
case GET:
Expand All @@ -126,7 +113,7 @@ func GetResponse(method HTTPMethod, requestUrl string, timeout int, v ...interfa
// validate response
if err != nil {
log.WithError(err).Debugf("Failed requesting: %q", requestUrl)
if os.IsTimeout(err) {
if os.IsTimeout(err) || err == context.Canceled {
if retry.MaxAttempts == 0 || retry.Attempt() >= retry.MaxAttempts {
return nil, err
}
Expand Down

0 comments on commit eb72a2b

Please sign in to comment.