util

package
v1.26.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RetryClient

type RetryClient struct {
	Client      *http.Client
	MaxRetries  int
	BaseBackoff time.Duration
	MaxBackoff  time.Duration
	Limiter     *rate.Limiter // controls QPS and Burst
}

RetryClient wraps an http.Client to add automatic retry on 429 and 5xx errors.

func NewRetryClient

func NewRetryClient(cli *http.Client) *RetryClient

NewRetryClient returns a RetryClient configured with retry and rate limiting settings sourced from environment variables.

Environment variables (with defaults):

  • CLIENT_MAX_RETRIES (int, default: 5) Maximum number of retries before giving up.

  • CLIENT_BASE_BACKOFF (duration, default: 500ms) Initial backoff duration for retries. The backoff grows exponentially.

  • CLIENT_MAX_BACKOFF (duration, default: 10s) Maximum cap for the exponential backoff duration.

  • CLIENT_QPS (float, default: 30.0) Average sustained queries per second allowed by the rate limiter.

  • CLIENT_BURST (int, default: 45) Maximum burst of requests allowed before the limiter enforces QPS.

The returned client retries failed requests (429 and 5xx) with exponential backoff and jitter, and enforces request rate limiting using a token bucket.

Example usage:

cli := &http.Client{Timeout: 30 * time.Second}
retryCli := NewRetryClient(cli)
resp, err := retryCli.Do(req)

func (*RetryClient) Do

func (rc *RetryClient) Do(req *http.Request) (*http.Response, error)

Do executes the request with retry logic only for idempotent methods.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL