164typedef enum : uint16_t {
373 uint32_t jitter_min_ms,
374 uint32_t jitter_max_ms);
434 const char* retry_after);
464 const char* retry_after,
495 uint16_t* backoff_level,
496 int64_t* earliest_next_ms);
void mdl_governor_init(mdl_governor_t *g, const mdl_gov_cfg_t *cfg, uint64_t seed)
Initialise a governor on the real host clock and blocking sleep.
bool mdl_retry_after_parse(const char *value, int64_t now_wall_s, uint32_t *out_ms)
Parse an HTTP Retry-After header value into a delay in milliseconds.
void mdl_governor_observe(mdl_governor_t *g, const char *host, long status, const char *retry_after)
Feed a completed request's outcome back into the host's governor state.
void mdl_politeness_init(mdl_politeness_t *p, uint64_t seed)
Seed the jitter source, using the real host clock for sleeps.
mdl_gov_limits_t
Fixed sizes and bounds for the governor's per-host table.
@ k_mdl_gov_ms_per_req
Milliseconds per minute (rate -> interval).
@ k_mdl_gov_host_max
Host-key buffer bytes (matches config).
@ k_mdl_gov_level_max
Backoff-exponent ceiling (overflow guard).
@ k_mdl_gov_max_hosts
Per-host record slots (origin + CDNs).
int64_t(* mdl_now_fn)(void *ctx)
Injected monotonic clock: milliseconds since an arbitrary fixed epoch.
void mdl_politeness_init_clock(mdl_politeness_t *p, uint64_t seed, mdl_sleep_fn sleep_fn, void *sleep_ctx)
Seed the jitter source and inject a clock for the blocking sleep.
mdl_gov_cfg_t mdl_gov_cfg_default(void)
Conservative default tunables for a site that configures none.
void(* mdl_sleep_fn)(void *ctx, uint32_t ms)
Injected blocking sleep: pause the caller for ms milliseconds.
ra8_err_t mdl_governor_acquire(mdl_governor_t *g, const char *host, uint32_t jitter_min_ms, uint32_t jitter_max_ms)
Reserve an in-flight slot for a request to host, pacing as required.
void mdl_governor_observe_at_wall(mdl_governor_t *g, const char *host, long status, const char *retry_after, int64_t now_wall_s)
Observe a response using an explicit wall-clock timestamp.
bool mdl_governor_peek(const mdl_governor_t *g, const char *host, uint16_t *backoff_level, int64_t *earliest_next_ms)
Read a host's current backoff level and earliest-next gate.
void mdl_governor_release(mdl_governor_t *g, const char *host)
Release the in-flight slot reserved by a matching mdl_governor_acquire.
void mdl_governor_init_clock(mdl_governor_t *g, const mdl_gov_cfg_t *cfg, uint64_t seed, mdl_now_fn now_fn, void *now_ctx, mdl_sleep_fn sleep_fn, void *sleep_ctx)
Initialise a governor with injected clock and sleep seams (DI).
uint32_t mdl_politeness_wait(mdl_politeness_t *p, uint32_t min_ms, uint32_t max_ms)
Sleep a jittered delay in [min_ms, max_ms] and return it.
Error Code Definitions for ra8-firmware.
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Per-site politeness tunables the governor is initialised with.
uint32_t burst
Token-bucket capacity, in requests (>= 1).
uint32_t rate_per_min
Sustained per-host request ceiling (0 = off).
uint16_t decay_after
Consecutive successes that drop one backoff level.
uint16_t max_inflight
Per-host in-flight request cap (>= 1).
uint32_t backoff_max_ms
Backoff-window ceiling.
uint32_t backoff_base_ms
First backoff window on a 429/503.
Closed-loop per-host politeness governor (rate + backoff + concurrency).
mdl_now_fn now_fn
Injected monotonic clock (ms).
uint64_t rng
Seeded xorshift64 jitter state.
void * now_ctx
Context for now_fn.
mdl_gov_cfg_t cfg
Politeness tunables.
void * sleep_ctx
Context for sleep_fn.
mdl_sleep_fn sleep_fn
Injected sleeper, NULL = host.
mdl_host_rec_t hosts[k_mdl_gov_max_hosts]
Per-host records.
One host's live governor state (a slot in the fixed per-host table).
uint16_t inflight
Requests currently in flight.
uint16_t success_streak
Consecutive successes since last drop.
uint16_t backoff_level
Consecutive-throttle exponent.
int64_t credit_ms
Token-bucket credit, ms of rate.
int64_t last_ms
Wall-ms of the previous scheduled start.
char host[k_mdl_gov_host_max]
Host key; "" when the slot is free.
int64_t earliest_next_ms
Backoff / Retry-After gate (mono-ms).
Deterministic jitter source plus its (optional) injected clock.
uint64_t state
PRNG state; never 0 after init.
void * sleep_ctx
Context passed to sleep_fn.
mdl_sleep_fn sleep_fn
Injected sleeper; NULL uses the host clock.