@cryptotaxi247 / kubo / commits / 45999946b

Directly wire ctx into http request

License: MIT Signed-off-by: rht <rhtbot@gmail.com>

rht committed Jan 30, 2016 at 10:19 UTC 45999946be1b6c1e490c00233ba617f758e6e564
1 file changed +13 -20
commands/http/client.go
+13 -20
@@ -34,7 +34,7 @@ type Client interface {
34
35 type client struct {
36 serverAddress string
37 - httpClient http.Client
37 + httpClient *http.Client
38 }
39
40 func NewClient(address string) Client {
@@ -43,7 +43,7 @@ func NewClient(address string) Client {
43 // refused on 'client.Do'
44 return &client{
45 serverAddress: address,
46 - httpClient: http.Client{
46 + httpClient: &http.Client{
47 Transport: &http.Transport{
48 DisableKeepAlives: true,
49 },
@@ -103,7 +103,7 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
103
104 ec := make(chan error, 1)
105 rc := make(chan cmds.Response, 1)
106 - dc := req.Context().Done()
106 + httpReq.Cancel = req.Context().Done()
107
108 go func() {
109 httpRes, err := c.httpClient.Do(httpReq)
@@ -122,24 +122,17 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
122 rc <- res
123 }()
124
125 - for {
126 - select {
127 - case <-dc:
128 - log.Debug("Context cancelled, cancelling HTTP request...")
129 - tr := http.DefaultTransport.(*http.Transport)
130 - tr.CancelRequest(httpReq)
131 - dc = nil // Wait for ec or rc
132 - case err := <-ec:
133 - return nil, err
134 - case res := <-rc:
135 - if found && len(previousUserProvidedEncoding) > 0 {
136 - // reset to user provided encoding after sending request
137 - // NB: if user has provided an encoding but it is the empty string,
138 - // still leave it as JSON.
139 - req.SetOption(cmds.EncShort, previousUserProvidedEncoding)
140 - }
141 - return res, nil
125 + select {
126 + case err := <-ec:
127 + return nil, err
128 + case res := <-rc:
129 + if found && len(previousUserProvidedEncoding) > 0 {
130 + // reset to user provided encoding after sending request
131 + // NB: if user has provided an encoding but it is the empty string,
132 + // still leave it as JSON.
133 + req.SetOption(cmds.EncShort, previousUserProvidedEncoding)
134 }
135 + return res, nil
136 }
137 }
138