From 76d93b5571b3b427cf3973c368dcbeab6d3bbe2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E5=BB=BA=E5=8D=8E?= Date: Fri, 24 Nov 2023 16:43:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- component/requests/client.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/component/requests/client.go b/component/requests/client.go index 59cd4dc..4ced3c0 100644 --- a/component/requests/client.go +++ b/component/requests/client.go @@ -48,8 +48,10 @@ func HWithTimeout(timeout time.Duration) HOpt { } func NewHTTPClient(hOPts ...HOpt) *HTTPClient { + logger := log.NewStdLogger() cli := &HTTPClient{ - client: resty.New(), + client: resty.New().SetLogger(logger), + logger: logger, } for _, fn := range hOPts { @@ -110,6 +112,16 @@ func (api *HTTPClient) getRetryWaitTime(request *HTTPRequest) time.Duration { return 100 * time.Millisecond } +func (api *HTTPClient) getTimeout(request *HTTPRequest) time.Duration { + if request.Timeout > 0 { + return request.Timeout + } else if api.timeout > 0 { + return api.timeout + } + + return 0 +} + func (api *HTTPClient) do(method, url string, rOpts ...ROpt) (*HTTPResponse, error) { request := NewRequest(rOpts...) client := api.client @@ -125,6 +137,11 @@ func (api *HTTPClient) do(method, url string, rOpts ...ROpt) (*HTTPResponse, err return false }) } + + if timeout := api.getTimeout(request); timeout > 0 { + client.SetTimeout(timeout) + } + r := client.R() request.setAttributes(r) response, err := r.Execute(method, url)