From d3aa0db8d754ad1bf2418284b684a2e48605625e Mon Sep 17 00:00:00 2001 From: nntaoli <2767415655@qq.com> Date: Mon, 23 Sep 2024 17:03:56 +0800 Subject: [PATCH] [default http client] optimize --- go.mod | 6 +++--- go.sum | 12 ++++++------ httpcli/default_http_client.go | 12 ++++++++---- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 82415a81..67a6f27b 100644 --- a/go.mod +++ b/go.mod @@ -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 ) @@ -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 ) diff --git a/go.sum b/go.sum index 5e295871..fb57c7cb 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/httpcli/default_http_client.go b/httpcli/default_http_client.go index 06e64b58..20d61603 100644 --- a/httpcli/default_http_client.go +++ b/httpcli/default_http_client.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/nntaoli-project/goex/v2/logger" "io" - "io/ioutil" "net/http" "net/url" "strings" @@ -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 { @@ -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) }