The service aggregates active session metrics and, on request, publishes a snapshot into a text buffer for an external consumer. After a recent PR, two problems started to appear: when preparing the string fails, part of the internal state is sometimes already changed, and under high contention publication noticeably slows down parallel updates. A review of the changes is needed to find the causes.
Stack and constraints:
std::thread and Arc<Mutex<_>>.SessionStore::record(session_id, delta) increases the active session counter by delta.SessionStore::publish_into(writer) writes one line per session in the format <id>:<value>\n only for positive values, in the insertion order in entries.writer fails, published state must not change.What counts as reproduction:
SessionStore, call several records.publish_into a writer that returns an error after the first successful write_all.published field did not change.publish_into with a slow writer, and in parallel call record; the update must not wait for all writer operations to finish.Assumptions:
entries stores (session_id, value) pairs and may contain several different sessions.Rust 1.85.0 (stable), cargo test / cargo run, edition 2021. Any OS with the Rust standard library available. No external crates.
Use line comments to explain the problem, its impact, the fix and how to test it.