Skip to content

Commit

Permalink
Merge pull request #892 from go-resty/code-optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm authored Oct 26, 2024
2 parents a6a489b + cf921ad commit 1c4960b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
18 changes: 10 additions & 8 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,19 @@ func createHTTPRequest(c *Client, r *Request) (err error) {
r.RawRequest = r.RawRequest.WithContext(r.ctx)
}

bodyCopy, err := getBodyCopy(r)
if err != nil {
return err
}

// assign get body func for the underlying raw request instance
r.RawRequest.GetBody = func() (io.ReadCloser, error) {
if r.RawRequest.GetBody == nil {
bodyCopy, err := getBodyCopy(r)
if err != nil {
return err
}
if bodyCopy != nil {
return io.NopCloser(bytes.NewReader(bodyCopy.Bytes())), nil
buf := bodyCopy.Bytes()
r.RawRequest.GetBody = func() (io.ReadCloser, error) {
b := bytes.NewReader(buf)
return io.NopCloser(b), nil
}
}
return nil, nil
}

return
Expand Down
28 changes: 14 additions & 14 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ func TestClientRetryWait(t *testing.T) {
retryIntervals := make([]uint64, retryCount+1)

// Set retry wait times that do not intersect with default ones
retryWaitTime := time.Duration(3) * time.Second
retryMaxWaitTime := time.Duration(9) * time.Second
retryWaitTime := time.Duration(50) * time.Millisecond
retryMaxWaitTime := time.Duration(150) * time.Millisecond

c := dc().
SetRetryCount(retryCount).
Expand Down Expand Up @@ -262,7 +262,7 @@ func TestClientRetryWaitMaxInfinite(t *testing.T) {
retryIntervals := make([]uint64, retryCount+1)

// Set retry wait times that do not intersect with default ones
retryWaitTime := time.Duration(3) * time.Second
retryWaitTime := time.Duration(100) * time.Millisecond
retryMaxWaitTime := time.Duration(-1.0) // negative value

c := dc().
Expand Down Expand Up @@ -319,8 +319,8 @@ func TestClientRetryWaitCallbackError(t *testing.T) {
retryIntervals := make([]uint64, retryCount+1)

// Set retry wait times that do not intersect with default ones
retryWaitTime := 3 * time.Second
retryMaxWaitTime := 9 * time.Second
retryWaitTime := 50 * time.Millisecond
retryMaxWaitTime := 150 * time.Millisecond

retryAfter := func(client *Client, resp *Response) (time.Duration, error) {
return 0, errors.New("quota exceeded")
Expand Down Expand Up @@ -359,11 +359,11 @@ func TestClientRetryWaitCallback(t *testing.T) {
retryIntervals := make([]uint64, retryCount+1)

// Set retry wait times that do not intersect with default ones
retryWaitTime := 3 * time.Second
retryMaxWaitTime := 9 * time.Second
retryWaitTime := 50 * time.Millisecond
retryMaxWaitTime := 150 * time.Millisecond

retryAfter := func(client *Client, resp *Response) (time.Duration, error) {
return 5 * time.Second, nil
return 50 * time.Millisecond, nil
}

c := dc().
Expand Down Expand Up @@ -407,11 +407,11 @@ func TestClientRetryWaitCallbackTooShort(t *testing.T) {
retryIntervals := make([]uint64, retryCount+1)

// Set retry wait times that do not intersect with default ones
retryWaitTime := 3 * time.Second
retryMaxWaitTime := 9 * time.Second
retryWaitTime := 50 * time.Millisecond
retryMaxWaitTime := 150 * time.Millisecond

retryAfter := func(client *Client, resp *Response) (time.Duration, error) {
return 2 * time.Second, nil // too short duration
return 10 * time.Millisecond, nil // too short duration
}

c := dc().
Expand Down Expand Up @@ -455,11 +455,11 @@ func TestClientRetryWaitCallbackTooLong(t *testing.T) {
retryIntervals := make([]uint64, retryCount+1)

// Set retry wait times that do not intersect with default ones
retryWaitTime := 1 * time.Second
retryMaxWaitTime := 3 * time.Second
retryWaitTime := 10 * time.Millisecond
retryMaxWaitTime := 100 * time.Millisecond

retryAfter := func(client *Client, resp *Response) (time.Duration, error) {
return 4 * time.Second, nil // too long duration
return 150 * time.Millisecond, nil // too long duration
}

c := dc().
Expand Down

0 comments on commit 1c4960b

Please sign in to comment.