Documentation ¶
Overview ¶
Package httpretrier provides the ability to automatically repeat HTTP requests based on user-defined conditions.
Any part of the returned http.Response (e.g., StatusCode) and error can be used to define the retry function (retry condition). The default behavior is to retry only in case of an error.
This package also offers ready-made retry functions that can be used for most cases:
- RetryIfForWriteRequests - RetryIfForReadRequests
Additionally, it allows you to set the maximum number of retries, the delay after the first failed attempt, the time multiplication factor to determine the successive delay value, and the jitter used to introduce randomness and avoid request collisions.
Index ¶
Constants ¶
const ( // DefaultAttempts is the default maximum number of retry attempts. DefaultAttempts = 4 // DefaultDelay is the delay to apply after the first failed attempt. DefaultDelay = 1 * time.Second // DefaultDelayFactor is the default multiplication factor to get the successive delay value. DefaultDelayFactor = 2 // DefaultJitter is the maximum random Jitter time between retries. DefaultJitter = 100 * time.Millisecond )
Variables ¶
This section is empty.
Functions ¶
func RetryIfForReadRequests ¶
RetryIfForReadRequests is a retry check function used for read requests (e.g. GET requests that are guaranteed to not modify the remote state).
Types ¶
type HTTPClient ¶
HTTPClient contains the function to perform the actual HTTP request.
type HTTPRetrier ¶
type HTTPRetrier struct {
// contains filtered or unexported fields
}
HTTPRetrier represents an instance of the HTTP retrier.
func New ¶
func New(httpClient HTTPClient, opts ...Option) (*HTTPRetrier, error)
New creates a new instance.
type Option ¶
type Option func(c *HTTPRetrier) error
Option is the interface that allows to set the options.
func WithAttempts ¶
WithAttempts set the maximum number of retries.
func WithDelayFactor ¶
WithDelayFactor set the multiplication factor to get the successive delay value. A delay factor greater than 1 means an exponential delay increase. if the delay factor is 2 and the first delay is 1, then the delays will be: [1, 2, 4, 8, ...].
func WithJitter ¶
WithJitter sets the maximum random Jitter time between retries. This is useful to avoid the Thundering herd problem (https://en.wikipedia.org/wiki/Thundering_herd_problem).
func WithRetryIfFn ¶
WithRetryIfFn set the function used to decide when retry.