Skip to content

Commit

Permalink
encoding: remove SourceNil; httpcli: drain response body
Browse files Browse the repository at this point in the history
  • Loading branch information
James DeFelice committed Sep 15, 2017
1 parent b08f1ba commit c10cbad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
8 changes: 0 additions & 8 deletions api/v1/lib/encoding/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ var (
_ = SinkFactory(SinkFactoryFunc(nil))
)

func SourceNil(r io.Reader) Source {
return func() framing.Reader {
return framing.ReaderFunc(func() ([]byte, error) {
return nil, io.EOF
})
}
}

// SourceReader returns a Source that buffers all input from the given io.Reader
// and returns the contents in a single frame.
func SourceReader(r io.Reader) Source {
Expand Down
27 changes: 23 additions & 4 deletions api/v1/lib/httpcli/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"sync"
Expand Down Expand Up @@ -287,7 +288,7 @@ func validateSuccessfulResponse(codec encoding.Codec, res *http.Response, rc cli
func responseToSource(res *http.Response, rc client.ResponseClass) encoding.SourceFactoryFunc {
switch rc {
case client.ResponseClassNoData:
return encoding.SourceNil
return nil
case client.ResponseClassSingleton:
return encoding.SourceReader
case client.ResponseClassStreaming, client.ResponseClassAuto:
Expand Down Expand Up @@ -328,17 +329,35 @@ func (c *Client) HandleResponse(res *http.Response, rc client.ResponseClass, err
switch res.StatusCode {
case http.StatusOK:
debug.Log("request OK, decoding response")

sf := responseToSource(res, rc)
if sf == nil {
if rc != client.ResponseClassNoData {
panic("nil Source for response that expected data")
}
// we don't expect any data. drain the response body and close it (compliant with golang's expectations
// for http/1.1 keepalive support.
defer res.Body.Close()
_, err = io.Copy(ioutil.Discard, res.Body)
return nil, err
}

result.Decoder = c.codec.NewDecoder(sf.NewSource(res.Body))

case http.StatusAccepted:
debug.Log("request Accepted")

// noop; no decoder for these types of calls
defer res.Body.Close()
_, err = io.Copy(ioutil.Discard, res.Body)
return nil, err

default:
// don't close the response here because the caller may want to evaluate the entity.
// it's the caller's job to Close the returned response.
return result, ProtocolError(fmt.Sprintf("unexpected mesos HTTP response code: %d", res.StatusCode))
debug.Log("unexpected HTTP status", res.StatusCode)

defer res.Body.Close()
io.Copy(ioutil.Discard, res.Body) // intentionally discard any error here
return nil, ProtocolError(fmt.Sprintf("unexpected mesos HTTP response code: %d", res.StatusCode))
}

return result, nil
Expand Down

0 comments on commit c10cbad

Please sign in to comment.