Expand description
Proactive cost-throttle management for GraphQL APIs Proactive GraphQL cost-throttle management.
LiveBudget tracks the rolling point budget advertised by APIs that
implement the Shopify / Jobber-style cost-throttle extension envelope:
{ "extensions": { "cost": {
"requestedQueryCost": 12,
"actualQueryCost": 12,
"throttleStatus": {
"maximumAvailable": 10000.0,
"currentlyAvailable": 9988.0,
"restoreRate": 500.0
}
}}}Before each request a proactive pre-flight delay is computed: if the
projected available budget (accounting for elapsed restore time and
in-flight reservations) will be too low, the caller sleeps until it
recovers. After the delay, pre_flight_reserve atomically reserves an
estimated cost against the budget so concurrent callers immediately see a
reduced balance.
§BudgetGuard (RAII)
BudgetGuard wraps pre_flight_reserve + release_reservation into a
scope-based guard so callers no longer need to track every exit path
manually. Call BudgetGuard::acquire before the request and
BudgetGuard::release on the success path. If release() is never
called (e.g. early return or ? propagation), the Drop impl spawns a
background task to release the reservation as a safety net.
use stygian_graph::adapters::graphql_throttle::{
CostThrottleConfig, PluginBudget, BudgetGuard,
};
let budget = PluginBudget::new(CostThrottleConfig::default());
let guard = BudgetGuard::acquire(&budget).await;
// ... send request ...
guard.release().await; // explicit async release on success path
// If this line is never reached, Drop releases via tokio::spawn.Re-exports§
pub use crate::ports::graphql_plugin::CostThrottleConfig;
Structs§
- Budget
Guard - RAII guard that automatically releases a budget reservation on drop.
- Live
Budget - Mutable runtime state tracking the current point budget.
- Plugin
Budget - A shareable, cheaply-cloneable handle to a per-plugin
LiveBudget.
Functions§
- pre_
flight_ reserve - Sleep if the projected budget is too low, then atomically reserve an estimated cost for the upcoming request.
- reactive_
backoff_ ms - Compute the reactive back-off delay from a throttle response body.
- release_
reservation - Release a reservation made by
pre_flight_reserve. - update_
budget - Update the
PluginBudgetfrom a completed response body.