Cleanup http client Send

License: MIT
Signed-off-by: rht <rhtbot@gmail.com>
This commit is contained in:
rht 2016-01-30 10:22:46 +07:00
parent 45999946be
commit 72e98deb93

View File

@ -38,16 +38,9 @@ type client struct {
}
func NewClient(address string) Client {
// We cannot use the default transport because of a bug in go's connection reuse
// code. It causes random failures in the connection including io.EOF and connection
// refused on 'client.Do'
return &client{
serverAddress: address,
httpClient: &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
},
},
httpClient: http.DefaultClient,
}
}
@ -101,39 +94,28 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
}
httpReq.Header.Set(uaHeader, config.ApiVersion)
ec := make(chan error, 1)
rc := make(chan cmds.Response, 1)
httpReq.Cancel = req.Context().Done()
httpReq.Close = true
go func() {
httpRes, err := c.httpClient.Do(httpReq)
if err != nil {
ec <- err
return
}
// using the overridden JSON encoding in request
res, err := getResponse(httpRes, req)
if err != nil {
ec <- err
return
}
rc <- res
}()
select {
case err := <-ec:
httpRes, err := c.httpClient.Do(httpReq)
if err != nil {
return nil, err
case res := <-rc:
if found && len(previousUserProvidedEncoding) > 0 {
// reset to user provided encoding after sending request
// NB: if user has provided an encoding but it is the empty string,
// still leave it as JSON.
req.SetOption(cmds.EncShort, previousUserProvidedEncoding)
}
return res, nil
}
// using the overridden JSON encoding in request
res, err := getResponse(httpRes, req)
if err != nil {
return nil, err
}
if found && len(previousUserProvidedEncoding) > 0 {
// reset to user provided encoding after sending request
// NB: if user has provided an encoding but it is the empty string,
// still leave it as JSON.
req.SetOption(cmds.EncShort, previousUserProvidedEncoding)
}
return res, nil
}
func getQuery(req cmds.Request) (string, error) {