Skip to content

Commit

Permalink
Update xhttp XRequest.RetryCount
Browse files Browse the repository at this point in the history
  • Loading branch information
onanying committed Oct 13, 2023
1 parent dcead17 commit fab6031
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/xutil/xhttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
```
Expand Down
4 changes: 2 additions & 2 deletions src/xutil/xhttp/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
27 changes: 18 additions & 9 deletions src/xutil/xhttp/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import (

type XRequest struct {
*http.Request

Body Body

// Number of retries
RetryCount int
}

type XResponse struct {
Expand Down Expand Up @@ -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
}

0 comments on commit fab6031

Please sign in to comment.