ratecoord

package
v0.134.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 14 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 ratecoord provides a distributed rate-limit coordination client. Services consult the coordinator before sending requests to rate-limited upstreams, sharing a global token bucket across fleet instances.

Package ratecoord is the fleet client for go-pentest-rate-coordinator, the per-host token-bucket service. Any service that fans out HTTP requests against arbitrary upstream hosts (broken-links, subdomain- finder, iframe-analyzer's recursive walk, …) should route through this client to share a global budget instead of each service having its own private rate limit.

Graceful degradation is the design centre: the coordinator is a single-instance service and may be unreachable. Wait() never blocks forever and never fails closed by default — on coordinator outage the call falls back to a per-process token bucket so the calling service keeps making progress, and the FellBack flag on the response surfaces the degradation to /health and metrics.

Environment:

RATECOORD_URL          default https://rate-coordinator.0exec.com
RATECOORD_API_KEY      or FLEET_API_KEY (required for real coordinator;
                        unused on the in-process fallback path)
RATECOORD_DEFAULT_RPS  per-host fallback bucket rate (default 4)
RATECOORD_DEFAULT_BURST per-host fallback bucket burst (default 8)

The in-process fallback intentionally uses a different (and stricter) default than the coordinator's network policy: when coordination breaks down we want each instance to slow down on its own to avoid thundering-herd against the upstream.

Index

Constants

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

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	BaseURL string
	APIKey  string
	HTTP    *http.Client
	// contains filtered or unexported fields
}

Client talks to a rate-coordinator instance and, on failure, falls back to a per-process per-host limiter.

func New

func New() *Client

New constructs a client from environment. Safe to call at package init.

func (*Client) Probe

func (c *Client) Probe(ctx context.Context) error

Probe is a depcheck-friendly health check for the coordinator. Returns nil if the coordinator's /health endpoint returns 200 within 2 seconds.

func (*Client) SetObserver

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

SetObserver attaches an Observer to the client. Idempotent.

func (*Client) Wait

func (c *Client) Wait(ctx context.Context, host string, weight int, maxWait time.Duration) (*WaitResult, error)

Wait blocks up to maxWait for a token for host. If maxWait <= 0, defaults to 5s.

Returns nil error on success (token acquired, either remotely or via fallback). Returns context error if ctx cancels first.

type Event

type Event struct {
	Host     string
	Weight   int
	Waited   time.Duration
	Allowed  bool
	FellBack bool
	Reason   string
}

Event is the per-call payload handed to an Observer.

Allowed is true when a token was acquired (whether remotely or via the in-process fallback). FellBack records whether the local bucket served the answer because the coordinator was unreachable.

type Observer

type Observer interface {
	ObserveRate(Event)
}

Observer receives one event per Client.Wait call. Implementations MUST NOT block — callbacks run inline on the request hot path of any service fanning out HTTP traffic. The canonical implementation lives in go-common/promx.

ratecoord deliberately defines the contract here rather than importing a metrics library: ratecoord keeps zero metric-stack deps.

type WaitResult

type WaitResult struct {
	WaitedMs int64 `json:"waited_ms"`
	// FellBack is true when the coordinator was unreachable and we used
	// the in-process bucket. Surface this via depcheck / metrics so an
	// operator can spot a coordinator outage.
	FellBack bool   `json:"fell_back,omitempty"`
	Reason   string `json:"reason,omitempty"`
}

WaitResult conveys what happened during Wait.

Jump to

Keyboard shortcuts

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