Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented new retry system #566

Merged
merged 5 commits into from
Aug 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Addressed PR comments
  • Loading branch information
ezilber-akamai committed Aug 14, 2024
commit 79b98e87e3a83a2711ffd9e1ab61251d1050d891
53 changes: 35 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ type RequestParams struct {

// Generic helper to execute HTTP requests using the net/http package
//
// nolint:unused, gocognit
// nolint:unused, funlen, gocognit
func (c *httpClient) doRequest(ctx context.Context, method, url string, params RequestParams, mutators ...func(req *http.Request) error) error {
var (
req *http.Request
Expand All @@ -158,21 +158,34 @@ func (c *httpClient) doRequest(ctx context.Context, method, url string, params R
c.logRequest(req, method, url, bodyBuffer)
}

resp, err = c.sendRequest(req)
if err == nil { //nolint:nestif
defer resp.Body.Close()
if err = c.checkHTTPError(resp); err == nil {
if c.debug && c.logger != nil {
resp, err = c.logResponse(resp)
if err != nil {
return err
}
processResponse := func() error {
defer func() {
closeErr := resp.Body.Close()
if closeErr != nil && err == nil {
err = closeErr
}
}()
if err = c.checkHTTPError(resp); err != nil {
return err
}
if c.debug && c.logger != nil {
var logErr error
resp, logErr = c.logResponse(resp)
if logErr != nil {
return logErr
}
if params.Response != nil {
if err = c.decodeResponseBody(resp, params.Response); err != nil {
return err
}
}
if params.Response != nil {
if err = c.decodeResponseBody(resp, params.Response); err != nil {
return err
}
}
return nil
}

resp, err = c.sendRequest(req)
if err == nil {
if err = processResponse(); err == nil {
return nil
}
}
Expand All @@ -185,6 +198,10 @@ func (c *httpClient) doRequest(ctx context.Context, method, url string, params R
if retryErr != nil {
return retryErr
}

// Sleep for the specified duration before retrying.
// If retryAfter is 0 (i.e., Retry-After header is not found),
// no delay is applied.
time.Sleep(retryAfter)
ezilber-akamai marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -604,14 +621,14 @@ func (c *Client) UseCache(value bool) {
}

// SetRetryMaxWaitTime sets the maximum delay before retrying a request.
func (c *Client) SetRetryMaxWaitTime(max time.Duration) *Client {
c.resty.SetRetryMaxWaitTime(max)
func (c *Client) SetRetryMaxWaitTime(maxWaitTime time.Duration) *Client {
c.resty.SetRetryMaxWaitTime(maxWaitTime)
return c
}

// SetRetryWaitTime sets the default (minimum) delay before retrying a request.
func (c *Client) SetRetryWaitTime(min time.Duration) *Client {
c.resty.SetRetryWaitTime(min)
func (c *Client) SetRetryWaitTime(minWaitTime time.Duration) *Client {
c.resty.SetRetryWaitTime(minWaitTime)
return c
}

Expand Down