RPC: bounded observation sink for hosts (S-RPC-05)

This commit is contained in:
faumaray committed 2026-12-03 19:58:20 +03:00
1 parent 3f71bfdd4a
commit dec48426ae
3 files changed
+232

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
#pragma once
#include <Void/RPC/Types.hpp>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <mutex>
#include <vector>
namespace Void::RPC
{
/// Capacity bounds for one observation sink.
struct RpcObservationLimits {
/// Fixed ring size. Memory never exceeds this many events, no
/// matter how fast the producers are or how slow the consumer is.
std::size_t max_events{1024};
};
enum class RpcObservationKind : std::uint8_t {
CallStarted,
CallFinished,
StreamOpened,
StreamClosed,
BytesReceived,
BytesSent,
Error
};
struct RpcObservation {
RpcObservationKind kind{RpcObservationKind::CallStarted};
RpcStatusCode status{RpcStatusCode::Ok};
std::size_t bytes{0};
std::chrono::milliseconds duration{0};
};
/// Bounded, thread-safe observation sink for RPC hosts.
///
/// Producers never block: when the fixed ring is full the event is
/// dropped and counted, so a stalled or absent collector cannot grow
/// memory. `drain()` hands the retained events to a consumer.
class RpcObservations
{
public:
explicit RpcObservations(RpcObservationLimits limits = {});
void record(const RpcObservation& event) noexcept;
/// Remove and return all retained events in arrival order.
[[nodiscard]] std::vector<RpcObservation> drain() noexcept;
[[nodiscard]] std::size_t capacity() const noexcept;
[[nodiscard]] std::size_t size() const noexcept;
/// Events discarded because the ring was full (cumulative).
[[nodiscard]] std::size_t dropped() const noexcept;
/// Every event ever recorded, including dropped ones.
[[nodiscard]] std::uint64_t total_recorded() const noexcept;
/// Sum of `bytes` across all byte observations (cumulative).
[[nodiscard]] std::uint64_t total_bytes() const noexcept;
private:
mutable std::mutex m_mutex;
RpcObservationLimits m_limits;
std::deque<RpcObservation> m_events;
std::size_t m_dropped{0};
std::uint64_t m_recorded{0};
std::uint64_t m_bytes{0};
};
} // namespace Void::RPC