feat: auto-flush stats timer, .env.json defaults, expose round-trip+timer tests

This commit is contained in:
2026-10-24 00:36:36 +02:00
parent 0bd9dcd584
commit 7d968fb7a0
5 changed files with 48 additions and 1 deletions
+10
View File
@@ -5,7 +5,9 @@
// atomic in-memory accumulator + a pluggable Store backend; flush is
// explicit (timer-driven by caller) inside one transaction.
#include <atomic>
#include <chrono>
#include <cstdint>
#include <thread>
#include <map>
#include <memory>
#include <mutex>
@@ -49,6 +51,12 @@ class StatsCollector {
}
bool enabled() const { return enabled_.load(std::memory_order_relaxed); }
void set_enabled(bool v) { enabled_.store(v, std::memory_order_relaxed); }
~StatsCollector() { StopAutoFlush(); }
// Time-based persisted flush: background thread calls Flush() every
// interval (default 5s; STATS_FLUSH_SECS in .env.json/env overrides).
// Idempotent; safe to call twice.
void StartAutoFlush(std::chrono::seconds interval = std::chrono::seconds(5));
void StopAutoFlush();
void Note(const std::string& method, std::uint64_t bytes_in,
std::uint64_t bytes_out, std::uint64_t latency_us);
@@ -61,6 +69,8 @@ class StatsCollector {
std::mutex mu_;
std::unique_ptr<StatsStore> store_;
std::map<std::string, MethodStat> pending_;
std::thread flush_thread_;
std::atomic<bool> flush_run_{false};
};
} // namespace molo::realtime::expose