From fab6031ad9082768c591d029c9ac5bbf9d52e8c9 Mon Sep 17 00:00:00 2001 From: liujian Date: Fri, 13 Oct 2023 14:40:12 +0800 Subject: [PATCH] Update xhttp XRequest.RetryCount --- src/xutil/xhttp/README.md | 4 ++-- src/xutil/xhttp/log.go | 4 ++-- src/xutil/xhttp/request.go | 27 ++++++++++++++++++--------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/xutil/xhttp/README.md b/src/xutil/xhttp/README.md index 24f0c1c..87b89f2 100644 --- a/src/xutil/xhttp/README.md +++ b/src/xutil/xhttp/README.md @@ -36,8 +36,8 @@ The log object contains the following fields ```go type Log struct { Duration time.Duration `json:"duration"` - Request *XRequest `json:"request"` - Response *XResponse `json:"response"` // if error this field is equal to nil + Request *XRequest `json:"request"` // The XRequest.RetryCount field records the number of retries that have been completed. + Response *XResponse `json:"response"` // If request error this field is equal to nil Error error `json:"error"` } ``` diff --git a/src/xutil/xhttp/log.go b/src/xutil/xhttp/log.go index db6e4e2..c53940e 100644 --- a/src/xutil/xhttp/log.go +++ b/src/xutil/xhttp/log.go @@ -6,8 +6,8 @@ import ( type Log struct { Duration time.Duration `json:"duration"` - Request *XRequest `json:"request"` - Response *XResponse `json:"response"` // if error this field is equal to nil + Request *XRequest `json:"request"` // The XRequest.RetryCount field records the number of retries that have been completed. + Response *XResponse `json:"response"` // If request error this field is equal to nil Error error `json:"error"` } diff --git a/src/xutil/xhttp/request.go b/src/xutil/xhttp/request.go index efac330..56c60d2 100644 --- a/src/xutil/xhttp/request.go +++ b/src/xutil/xhttp/request.go @@ -11,7 +11,11 @@ import ( type XRequest struct { *http.Request + Body Body + + // Number of retries + RetryCount int } type XResponse struct { @@ -84,36 +88,41 @@ func Request(method string, u string, opts ...RequestOption) (*XResponse, error) } if o.RetryOptions != nil { + xReq := newXRequest(req) return doRetry(o, func() (*XResponse, error) { - return doRequest(o, req) + return doRequest(o, xReq) }) } - return doRequest(o, req) + return doRequest(o, newXRequest(req)) } func Do(req *http.Request, opts ...RequestOption) (*XResponse, error) { o := mergeOptions(opts) if o.RetryOptions != nil { + xReq := newXRequest(req) return doRetry(o, func() (*XResponse, error) { - return doRequest(o, req) + return doRequest(o, xReq) }) } - return doRequest(o, req) + return doRequest(o, newXRequest(req)) } -func doRequest(opts *requestOptions, req *http.Request) (*XResponse, error) { +func doRequest(opts *requestOptions, req *XRequest) (*XResponse, error) { + defer func() { + req.RetryCount++ + }() + cli := http.Client{ Timeout: opts.Timeout, } startTime := time.Now() - xReq := newXRequest(req) - r, err := cli.Do(req) + r, err := cli.Do(req.Request) if err != nil { - doDebug(opts, time.Now().Sub(startTime), xReq, nil, err) + doDebug(opts, time.Now().Sub(startTime), req, nil, err) return nil, err } xResp := newXResponse(r) - doDebug(opts, time.Now().Sub(startTime), xReq, xResp, nil) + doDebug(opts, time.Now().Sub(startTime), req, xResp, nil) return xResp, nil }