#pragma once #include #include #include #include // M10.9a overload response (ADR-0097): a region's per-tick work and tick debt decide whether // new logins are refused and how far replication is shed. Portable: no clocks (callers pass a // monotonic time in microseconds), no transport. Thresholds are engineering budgets // (schemas/network/m10-budgets-v1.md), not game rules. namespace evo::server { struct LoadPolicy { std::uint64_t tick_budget_us{25'000}; // p99 per-tick region work budget (ADR-0089). std::uint64_t refuse_us{15'000}; // Window p95 at or above: refuse new logins. std::uint64_t shed_us{20'000}; // Window p95 at or above: shed one more level. std::uint64_t relax_us{10'000}; // Window p95 below (and no debt): calm. std::uint64_t step_us{1'000'000}; // At most one shed step per interval. std::uint64_t settle_us{5'000'000}; // Calm this long relaxes one step. std::uint8_t max_shed{3}; }; inline constexpr std::size_t load_window_ticks = 40; // 2 s of 50 ms ticks. // Tick debt (due minus simulated) that refuses logins and that sheds. One tick is jitter (a // commit flush, a scheduler hiccup): it only interrupts calm. inline constexpr std::uint64_t refuse_debt_ticks = 2, shed_debt_ticks = 4; struct LoadState { bool refusing{}; std::uint8_t shed{}; std::uint64_t p95_us{}, p99_us{}, max_us{}; // Over the window (0 before any sample). std::size_t samples{}; // In the window. // The session layer's share of the same ticks (ADR-0089 budgets it per session). std::uint64_t session_p95_us{}, session_p99_us{}; friend bool operator==(const LoadState&, const LoadState&) = default; }; enum class LoadPolicyError : std::uint8_t { invalid }; class LoadGovernor { public: [[nodiscard]] static std::expected create(LoadPolicy policy); // One simulated tick's region work (of which `session_us` was the session layer's) and the // host's tick debt, at monotonic `now_us`. Returns true when `refusing` or `shed` changed. bool observe(std::uint64_t work_us, std::uint64_t session_us, std::uint64_t tick_debt, std::uint64_t now_us); [[nodiscard]] const LoadState& state() const noexcept { return state_; } [[nodiscard]] const LoadPolicy& policy() const noexcept { return policy_; } private: explicit LoadGovernor(LoadPolicy policy) : policy_{policy} {} LoadPolicy policy_; LoadState state_; std::array window_{}, session_window_{}; std::size_t next_{}; bool stepped_{}, calm_{}; std::uint64_t last_step_us_{}, calm_since_us_{}; }; } // namespace evo::server