Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a2e3c69

Browse files
committedNov 23, 2015
Directly wire ctx into http request
License: MIT Signed-off-by: rht <rhtbot@gmail.com>
1 parent b4ab684 commit a2e3c69

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed
 

‎commands/http/client.go

+13-20
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type Client interface {
3030

3131
type client struct {
3232
serverAddress string
33-
httpClient http.Client
33+
httpClient *http.Client
3434
}
3535

3636
func NewClient(address string) Client {
@@ -39,7 +39,7 @@ func NewClient(address string) Client {
3939
// refused on 'client.Do'
4040
return &client{
4141
serverAddress: address,
42-
httpClient: http.Client{
42+
httpClient: &http.Client{
4343
Transport: &http.Transport{
4444
DisableKeepAlives: true,
4545
},
@@ -105,7 +105,7 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
105105

106106
ec := make(chan error, 1)
107107
rc := make(chan cmds.Response, 1)
108-
dc := req.Context().Done()
108+
httpReq.Cancel = req.Context().Done()
109109

110110
go func() {
111111
httpRes, err := c.httpClient.Do(httpReq)
@@ -124,24 +124,17 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
124124
rc <- res
125125
}()
126126

127-
for {
128-
select {
129-
case <-dc:
130-
log.Debug("Context cancelled, cancelling HTTP request...")
131-
tr := http.DefaultTransport.(*http.Transport)
132-
tr.CancelRequest(httpReq)
133-
dc = nil // Wait for ec or rc
134-
case err := <-ec:
135-
return nil, err
136-
case res := <-rc:
137-
if found && len(previousUserProvidedEncoding) > 0 {
138-
// reset to user provided encoding after sending request
139-
// NB: if user has provided an encoding but it is the empty string,
140-
// still leave it as JSON.
141-
req.SetOption(cmds.EncShort, previousUserProvidedEncoding)
142-
}
143-
return res, nil
127+
select {
128+
case err := <-ec:
129+
return nil, err
130+
case res := <-rc:
131+
if found && len(previousUserProvidedEncoding) > 0 {
132+
// reset to user provided encoding after sending request
133+
// NB: if user has provided an encoding but it is the empty string,
134+
// still leave it as JSON.
135+
req.SetOption(cmds.EncShort, previousUserProvidedEncoding)
144136
}
137+
return res, nil
145138
}
146139
}
147140

0 commit comments

Comments
 (0)
Please sign in to comment.