{"record":{"id":"cccd7d0b7842ddd1","repo":"nautechsystems/nautilus_trader","slug":"window-index-fits-u32","errorCode":null,"errorMessage":"window index fits u32","messagePattern":"window index fits u32","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/derive/src/common/rate_limit.rs","lineNumber":431,"sourceCode":"            )\n            .as_str(),\n        ),\n        RateBucket::CancelAll => Ustr::from(DERIVE_CANCEL_ALL_RATE_KEY),\n        RateBucket::CancelByLabel => Ustr::from(DERIVE_CANCEL_BY_LABEL_RATE_KEY),\n    }\n}\n\nfn resolve_tps(configured: Option<u32>, default_tps: u32) -> u32 {\n    configured.filter(|&v| v > 0).unwrap_or(default_tps)\n}\n\nfn window_limit(tps: u32) -> NonZeroU32 {\n    NonZeroU32::new(tps.saturating_mul(DERIVE_RATE_BURST_MULTIPLIER))\n        .expect(\"window limit must be non-zero\")\n}\n\nfn window_index(elapsed_nanos: u64) -> u32 {\n    u32::try_from(elapsed_nanos / RATE_WINDOW_NANOS).expect(\"window index fits u32\")\n}\n\n/// Packs `(window index, consumed)` into one atomic word; the window index in\n/// the high half so the default zero value reads as a stale window.\nfn pack(window: u32, consumed: u32) -> u64 {\n    (u64::from(window) << 32) | u64::from(consumed)\n}\n\nfn unpack(packed: u64) -> (u32, u32) {\n    (\n        u32::try_from(packed >> 32).expect(\"window index fits u32\"),\n        packed as u32,\n    )\n}\n\n/// Outcome of a one-cell consumption attempt against a fixed `window`.\nenum CellOutcome {\n    Consumed,","sourceCodeStart":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/derive/src/common/rate_limit.rs#L413-L449","documentation":"`window_index` converts elapsed-nanos-divided-by-window-size into a u32 window counter and panics via `try_from` if it exceeds u32::MAX. Given `RATE_WINDOW_NANOS` (a large constant), uptime would need to exceed roughly 1,36 years of continuous operation for a single counter to overflow — it is an overflow guard, not an expected path.","triggerScenarios":"Elapsed time since limiter start exceeding ~2^32 * RATE_WINDOW_NANOS nanoseconds, or a caller passing a bogus `elapsed_nanos` (e.g. a clock computed from a wrong epoch or monotonic-clock mismatch).","commonSituations":"A process that never restarts across years, or a bug where elapsed time is computed from wall-clock since UNIX epoch instead of process start (instantly huge values).","solutions":["Compute elapsed time from a monotonic process-start instant, not from the wall clock / UNIX epoch.","Restart the process periodically (typical for long-running traders) to reset the counter.","Verify RATE_WINDOW_NANOS is unchanged; shrinking the window raises the overflow risk."],"exampleFix":"// before\nlet elapsed = SystemTime::now().duration_since(UNIX_EPOCH)?.as_nanos() as u64;\n// after\nlet elapsed = start_instant.elapsed().as_nanos() as u64;","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Base elapsed time on a monotonic Instant from process start, not wall clock.","Restart long-running feeds periodically.","Don't shrink RATE_WINDOW_NANOS without auditing the counter width."],"tags":["rust","panic","overflow","rate-limit"],"backgroundTag":"value-out-of-range","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}