spacetraders-client 0.1.0

Async Rust client for the SpaceTraders API v2 with priority-aware rate limiting and retry
Documentation
use tokio::time::Duration;

/// Sustained request rate, in requests per second.
pub(crate) const DEFAULT_RATE_PER_SEC: f64 = 2.0;
/// Burst capacity: how many requests may fire back-to-back before the
/// sustained rate kicks in. SpaceTraders allows a short burst on top of the
/// strict ~2 req/s, so the bucket alone keeps us within budget.
pub(crate) const DEFAULT_BURST: f64 = 10.0;
/// Anti-starvation: a waiter's effective priority is bumped up one level for
/// every interval it has spent queued. A `Low` request thus overtakes freshly
/// arriving `Normal` work after ~2 intervals and `Critical` after ~3, so a
/// steady stream of higher-priority requests can never block it forever.
pub(crate) const DEFAULT_AGING_INTERVAL: Duration = Duration::from_secs(10);

/// Priority of a queued request. Higher-priority requests receive the next
/// available permit before lower-priority ones; within the same priority the
/// scheduler is first-in, first-out.
///
/// ```
/// use spacetraders_client::RequestPriority;
///
/// assert!(RequestPriority::Critical > RequestPriority::Normal);
/// assert!(RequestPriority::Normal > RequestPriority::Low);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum RequestPriority {
    /// Best-effort work that must never outrank real trade/delivery actions,
    /// e.g. market polling and idle probes.
    Low,
    /// Routine control actions: status fetches, navigation, docking, extraction.
    #[default]
    Normal,
    /// Income-critical or urgent actions: buy/sell, contract delivery,
    /// emergency refuel/repair.
    Critical,
}

impl RequestPriority {
    /// Maps an aged effective-priority level back onto a named priority.
    pub(super) fn from_level(level: u32) -> Self {
        match level {
            0 => RequestPriority::Low,
            1 => RequestPriority::Normal,
            _ => RequestPriority::Critical,
        }
    }
}