#include "log_gate.hpp" #include namespace burstlog { namespace { enum class Event { put, tick, config, erase }; Result settle(Gate& g, std::uint64_t time) { Result r{}; const auto age = time - g.start; if (age >= g.interval) { const auto aligned = time - age % g.interval; r.windows = (aligned - g.start) / g.interval; r.flushed = g.pending; g.start = aligned; g.reported = saturating_add(g.reported, g.pending); g.closed = saturating_add(g.closed, r.windows); g.used = g.pending = 0; } g.last = time; return r; } void install(Gate& g, std::uint64_t time, const Config& c) { g = Gate{}; g.ready = 1; g.start = g.last = time; g.interval = c.interval; g.burst = c.burst; } int transition(Event event, Gate* g, std::uint64_t time, Config c, std::uint64_t count, Result* out) { if (!g || !out || g->ready != 1 || !valid_time(time) || time < g->last) { return -1; } if (event == Event::put && count > max_burst) return -1; if (event == Event::config && !valid_config(c)) return -1; if (event == Event::erase) { install(*g, time, Config{g->interval, g->burst}); *out = Result{}; return 0; } auto r = settle(*g, time); switch (event) { case Event::put: { const auto free = g->burst - g->used; const auto denied = count > free ? count - free : 0; r.allowed = count - denied; r.blocked = denied; g->requests = saturating_add(g->requests, count); g->emitted = saturating_add(g->emitted, r.allowed); g->suppressed = saturating_add(g->suppressed, denied); g->used += r.allowed; g->pending = saturating_add(g->pending, denied); break; } case Event::config: r.flushed = saturating_add(r.flushed, g->pending); g->reported = saturating_add(g->reported, g->pending); g->used = g->pending = 0; g->start = time; g->interval = c.interval; g->burst = c.burst; break; case Event::tick: case Event::erase: break; } *out = r; return 0; } } int gate_init(Gate* g, std::uint64_t now, const Config& c, Result* out) { if (!g || !out || !valid_time(now) || !valid_config(c)) return -1; install(*g, now, c); *out = Result{}; return 0; } int gate_log(Gate* g, std::uint64_t now, std::uint64_t count, Result* out) { return transition(Event::put, g, now, {}, count, out); } int gate_advance(Gate* g, std::uint64_t now, Result* out) { return transition(Event::tick, g, now, {}, 0, out); } int gate_configure(Gate* g, std::uint64_t now, const Config& c, Result* out) { return transition(Event::config, g, now, c, 0, out); } int gate_reset(Gate* g, std::uint64_t now, Result* out) { return transition(Event::erase, g, now, {}, 0, out); } int gate_snapshot(const Gate* g, Snapshot* out) { if (!g || !out) return -1; *out = {g->ready,g->start,g->last,g->interval,g->burst,g->used,g->pending, g->requests,g->emitted,g->suppressed,g->closed,g->reported}; return 0; } int execute(const Command& c, std::array* gates, Result* out) { if (!gates || !out || c.object > 1) return -1; Gate* g = &gates->at(c.object); if (c.kind == Kind::init) return gate_init(g,c.time,c.config,out); if (c.kind == Kind::log) return gate_log(g,c.time,c.count,out); if (c.kind == Kind::advance) return gate_advance(g,c.time,out); if (c.kind == Kind::configure) return gate_configure(g,c.time,c.config,out); if (c.kind == Kind::reset) return gate_reset(g,c.time,out); if (c.kind == Kind::snapshot) { *out = {}; return 0; } return -1; } }