Skip to content

Commit

Permalink
[default http client] optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
nntaoli committed Sep 23, 2024
1 parent 2fdbf71 commit d3aa0db
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/buger/jsonparser v1.1.1
github.com/google/uuid v1.3.0
github.com/nntaoli/go-tools v0.0.0-20221214092849-da8996a4cbdb
github.com/nntaoli/go-tools v0.0.0-20231117134637-ffc092526634
github.com/spf13/cast v1.5.0
github.com/valyala/fasthttp v1.47.0
)
Expand All @@ -16,6 +16,6 @@ require (
github.com/klauspost/compress v1.16.5 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/text v0.15.0 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/nntaoli/go-tools v0.0.0-20221214092849-da8996a4cbdb h1:DGUbqhO7N4k5HLiaiTvsdfnP5EnLaEloW1D2jVIlUP8=
github.com/nntaoli/go-tools v0.0.0-20221214092849-da8996a4cbdb/go.mod h1:+seRRDefTRThXov53ueVr+q/CxODlKnuqQ4hKU0YFY4=
github.com/nntaoli/go-tools v0.0.0-20231117134637-ffc092526634 h1:ww5OcxwjZDBJV61hQk2Bt1AYakozcYiJXNYy7+Q+mW0=
github.com/nntaoli/go-tools v0.0.0-20231117134637-ffc092526634/go.mod h1:+seRRDefTRThXov53ueVr+q/CxODlKnuqQ4hKU0YFY4=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
Expand All @@ -25,9 +25,9 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.47.0 h1:y7moDoxYzMooFpT5aHgNgVOQDrS3qlkfiP9mDtGGK9c=
github.com/valyala/fasthttp v1.47.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
12 changes: 8 additions & 4 deletions httpcli/default_http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/nntaoli-project/goex/v2/logger"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -75,8 +74,13 @@ func (cli *DefaultHttpClient) SetProxy(proxy string) error {
func (cli *DefaultHttpClient) DoRequest(method, rqUrl string, reqBody string, headers map[string]string) (data []byte, err error) {
logger.Debugf("[DefaultHttpClient] [%s] request url: %s", method, rqUrl)

reqTimeoutCtx, _ := context.WithTimeout(context.TODO(), cli.timeout)
req, _ := http.NewRequestWithContext(reqTimeoutCtx, method, rqUrl, strings.NewReader(reqBody))
reqTimeoutCtx, cancelFn := context.WithTimeout(context.TODO(), cli.timeout)
defer cancelFn()

req, err := http.NewRequestWithContext(reqTimeoutCtx, method, rqUrl, strings.NewReader(reqBody))
if err != nil {
return nil, fmt.Errorf("failed to create new request: %w", err)
}

if headers != nil {
for k, v := range headers {
Expand All @@ -96,7 +100,7 @@ func (cli *DefaultHttpClient) DoRequest(method, rqUrl string, reqBody string, he
}
}(resp.Body)

bodyData, err := ioutil.ReadAll(resp.Body)
bodyData, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read response body error: %w", err)
}
Expand Down

0 comments on commit d3aa0db

Please sign in to comment.