Commit 54f0ac4c authored by ale's avatar ale

Prevent premature cancellation of the HTTP request context

The Context in the http.Request is bound to the one we give it when
calling balancedBackend.do(), so we were accessing it out of scope
when calling the final json.Decode on the response body.

This was not a problem for most *small* requests, as the response Body
already contains all the data due to having read it along with the
headers. However, larger response bodies would cause json.Decode to
call a Read with what at that point is a canceled Context, so the Call
function would return a mysterious "context canceled" error.

(Note that this change introduces a minor, probably less annoying,
issue, where we can't reset "resp" between successive calls so we may
be invoking json.Decode on a non-pristine object if the first call
fails in some circumstances).
parent b4364e84
......@@ -90,7 +90,7 @@ func newErrorHTTPServer(statusCode int) *httpServer {
func newJSONHTTPServer() *httpServer {
return &httpServer{httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, "{\"value\": 42}")
io.WriteString(w, "{\"value\": 42}") // nolint
}))}
}
......@@ -99,7 +99,7 @@ func newHostCountingJSONHTTPServer() (*httpServer, map[string]int) {
return &httpServer{httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
counters[r.Host]++
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, "{\"value\": 42}")
io.WriteString(w, "{\"value\": 42}") // nolint
}))}, counters
}
......
......@@ -118,30 +118,31 @@ func (b *balancedBackend) Call(ctx context.Context, shard, path string, req, res
// Call the backends in the sequence until one succeeds, with an
// exponential backoff policy controlled by the outer Context.
var httpResp *http.Response
err = backoff.Retry(func() error {
return backoff.Retry(func() error {
req, rerr := b.newJSONRequest(path, shard, data)
if rerr != nil {
return rerr
}
innerCtx, cancel := context.WithTimeout(ctx, innerTimeout)
httpResp, rerr = b.do(innerCtx, seq, req)
cancel()
return rerr
}, backoff.WithContext(newExponentialBackOff(), ctx))
if err != nil {
return err
}
defer httpResp.Body.Close() // nolint
defer cancel()
// Decode the response.
if httpResp.Header.Get("Content-Type") != "application/json" {
return errors.New("not a JSON response")
}
if resp == nil {
return nil
}
return json.NewDecoder(httpResp.Body).Decode(resp)
// When do() returns successfully, we already know that the
// response had an HTTP status of 200.
httpResp, rerr := b.do(innerCtx, seq, req)
if rerr != nil {
return rerr
}
defer httpResp.Body.Close() // nolint
// Decode the response, unless the 'resp' output is nil.
if httpResp.Header.Get("Content-Type") != "application/json" {
return errors.New("not a JSON response")
}
if resp == nil {
return nil
}
return json.NewDecoder(httpResp.Body).Decode(resp)
}, backoff.WithContext(newExponentialBackOff(), ctx))
}
// Initialize a new target sequence.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment