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 ¶
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 ¶
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 ¶
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.