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

+68
View File
@@ -0,0 +1,68 @@
#include <Void/RPC/Observations.hpp>
#include <utility>
namespace Void::RPC
{
RpcObservations::RpcObservations(RpcObservationLimits limits)
: m_limits(limits)
{
}
void RpcObservations::record(const RpcObservation& event) noexcept
{
std::lock_guard lock{m_mutex};
++m_recorded;
if (event.kind == RpcObservationKind::BytesReceived ||
event.kind == RpcObservationKind::BytesSent) {
m_bytes += event.bytes;
}
if (m_events.size() >= m_limits.max_events) {
++m_dropped;
return;
}
m_events.push_back(event);
}
std::vector<RpcObservation> RpcObservations::drain() noexcept
{
std::lock_guard lock{m_mutex};
std::vector<RpcObservation> out;
out.reserve(m_events.size());
for (const auto& event : m_events) {
out.push_back(event);
}
m_events.clear();
return out;
}
std::size_t RpcObservations::capacity() const noexcept
{
std::lock_guard lock{m_mutex};
return m_limits.max_events;
}
std::size_t RpcObservations::size() const noexcept
{
std::lock_guard lock{m_mutex};
return m_events.size();
}
std::size_t RpcObservations::dropped() const noexcept
{
std::lock_guard lock{m_mutex};
return m_dropped;
}
std::uint64_t RpcObservations::total_recorded() const noexcept
{
std::lock_guard lock{m_mutex};
return m_recorded;
}
std::uint64_t RpcObservations::total_bytes() const noexcept
{
std::lock_guard lock{m_mutex};
return m_bytes;
}
} // namespace Void::RPC