backoffcoord

package
v0.135.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package backoffcoord is the fleet client for go-fleet-backoff-coordinator (ADR-0013). Where ratecoord coordinates per-host token budgets, backoffcoord coordinates per-host *post- failure* sleep so a shared upstream that started 5xx-ing doesn't get hammered by every fleet caller's local retry.

safehttp's WithBackoffCoordinator option already speaks this protocol, but it's hard-wired inside the transport — services that want to consult the coordinator from non-HTTP code paths (e.g. a queue worker re-trying a job, a DNS resolver wrapper) have no canonical client to call. This package extracts the wire protocol into a reusable shape with an Observer for fleet metrics.

Design centre: fail-open, cheap, observable. Consult never blocks beyond the connect+read budget. On any error the response is "wait 0" so callers can proceed; the Observer fires "unreachable" so fleet metrics show the coordinator is degraded.

Package backoffcoord provides a distributed backoff coordinator client. Before retrying a failed upstream call, services consult the coordinator to avoid thundering-herd pile-ons when the upstream recovers.

Index

Constants

View Source
const (
	DefaultURL = "https://backoff-coordinator.0exec.com"
)

Variables

View Source
var ErrNoHost = errors.New("backoffcoord: host required")

ErrNoHost is returned by Consult when host is empty.

Functions

func SetDefaultObserver

func SetDefaultObserver(o Observer)

SetDefaultObserver installs a process-wide observer.

Types

type Client

type Client struct {
	BaseURL string
	HTTP    *http.Client

	// MaxWait caps how long any single Consult call will block on the
	// coordinator's advice. Defaults to 5s; values <= 0 use the default.
	MaxWait time.Duration
	// contains filtered or unexported fields
}

Client talks to a backoff-coordinator instance. Construct one per process and reuse — no per-call allocation beyond the request body.

func New

func New() *Client

New constructs a client from env vars. Safe at package init.

BACKOFF_COORDINATOR_URL  default https://backoff-coordinator.0exec.com
BACKOFF_COORDINATOR_MAX_WAIT_MS  optional; default 5000

func (*Client) Consult

func (c *Client) Consult(ctx context.Context, host string, fail Failure) (*Result, error)

Consult asks the coordinator how long to sleep before retrying a failed call to host. Never blocks beyond MaxWait + the connect+read budget.

Returns nil error on success (advice obtained or coordinator unreachable — both are non-fatal). Returns ctx.Err() if the parent context cancels first.

func (*Client) SetObserver

func (c *Client) SetObserver(o Observer) *Client

SetObserver attaches an Observer to the client. Idempotent.

func (*Client) Sleep

func (c *Client) Sleep(ctx context.Context, host string, fail Failure) (*Result, error)

Sleep is a convenience wrapper that calls Consult and then sleeps the recommended duration (bounded by ctx). Useful for the common "if recently-failed, then back off" pattern outside HTTP transports.

type Event

type Event struct {
	Host           string
	Outcome        string
	ConsultLatency time.Duration
	Waited         time.Duration
}

Event is the per-Consult payload handed to an Observer. Outcome is one of: "no_wait", "waited", "unreachable".

type Failure

type Failure struct {
	Status        int
	RetryAfterSec int
	At            time.Time
}

Failure describes the most recent bad response from the host the caller is about to retry. status==0 means "network error / timeout"; retryAfterSec is the integer-seconds form of the upstream's Retry- After header (0 if absent).

type Observer

type Observer interface {
	ObserveBackoffCoord(Event)
}

Observer receives one event per Consult call. Implementations MUST NOT block.

func DefaultObserver

func DefaultObserver() Observer

DefaultObserver returns the current process-wide observer.

type Result

type Result struct {
	Wait     time.Duration
	FellOpen bool
	Reason   string
}

Result is what Consult returns. Wait is what the caller should sleep before retrying. FellOpen is true when the coordinator was unreachable and we returned wait=0 to keep the caller moving.

Jump to

Keyboard shortcuts

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