backoff

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: GPL-3.0 Imports: 3 Imported by: 0

README

server/backoff

Import path: github.com/InsideGallery/core/server/backoff

backoff wraps an http.RoundTripper with retry behavior for outbound HTTP requests.

Main APIs

  • PoliticType: retry policy enum. Supported values are NoBackoff, ExponentialBackoff, and ConstantBackoff.
  • HTTPTransport: an http.RoundTripper implementation that retries failed transport calls and HTTP responses with status code >= 400.
  • NewTransport(delay, retries, backoff, tripper): creates a retrying transport. Non-positive delay and retry values fall back to package defaults.
  • SetupClientBackoff(client, delay, retries, backoff): replaces client.Transport with a retrying transport.

Usage

client := &http.Client{}
backoff.SetupClientBackoff(client, 250*time.Millisecond, 3, backoff.ExponentialBackoff)

resp, err := client.Get("https://api.example.test/resource")

If the wrapped transport is nil, HTTPTransport uses http.DefaultTransport. Constant backoff sleeps the configured delay between attempts; exponential backoff doubles the delay up to DefaultMaxInterval.

Operational Notes

Each retry sets req.Close = true, so callers should expect connections to be closed between attempts. The transport serializes RoundTrip calls with a mutex.

Documentation

Index

Constants

View Source
const (
	NoBackoff PoliticType = iota
	ExponentialBackoff
	ConstantBackoff

	DefaultRetries                = 1
	DefaultMaxInterval            = 60 * time.Second
	DefaultExponentialMultiplayer = 2
	DefaultDelay                  = 250 * time.Millisecond
)

Variables

This section is empty.

Functions

func SetupClientBackoff

func SetupClientBackoff(client *http.Client, delay time.Duration, retries int, backoff PoliticType)

Types

type HTTPTransport

type HTTPTransport struct {
	http.RoundTripper
	// contains filtered or unexported fields
}

func NewTransport

func NewTransport(delay time.Duration, retries int, backoff PoliticType, tripper http.RoundTripper) *HTTPTransport

func (*HTTPTransport) RoundTrip

func (s *HTTPTransport) RoundTrip(req *http.Request) (resp *http.Response, err error)

type PoliticType

type PoliticType int

Jump to

Keyboard shortcuts

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