Merge s-rpc-04: bounded RPC streaming (S-RPC-04)

This commit is contained in:
faumaray committed 2026-12-03 19:51:10 +03:00
commit a26ba70e48
5 files changed
+721 -1

No files matched your search

+391
View File
@@ -0,0 +1,391 @@
#include <Void/RPC/Stream.hpp>
#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <deque>
#include <memory>
#include <mutex>
#include <optional>
#include <stop_token>
#include <string>
#include <utility>
#include <vector>
namespace Void::RPC
{
namespace
{
struct SharedState {
std::mutex mutex{};
std::condition_variable_any cv{};
StreamLimits limits{};
// Data queued towards each endpoint.
std::deque<std::byte> to_client{};
std::deque<std::byte> to_server{};
bool client_closed{false};
bool server_closed{false};
bool cancelled{false};
std::optional<StreamTerminal> terminal{};
// High-water marks of each queue.
std::size_t peak_to_client{0};
std::size_t peak_to_server{0};
};
[[nodiscard]] std::deque<std::byte>&
outgoing(SharedState& state, bool client) noexcept
{
return client ? state.to_server : state.to_client;
}
[[nodiscard]] std::deque<std::byte>&
incoming(SharedState& state, bool client) noexcept
{
return client ? state.to_client : state.to_server;
}
[[nodiscard]] bool& local_closed(SharedState& state, bool client) noexcept
{
return client ? state.client_closed : state.server_closed;
}
[[nodiscard]] bool& peer_closed(SharedState& state, bool client) noexcept
{
return client ? state.server_closed : state.client_closed;
}
[[nodiscard]] std::size_t&
peak_of(SharedState& state, bool client_incoming) noexcept
{
return client_incoming ? state.peak_to_client : state.peak_to_server;
}
[[nodiscard]] std::size_t
capacity_of(const SharedState& state) noexcept
{
return state.limits.initial_credit_bytes;
}
} // namespace
struct StreamEndpoint::Impl {
std::shared_ptr<SharedState> shared{};
bool is_client{false};
};
std::pair<StreamEndpoint, StreamEndpoint>
StreamEndpoint::make_pair(StreamLimits limits)
{
if (limits.initial_credit_bytes == 0) {
limits.initial_credit_bytes = 1;
}
if (limits.max_data_frame_bytes == 0) {
limits.max_data_frame_bytes = 1;
}
auto shared = std::make_shared<SharedState>();
shared->limits = limits;
auto client = std::make_shared<Impl>();
client->shared = shared;
client->is_client = true;
auto server = std::make_shared<Impl>();
server->shared = std::move(shared);
server->is_client = false;
return {StreamEndpoint{std::move(client)},
StreamEndpoint{std::move(server)}};
}
StreamPair make_stream_pair(StreamLimits limits)
{
auto [client, server] = StreamEndpoint::make_pair(limits);
return StreamPair{std::move(client), std::move(server)};
}
Void::Result::Result<void>
StreamEndpoint::write(std::span<const std::byte> bytes,
std::stop_token stop)
{
if (!m_impl) {
return Void::Result::fail(
make_rpc_error(RpcStatusCode::Unimplemented,
"RPC stream endpoint is not connected"));
}
SharedState& state = *m_impl->shared;
const bool client = m_impl->is_client;
std::unique_lock lock{state.mutex};
if (local_closed(state, client)) {
return Void::Result::fail(
make_rpc_error(RpcStatusCode::FailedPrecondition,
"RPC stream write side is already half-closed"));
}
std::stop_callback wake{stop, [&state] { state.cv.notify_all(); }};
std::size_t written = 0;
while (written < bytes.size()) {
if (state.cancelled) {
return Void::Result::fail(
make_rpc_error(RpcStatusCode::Cancelled,
"RPC stream was cancelled"));
}
if (stop.stop_requested()) {
return Void::Result::fail(
make_rpc_error(RpcStatusCode::Cancelled,
"RPC stream write was stopped"));
}
auto& queue = outgoing(state, client);
const std::size_t capacity = capacity_of(state);
if (queue.size() < capacity) {
const std::size_t space = capacity - queue.size();
const std::size_t take =
std::min(space, bytes.size() - written);
for (std::size_t i = 0; i < take; ++i) {
queue.push_back(bytes[written + i]);
}
written += take;
peak_of(state, !client) =
std::max(peak_of(state, !client), queue.size());
state.cv.notify_all();
continue;
}
state.cv.wait(lock, [&] {
return state.cancelled || stop.stop_requested() ||
outgoing(state, client).size() < capacity;
});
}
return {};
}
Void::Result::Result<OwnedBytes>
StreamEndpoint::read(std::stop_token stop)
{
if (!m_impl) {
return Void::Result::fail(
make_rpc_error(RpcStatusCode::Unimplemented,
"RPC stream endpoint is not connected"));
}
SharedState& state = *m_impl->shared;
const bool client = m_impl->is_client;
std::unique_lock lock{state.mutex};
std::stop_callback wake{stop, [&state] { state.cv.notify_all(); }};
for (;;) {
auto& queue = incoming(state, client);
if (!queue.empty()) {
const std::size_t take = std::min(
queue.size(), state.limits.max_data_frame_bytes);
OwnedBytes out;
out.reserve(take);
for (std::size_t i = 0; i < take; ++i) {
out.push_back(queue.front());
queue.pop_front();
}
state.cv.notify_all();
return out;
}
if (state.cancelled) {
return Void::Result::fail(
make_rpc_error(RpcStatusCode::Cancelled,
"RPC stream was cancelled"));
}
// EOF: the peer half-closed or committed a terminal outcome and
// every buffered byte has been consumed.
if (peer_closed(state, client) || state.terminal.has_value()) {
return OwnedBytes{};
}
if (stop.stop_requested()) {
return Void::Result::fail(
make_rpc_error(RpcStatusCode::Cancelled,
"RPC stream read was stopped"));
}
state.cv.wait(lock, [&] {
return state.cancelled || stop.stop_requested() ||
!incoming(state, client).empty() ||
peer_closed(state, client) ||
state.terminal.has_value();
});
}
}
void StreamEndpoint::half_close() noexcept
{
if (!m_impl) {
return;
}
SharedState& state = *m_impl->shared;
std::lock_guard lock{state.mutex};
local_closed(state, m_impl->is_client) = true;
state.cv.notify_all();
}
void StreamEndpoint::cancel() noexcept
{
if (!m_impl) {
return;
}
SharedState& state = *m_impl->shared;
std::lock_guard lock{state.mutex};
if (state.terminal.has_value()) {
return;
}
state.cancelled = true;
state.cv.notify_all();
}
Void::Result::Result<void>
StreamEndpoint::finish(RpcStatus status, OwnedBytes payload)
{
if (!m_impl) {
return Void::Result::fail(
make_rpc_error(RpcStatusCode::Unimplemented,
"RPC stream endpoint is not connected"));
}
if (m_impl->is_client) {
return Void::Result::fail(
make_rpc_error(RpcStatusCode::FailedPrecondition,
"RPC stream terminal outcome is server-side only"));
}
SharedState& state = *m_impl->shared;
std::lock_guard lock{state.mutex};
if (state.cancelled) {
return Void::Result::fail(
make_rpc_error(RpcStatusCode::Cancelled,
"RPC stream was cancelled"));
}
if (state.terminal.has_value()) {
return Void::Result::fail(
make_rpc_error(RpcStatusCode::FailedPrecondition,
"RPC stream terminal outcome already committed"));
}
state.terminal.emplace(StreamTerminal{
.status = std::move(status), .payload = std::move(payload)});
state.server_closed = true;
state.cv.notify_all();
return {};
}
Void::Result::Result<StreamTerminal>
StreamEndpoint::wait_terminal(std::stop_token stop)
{
if (!m_impl) {
return Void::Result::fail(
make_rpc_error(RpcStatusCode::Unimplemented,
"RPC stream endpoint is not connected"));
}
SharedState& state = *m_impl->shared;
std::unique_lock lock{state.mutex};
std::stop_callback wake{stop, [&state] { state.cv.notify_all(); }};
for (;;) {
if (state.terminal.has_value()) {
return *state.terminal;
}
if (state.cancelled) {
return Void::Result::fail(
make_rpc_error(RpcStatusCode::Cancelled,
"RPC stream was cancelled"));
}
if (stop.stop_requested()) {
return Void::Result::fail(
make_rpc_error(RpcStatusCode::Cancelled,
"RPC stream terminal wait was stopped"));
}
state.cv.wait(lock, [&] {
return state.terminal.has_value() || state.cancelled ||
stop.stop_requested();
});
}
}
bool StreamEndpoint::eof() const noexcept
{
if (!m_impl) {
return true;
}
SharedState& state = *m_impl->shared;
std::lock_guard lock{state.mutex};
return incoming(state, m_impl->is_client).empty() &&
(peer_closed(state, m_impl->is_client) ||
state.terminal.has_value());
}
bool StreamEndpoint::local_half_closed() const noexcept
{
if (!m_impl) {
return true;
}
SharedState& state = *m_impl->shared;
std::lock_guard lock{state.mutex};
return local_closed(state, m_impl->is_client);
}
bool StreamEndpoint::cancelled() const noexcept
{
if (!m_impl) {
return false;
}
SharedState& state = *m_impl->shared;
std::lock_guard lock{state.mutex};
return state.cancelled;
}
StreamState StreamEndpoint::state() const noexcept
{
if (!m_impl) {
return StreamState::Closed;
}
SharedState& shared = *m_impl->shared;
std::lock_guard lock{shared.mutex};
if (shared.cancelled) {
return StreamState::Cancelled;
}
const bool local = local_closed(shared, m_impl->is_client);
const bool peer = peer_closed(shared, m_impl->is_client);
if (local && peer) {
return StreamState::Closed;
}
if (local) {
return StreamState::HalfClosedLocal;
}
if (peer) {
return StreamState::HalfClosedRemote;
}
return StreamState::Open;
}
std::size_t StreamEndpoint::buffered_bytes() const noexcept
{
if (!m_impl) {
return 0;
}
SharedState& state = *m_impl->shared;
std::lock_guard lock{state.mutex};
return incoming(state, m_impl->is_client).size();
}
std::size_t StreamEndpoint::peak_buffered_bytes() const noexcept
{
if (!m_impl) {
return 0;
}
SharedState& state = *m_impl->shared;
std::lock_guard lock{state.mutex};
return peak_of(state, m_impl->is_client);
}
std::size_t StreamEndpoint::credit_available() const noexcept
{
if (!m_impl) {
return 0;
}
SharedState& state = *m_impl->shared;
std::lock_guard lock{state.mutex};
const std::size_t queued = outgoing(state, m_impl->is_client).size();
const std::size_t capacity = capacity_of(state);
return queued >= capacity ? 0 : capacity - queued;
}
} // namespace Void::RPC