Skip to content

Latest commit

 

History

History
74 lines (51 loc) · 2.41 KB

retry.md

File metadata and controls

74 lines (51 loc) · 2.41 KB

Retry Mechanism for HTTP Requests

The retry feature in the Requests library automatically attempts failed HTTP requests again based on certain conditions. This section explains how to set up and adjust these retry strategies for more dependable web interactions.

Table of Contents

  1. Enhancing Reliability with Retries
  2. Configuring Retry Strategies
  3. Customizing Retry Conditions
  4. Setting Maximum Retry Attempts

Enhancing Reliability with Retries

To set up retries in a fluent, chainable manner, you can configure your client like so:

client := requests.Create(&requests.Config{
    BaseURL: "https://api.example.com",
}).SetRetryStrategy(
    requests.ExponentialBackoffStrategy(1*time.Second, 2, 30*time.Second),
).SetRetryIf(
    requests.DefaultRetryIf,
).SetMaxRetries(3)

This setup ensures that your client is ready to handle transient failures gracefully.

Configuring Retry Strategies

Applying a Default Backoff Strategy

For consistent delay intervals between retries:

client.SetRetryStrategy(requests.DefaultBackoffStrategy(5 * time.Second))

Utilizing a Linear Backoff Strategy

To increase delay intervals linearly with each retry attempt:

client.SetRetryStrategy(requests.LinearBackoffStrategy(1 * time.Second))

Employing an Exponential Backoff Strategy

For exponential delay increases between attempts, with an option to cap the delay:

client.SetRetryStrategy(requests.ExponentialBackoffStrategy(1*time.Second, 2, 30*time.Second))

Customizing Retry Conditions

Define when retries should be attempted based on response status codes or errors:

client.SetRetryIf(func(req *http.Request, resp *http.Response, err error) bool {
    return resp.StatusCode == http.StatusInternalServerError || err != nil
})

Setting Maximum Retry Attempts

To limit the number of retries, use the SetMaxRetries method:

client.SetMaxRetries(3)

This method allows you to specify the maximum number of attempts the client should make to execute a request successfully.