Merge s-rpc-04: bounded RPC streaming (S-RPC-04)
This commit is contained in:
commit
a26ba70e48
5 files changed
+721
-1
No files matched your search
@@ -20,7 +20,7 @@
|
||||
|
||||
namespace Void::RPC
|
||||
{
|
||||
using OwnedBytes = std::vector<std::byte>;
|
||||
|
||||
|
||||
struct CallContext {
|
||||
CallId call_id{};
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/RPC/Types.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <stop_token>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::RPC
|
||||
{
|
||||
/// Bounds for one streaming call. The initial credit window is the hard
|
||||
/// upper bound on bytes buffered in either direction: a sender can never
|
||||
/// have more than `initial_credit_bytes` unacknowledged bytes in flight,
|
||||
/// so a stalled consumer applies backpressure instead of growing memory.
|
||||
struct StreamLimits {
|
||||
std::size_t initial_credit_bytes{256ULL * 1024};
|
||||
std::size_t max_data_frame_bytes{64ULL * 1024};
|
||||
std::size_t max_control_reserve_bytes{64ULL * 1024};
|
||||
std::size_t max_streams_per_connection{128};
|
||||
std::chrono::milliseconds default_timeout{std::chrono::seconds{30}};
|
||||
};
|
||||
|
||||
enum class StreamState : std::uint8_t {
|
||||
Open,
|
||||
HalfClosedLocal,
|
||||
HalfClosedRemote,
|
||||
Closed,
|
||||
Cancelled,
|
||||
};
|
||||
|
||||
/// Final outcome delivered by the server side of a stream.
|
||||
struct StreamTerminal {
|
||||
RpcStatus status{};
|
||||
OwnedBytes payload{};
|
||||
};
|
||||
|
||||
/// One endpoint of a duplex RPC stream: a bounded, credit-controlled
|
||||
/// byte pipe in each direction with half-close, cancellation and a single
|
||||
/// terminal outcome.
|
||||
///
|
||||
/// `write` blocks while the peer has not granted credit (the consumer is
|
||||
/// stalled); `read` blocks until data, EOF or cancellation. Every byte
|
||||
/// consumed by `read` replenishes the peer's credit. A call has exactly
|
||||
/// one outcome: the first of server `finish`, `cancel`, or transport loss
|
||||
/// wins; later attempts are rejected without changing the outcome.
|
||||
class StreamEndpoint
|
||||
{
|
||||
public:
|
||||
|
||||
StreamEndpoint() noexcept = default;
|
||||
|
||||
/// Send bytes to the peer. Blocks until credit is available.
|
||||
/// Returns `cancelled` after cancellation, `failed_precondition`
|
||||
/// when this side already half-closed, and succeeds early with
|
||||
/// a `cancelled` error if `stop` is requested.
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
write(std::span<const std::byte> bytes,
|
||||
std::stop_token stop = {});
|
||||
|
||||
/// Receive the next data chunk. Blocks until data, EOF or
|
||||
/// cancellation. An empty successful result means EOF: the peer
|
||||
/// half-closed (or sent a terminal frame) and all buffered data
|
||||
/// was consumed.
|
||||
[[nodiscard]] Void::Result::Result<OwnedBytes>
|
||||
read(std::stop_token stop = {});
|
||||
|
||||
/// No more writes from this side; the peer's `read` observes EOF
|
||||
/// after draining buffered data. Idempotent.
|
||||
void half_close() noexcept;
|
||||
|
||||
/// Abort the call for both sides. Idempotent; a no-op once a
|
||||
/// terminal outcome is committed.
|
||||
void cancel() noexcept;
|
||||
|
||||
/// Server side only: commit the terminal outcome. The client
|
||||
/// observes EOF after buffered data and receives the status from
|
||||
/// `wait_terminal`. Rejected after cancellation or a previous
|
||||
/// terminal.
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
finish(RpcStatus status, OwnedBytes payload = {});
|
||||
|
||||
/// Client side only: block until the server terminal outcome or
|
||||
/// cancellation.
|
||||
[[nodiscard]] Void::Result::Result<StreamTerminal>
|
||||
wait_terminal(std::stop_token stop = {});
|
||||
|
||||
[[nodiscard]] bool eof() const noexcept;
|
||||
[[nodiscard]] bool local_half_closed() const noexcept;
|
||||
[[nodiscard]] bool cancelled() const noexcept;
|
||||
[[nodiscard]] StreamState state() const noexcept;
|
||||
|
||||
/// Create a connected client/server endpoint pair over one bounded
|
||||
/// in-process stream.
|
||||
[[nodiscard]] static std::pair<StreamEndpoint, StreamEndpoint>
|
||||
make_pair(StreamLimits limits = {});
|
||||
|
||||
/// Bytes currently buffered in this endpoint's receive queue.
|
||||
[[nodiscard]] std::size_t buffered_bytes() const noexcept;
|
||||
/// High-water mark of the receive queue (bounded by the credit
|
||||
/// window); useful for stalled-consumer assertions.
|
||||
[[nodiscard]] std::size_t peak_buffered_bytes() const noexcept;
|
||||
/// Remaining send credit granted by the peer.
|
||||
[[nodiscard]] std::size_t credit_available() const noexcept;
|
||||
|
||||
private:
|
||||
|
||||
friend struct StreamPair;
|
||||
struct Impl;
|
||||
explicit StreamEndpoint(std::shared_ptr<Impl> impl) noexcept
|
||||
: m_impl(std::move(impl))
|
||||
{
|
||||
}
|
||||
|
||||
std::shared_ptr<Impl> m_impl;
|
||||
};
|
||||
|
||||
/// Connected client/server endpoints of one in-process duplex stream.
|
||||
struct StreamPair {
|
||||
StreamEndpoint client;
|
||||
StreamEndpoint server;
|
||||
};
|
||||
|
||||
/// Create a connected duplex stream pair with bounded credit.
|
||||
[[nodiscard]] StreamPair make_stream_pair(StreamLimits limits = {});
|
||||
|
||||
} // namespace Void::RPC
|
||||
@@ -15,6 +15,9 @@
|
||||
|
||||
namespace Void::RPC
|
||||
{
|
||||
/// Owned byte payload used by unary and streaming calls.
|
||||
using OwnedBytes = std::vector<std::byte>;
|
||||
|
||||
inline constexpr std::array<std::byte, 4> kWireMagic{
|
||||
std::byte{'V'}, std::byte{'R'}, std::byte{'P'}, std::byte{'C'}};
|
||||
inline constexpr std::uint16_t kWireMajorV1{1};
|
||||
|
||||
Reference in new issue
Block a user