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 54604c9

Browse files
committedNov 23, 2015
Cleanup http client Send
License: MIT Signed-off-by: rht <rhtbot@gmail.com>
1 parent a2e3c69 commit 54604c9

File tree

1 file changed

+17
-36
lines changed

1 file changed

+17
-36
lines changed
 

‎commands/http/client.go

+17-36
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,9 @@ type client struct {
3434
}
3535

3636
func NewClient(address string) Client {
37-
// We cannot use the default transport because of a bug in go's connection reuse
38-
// code. It causes random failures in the connection including io.EOF and connection
39-
// refused on 'client.Do'
4037
return &client{
4138
serverAddress: address,
42-
httpClient: &http.Client{
43-
Transport: &http.Transport{
44-
DisableKeepAlives: true,
45-
},
46-
},
39+
httpClient: http.DefaultClient,
4740
}
4841
}
4942

@@ -103,39 +96,27 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
10396
version := config.CurrentVersionNumber
10497
httpReq.Header.Set(uaHeader, fmt.Sprintf("/go-ipfs/%s/", version))
10598

106-
ec := make(chan error, 1)
107-
rc := make(chan cmds.Response, 1)
10899
httpReq.Cancel = req.Context().Done()
100+
httpReq.Close = true
109101

110-
go func() {
111-
httpRes, err := c.httpClient.Do(httpReq)
112-
if err != nil {
113-
ec <- err
114-
return
115-
}
116-
117-
// using the overridden JSON encoding in request
118-
res, err := getResponse(httpRes, req)
119-
if err != nil {
120-
ec <- err
121-
return
122-
}
123-
124-
rc <- res
125-
}()
102+
httpRes, err := c.httpClient.Do(httpReq)
103+
if err != nil {
104+
return nil, err
105+
}
126106

127-
select {
128-
case err := <-ec:
107+
// using the overridden JSON encoding in request
108+
res, err := getResponse(httpRes, req)
109+
if err != nil {
129110
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)
136-
}
137-
return res, nil
138111
}
112+
113+
if found && len(previousUserProvidedEncoding) > 0 {
114+
// reset to user provided encoding after sending request
115+
// NB: if user has provided an encoding but it is the empty string,
116+
// still leave it as JSON.
117+
req.SetOption(cmds.EncShort, previousUserProvidedEncoding)
118+
}
119+
return res, nil
139120
}
140121

141122
func getQuery(req cmds.Request) (string, error) {

0 commit comments

Comments
 (0)
Please sign in to comment.