Skip to content

Commit 8e5861e

Browse files
committed
use json.Decode instead of json.Unmarshal
1 parent bfef312 commit 8e5861e

File tree

2 files changed

+27
-39
lines changed

2 files changed

+27
-39
lines changed

httputils/http.go

+16-25
Original file line numberDiff line numberDiff line change
@@ -32,47 +32,38 @@ type Response struct {
3232
}
3333

3434
// Do will excute the request with the default http client
35-
func Do(req *http.Request) (Response, error) {
35+
func Do(req *http.Request) (*Response, error) {
3636
return ClientDo(DefaultHTTPClient, req)
3737
}
3838

3939
// ClientDo will excute the request with a specific http client
40-
func ClientDo(client *http.Client, req *http.Request, streamResp ...bool) (Response, error) {
41-
// TODO: process can be canceled by context
42-
//ctx := req.Context()
43-
//select {
44-
//}
40+
func ClientDo(client *http.Client, req *http.Request, streamResp ...bool) (*Response, error) {
4541
isStream := false
4642
if len(streamResp) > 0 && streamResp[0] == true {
4743
isStream = true
4844
}
4945
resp, err := client.Do(req)
50-
if !isStream && resp != nil {
51-
defer resp.Body.Close()
52-
}
5346
if err != nil {
54-
if isStream && resp != nil {
55-
resp.Body.Close()
56-
}
57-
return Response{}, fmt.Errorf(
58-
"do request(%s) failed: %s", req.URL, err)
47+
return nil, err
5948
}
6049

6150
// return a stream
6251
if isStream {
63-
return Response{
52+
return &Response{
6453
Status: resp.StatusCode,
6554
Header: resp.Header,
6655
BodyStream: resp.Body,
6756
}, nil
6857
}
58+
59+
defer resp.Body.Close()
6960
// return the data
7061
body, err := ioutil.ReadAll(resp.Body)
7162
if err != nil {
72-
return Response{}, fmt.Errorf(
63+
return nil, fmt.Errorf(
7364
"read response body failed:%v", err)
7465
}
75-
return Response{
66+
return &Response{
7667
Status: resp.StatusCode,
7768
Header: resp.Header,
7869
Body: body,
@@ -105,27 +96,27 @@ func NewRequest(ctx context.Context, method string, url string, headers map[stri
10596
return req, nil
10697
}
10798

108-
func doRequest(ctx context.Context, method string, url string, headers map[string]string, query url.Values, body io.Reader) (Response, error) {
99+
func doRequest(ctx context.Context, method string, url string, headers map[string]string, query url.Values, body io.Reader) (*Response, error) {
109100
req, err := NewRequest(ctx, method, url, headers, query, body)
110101
if err != nil {
111-
return Response{}, err
102+
return nil, err
112103
}
113104

114105
return Do(req)
115106
}
116107

117108
// Get will get remote data with custom headers
118-
func Get(ctx context.Context, url string, headers map[string]string, query url.Values) (Response, error) {
109+
func Get(ctx context.Context, url string, headers map[string]string, query url.Values) (*Response, error) {
119110
return doRequest(ctx, "GET", url, headers, query, nil)
120111
}
121112

122113
// Post will create remote resource
123-
func Post(ctx context.Context, url string, headers map[string]string, query url.Values, body io.Reader) (Response, error) {
114+
func Post(ctx context.Context, url string, headers map[string]string, query url.Values, body io.Reader) (*Response, error) {
124115
return doRequest(ctx, "POST", url, headers, query, body)
125116
}
126117

127118
// PostForm will create remote resource with x-www-form-urlencoded format data
128-
func PostForm(ctx context.Context, url string, form url.Values) (Response, error) {
119+
func PostForm(ctx context.Context, url string, form url.Values) (*Response, error) {
129120
return Post(ctx,
130121
url,
131122
map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
@@ -134,16 +125,16 @@ func PostForm(ctx context.Context, url string, form url.Values) (Response, error
134125
}
135126

136127
// Put will update a remote resource
137-
func Put(ctx context.Context, url string, headers map[string]string, query url.Values, body io.Reader) (Response, error) {
128+
func Put(ctx context.Context, url string, headers map[string]string, query url.Values, body io.Reader) (*Response, error) {
138129
return doRequest(ctx, "PUT", url, headers, query, body)
139130
}
140131

141132
// Patch will partially update a remote resource
142-
func Patch(ctx context.Context, url string, headers map[string]string, query url.Values, body io.Reader) (Response, error) {
133+
func Patch(ctx context.Context, url string, headers map[string]string, query url.Values, body io.Reader) (*Response, error) {
143134
return doRequest(ctx, "PATCH", url, headers, query, body)
144135
}
145136

146137
// Delete will delete remote resource
147-
func Delete(ctx context.Context, url string, headers map[string]string, query url.Values) (Response, error) {
138+
func Delete(ctx context.Context, url string, headers map[string]string, query url.Values) (*Response, error) {
148139
return doRequest(ctx, "DELETE", url, headers, query, nil)
149140
}

httputils/rest.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"crypto/tls"
77
"encoding/json"
8-
"fmt"
98
"io"
109
"net/http"
1110
"net/url"
@@ -266,7 +265,7 @@ func (rest *RestCli) Do() (*Response, error) {
266265
req.Close = true
267266
}
268267

269-
resp, err := ClientDo(rest.cli, req, rest.isStream)
268+
resp, err := ClientDo(rest.cli, req, true) // always return a Body Reader, avoid memory copy
270269
if err != nil {
271270
if rest.debug >= Debug1 {
272271
tracer.Error("do request failed:", err)
@@ -278,14 +277,9 @@ func (rest *RestCli) Do() (*Response, error) {
278277
}
279278

280279
if rest.isStream {
281-
return &resp, nil
282-
}
283-
284-
if rest.debug >= Debug2 {
285-
if len(resp.Body) > 0 {
286-
tracer.Infof("resp body: %v", string(resp.Body))
287-
}
280+
return resp, nil
288281
}
282+
defer resp.BodyStream.Close()
289283

290284
if len(rest.into) > 0 {
291285
status := strconv.Itoa(resp.Status)
@@ -298,17 +292,20 @@ func (rest *RestCli) Do() (*Response, error) {
298292
}
299293
for _, status := range ss {
300294
if rsp, exist := rest.into[status]; exist {
301-
err = json.Unmarshal(resp.Body, rsp)
295+
err = json.NewDecoder(resp.BodyStream).Decode(rsp)
302296
if err != nil {
303297
if rest.debug >= Debug1 {
304-
tracer.Errorf("unmarshal resp failed: %s, body: %s", err, string(resp.Body))
298+
tracer.Errorf("unmarshal resp failed: %s", err)
305299
}
306-
return nil, fmt.Errorf("err: %s, body: %s", err, string(resp.Body))
300+
return nil, err
301+
}
302+
if rest.debug >= Debug2 {
303+
tracer.Infof("resp: %+v", rsp)
307304
}
308-
return &resp, nil
305+
return resp, nil
309306
}
310307
}
311308
}
312309
}
313-
return &resp, nil
310+
return resp, nil
314311
}

0 commit comments

Comments
 (0)