Skip to content

Commit

Permalink
feat: 添加超时时间
Browse files Browse the repository at this point in the history
  • Loading branch information
吴建华 committed Nov 24, 2023
1 parent 41e8ee6 commit 76d93b5
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion component/requests/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 76d93b5

Please sign in to comment.