feat(messaging): implement P3 S-MESSAGING-01..04 Messaging queue, StorageQueue/OutboxInbox, and MessagingRpc adapter
This commit is contained in:
1 parent
aaeab50758
commit
60ee28e4ca
17 files changed
+4641
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,3 @@
|
||||
void_add_module(Messaging
|
||||
INTERNAL_DEPS Core Functional Binary Time Async
|
||||
)
|
||||
@@ -0,0 +1,513 @@
|
||||
#include <Void/MessagingRpc/RpcAdapter.hpp>
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::MessagingRpc
|
||||
{
|
||||
using namespace Void::Messaging;
|
||||
using Void::Messaging::Result;
|
||||
|
||||
namespace
|
||||
{
|
||||
void put_u8(Void::RPC::OwnedBytes& out, std::uint8_t v)
|
||||
{
|
||||
out.push_back(static_cast<std::byte>(v));
|
||||
}
|
||||
|
||||
void put_u32(Void::RPC::OwnedBytes& out, std::uint32_t v)
|
||||
{
|
||||
out.push_back(static_cast<std::byte>((v >> 24U) & 0xFFU));
|
||||
out.push_back(static_cast<std::byte>((v >> 16U) & 0xFFU));
|
||||
out.push_back(static_cast<std::byte>((v >> 8U) & 0xFFU));
|
||||
out.push_back(static_cast<std::byte>(v & 0xFFU));
|
||||
}
|
||||
|
||||
void put_u64(Void::RPC::OwnedBytes& out, std::uint64_t v)
|
||||
{
|
||||
for (int shift = 56; shift >= 0; shift -= 8) {
|
||||
out.push_back(static_cast<std::byte>((v >> shift) & 0xFFU));
|
||||
}
|
||||
}
|
||||
|
||||
void put_i64(Void::RPC::OwnedBytes& out, std::int64_t v)
|
||||
{
|
||||
put_u64(out, static_cast<std::uint64_t>(v));
|
||||
}
|
||||
|
||||
void put_str(Void::RPC::OwnedBytes& out, std::string_view s)
|
||||
{
|
||||
put_u32(out, static_cast<std::uint32_t>(s.size()));
|
||||
for (char c : s) {
|
||||
out.push_back(static_cast<std::byte>(c));
|
||||
}
|
||||
}
|
||||
|
||||
void put_bytes(Void::RPC::OwnedBytes& out,
|
||||
const std::vector<std::byte>& b)
|
||||
{
|
||||
put_u32(out, static_cast<std::uint32_t>(b.size()));
|
||||
out.insert(out.end(), b.begin(), b.end());
|
||||
}
|
||||
|
||||
void put_message(Void::RPC::OwnedBytes& out, const Message& m)
|
||||
{
|
||||
put_str(out, m.id);
|
||||
put_u32(out, m.schema_version);
|
||||
put_bytes(out, m.payload);
|
||||
put_u32(out, static_cast<std::uint32_t>(m.headers.size()));
|
||||
for (const auto& [k, v] : m.headers) {
|
||||
put_str(out, k);
|
||||
put_str(out, v);
|
||||
}
|
||||
}
|
||||
|
||||
void put_receipt(Void::RPC::OwnedBytes& out, const Receipt& r)
|
||||
{
|
||||
put_str(out, r.queue);
|
||||
put_str(out, r.group);
|
||||
put_str(out, r.message_id);
|
||||
put_u64(out, r.sequence);
|
||||
put_u64(out, r.generation);
|
||||
}
|
||||
|
||||
struct Cursor {
|
||||
std::span<const std::byte> data{};
|
||||
std::size_t pos{0};
|
||||
|
||||
[[nodiscard]] bool read_u8(std::uint8_t& v) noexcept
|
||||
{
|
||||
if (pos + 1U > data.size()) return false;
|
||||
v = std::to_integer<std::uint8_t>(data[pos++]);
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool read_u32(std::uint32_t& v) noexcept
|
||||
{
|
||||
if (pos + 4U > data.size()) return false;
|
||||
v = (static_cast<std::uint32_t>(
|
||||
std::to_integer<std::uint8_t>(data[pos]))
|
||||
<< 24U) |
|
||||
(static_cast<std::uint32_t>(
|
||||
std::to_integer<std::uint8_t>(data[pos + 1]))
|
||||
<< 16U) |
|
||||
(static_cast<std::uint32_t>(
|
||||
std::to_integer<std::uint8_t>(data[pos + 2]))
|
||||
<< 8U) |
|
||||
static_cast<std::uint32_t>(
|
||||
std::to_integer<std::uint8_t>(data[pos + 3]));
|
||||
pos += 4U;
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool read_u64(std::uint64_t& v) noexcept
|
||||
{
|
||||
if (pos + 8U > data.size()) return false;
|
||||
v = 0;
|
||||
for (std::size_t i = 0; i < 8U; ++i) {
|
||||
v = (v << 8U) |
|
||||
static_cast<std::uint64_t>(
|
||||
std::to_integer<std::uint8_t>(data[pos + i]));
|
||||
}
|
||||
pos += 8U;
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool read_i64(std::int64_t& v) noexcept
|
||||
{
|
||||
std::uint64_t u = 0;
|
||||
if (!read_u64(u)) return false;
|
||||
v = static_cast<std::int64_t>(u);
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool read_str(std::string& s)
|
||||
{
|
||||
std::uint32_t len = 0;
|
||||
if (!read_u32(len) || pos + len > data.size()) return false;
|
||||
s.resize(len);
|
||||
if (len > 0U) {
|
||||
std::memcpy(s.data(), data.data() + pos, len);
|
||||
}
|
||||
pos += len;
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool read_bytes(std::vector<std::byte>& b)
|
||||
{
|
||||
std::uint32_t len = 0;
|
||||
if (!read_u32(len) || pos + len > data.size()) return false;
|
||||
b.resize(len);
|
||||
if (len > 0U) {
|
||||
std::memcpy(b.data(), data.data() + pos, len);
|
||||
}
|
||||
pos += len;
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool read_message(Message& m)
|
||||
{
|
||||
std::uint32_t hcount = 0;
|
||||
if (!read_str(m.id) || !read_u32(m.schema_version) ||
|
||||
!read_bytes(m.payload) || !read_u32(hcount)) {
|
||||
return false;
|
||||
}
|
||||
m.headers.clear();
|
||||
for (std::uint32_t i = 0; i < hcount; ++i) {
|
||||
std::string k, v;
|
||||
if (!read_str(k) || !read_str(v)) return false;
|
||||
m.headers.emplace(std::move(k), std::move(v));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool read_receipt(Receipt& r)
|
||||
{
|
||||
return read_str(r.queue) && read_str(r.group) &&
|
||||
read_str(r.message_id) && read_u64(r.sequence) &&
|
||||
read_u64(r.generation);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool eof() const noexcept
|
||||
{
|
||||
return pos == data.size();
|
||||
}
|
||||
};
|
||||
|
||||
[[nodiscard]] Void::Result::Error
|
||||
map_rpc_err(const Void::Result::Error& err)
|
||||
{
|
||||
if (err.code().domain == Void::Result::kMessagingErrorDomain) {
|
||||
return err;
|
||||
}
|
||||
const auto status = Void::RPC::rpc_status_from_error(err);
|
||||
if (status == Void::RPC::RpcStatusCode::UnknownOutcome) {
|
||||
return make_messaging_error(MessagingErrorCode::UnknownOutcome,
|
||||
err.message());
|
||||
}
|
||||
if (status == Void::RPC::RpcStatusCode::FailedPrecondition) {
|
||||
return make_messaging_error(MessagingErrorCode::StaleReceipt,
|
||||
err.message());
|
||||
}
|
||||
if (status == Void::RPC::RpcStatusCode::NotFound) {
|
||||
return make_messaging_error(MessagingErrorCode::NotFound,
|
||||
err.message());
|
||||
}
|
||||
if (status == Void::RPC::RpcStatusCode::ResourceExhausted) {
|
||||
return make_messaging_error(MessagingErrorCode::QuotaExceeded,
|
||||
err.message());
|
||||
}
|
||||
if (status == Void::RPC::RpcStatusCode::DeadlineExceeded) {
|
||||
return make_messaging_error(MessagingErrorCode::Timeout,
|
||||
err.message());
|
||||
}
|
||||
if (status == Void::RPC::RpcStatusCode::Cancelled) {
|
||||
return make_messaging_error(MessagingErrorCode::Cancelled,
|
||||
err.message());
|
||||
}
|
||||
return make_messaging_error(MessagingErrorCode::PersistenceError,
|
||||
err.message());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
RpcQueueServer::~RpcQueueServer() noexcept { unmount_and_drain(); }
|
||||
|
||||
void RpcQueueServer::unmount_and_drain() noexcept
|
||||
{
|
||||
for (auto& reg : m_registrations) {
|
||||
reg.unregister_and_drain();
|
||||
}
|
||||
m_registrations.clear();
|
||||
}
|
||||
|
||||
Result<void> RpcQueueServer::mount(Void::RPC::Server& server, Queue& queue)
|
||||
{
|
||||
auto bind_one = [&](Void::RPC::MethodId id, std::string name,
|
||||
Void::RPC::UnaryHandler handler) -> Result<void> {
|
||||
auto reg = server.bind(
|
||||
Void::RPC::MethodDescriptor{
|
||||
.id = id,
|
||||
.service_name = "void.messaging.v1",
|
||||
.method_name = std::move(name),
|
||||
.schema_version = 1,
|
||||
},
|
||||
std::move(handler));
|
||||
if (!reg.has_value()) {
|
||||
return Void::Result::fail(reg.error());
|
||||
}
|
||||
m_registrations.push_back(std::move(*reg));
|
||||
return {};
|
||||
};
|
||||
|
||||
if (auto r = bind_one(
|
||||
kPublishMethod, "publish",
|
||||
[&queue](const Void::RPC::CallContext& ctx,
|
||||
Void::RPC::OwnedBytes req)
|
||||
-> Void::Result::Result<Void::RPC::OwnedBytes> {
|
||||
Cursor c{req, 0};
|
||||
std::string qname;
|
||||
std::uint8_t dur = 0;
|
||||
Message msg{};
|
||||
if (!c.read_str(qname) || !c.read_u8(dur) ||
|
||||
!c.read_message(msg) || !c.eof()) {
|
||||
return Void::Result::fail(Void::RPC::make_rpc_error(
|
||||
Void::RPC::RpcStatusCode::InvalidArgument,
|
||||
"MessagingRpc: malformed publish request"));
|
||||
}
|
||||
Void::Async::OperationOptions opts{.stop = ctx.stop};
|
||||
auto res = queue.publish(
|
||||
qname, std::move(msg),
|
||||
static_cast<PublishDurability>(dur), opts);
|
||||
if (!res.has_value()) {
|
||||
return Void::Result::fail(res.error());
|
||||
}
|
||||
Void::RPC::OwnedBytes out;
|
||||
put_str(out, res->queue);
|
||||
put_str(out, res->message_id);
|
||||
put_u64(out, res->sequence);
|
||||
put_u8(out, static_cast<std::uint8_t>(res->durability));
|
||||
put_u8(out, res->deduplicated ? 1U : 0U);
|
||||
return out;
|
||||
});
|
||||
!r.has_value()) {
|
||||
return r;
|
||||
}
|
||||
|
||||
if (auto r = bind_one(
|
||||
kTryReceiveMethod, "try_receive",
|
||||
[&queue](const Void::RPC::CallContext&,
|
||||
Void::RPC::OwnedBytes req)
|
||||
-> Void::Result::Result<Void::RPC::OwnedBytes> {
|
||||
Cursor c{req, 0};
|
||||
std::string qname, gname;
|
||||
if (!c.read_str(qname) || !c.read_str(gname) || !c.eof()) {
|
||||
return Void::Result::fail(Void::RPC::make_rpc_error(
|
||||
Void::RPC::RpcStatusCode::InvalidArgument,
|
||||
"MessagingRpc: malformed try_receive request"));
|
||||
}
|
||||
auto res = queue.try_receive(qname, gname);
|
||||
if (!res.has_value()) {
|
||||
return Void::Result::fail(res.error());
|
||||
}
|
||||
Void::RPC::OwnedBytes out;
|
||||
if (!res->has_value()) {
|
||||
put_u8(out, 0U);
|
||||
return out;
|
||||
}
|
||||
put_u8(out, 1U);
|
||||
put_message(out, (*res)->message);
|
||||
put_receipt(out, (*res)->receipt);
|
||||
put_u32(out, (*res)->attempt);
|
||||
put_i64(out, to_mono_ns((*res)->visibility_deadline));
|
||||
return out;
|
||||
});
|
||||
!r.has_value()) {
|
||||
return r;
|
||||
}
|
||||
|
||||
if (auto r = bind_one(
|
||||
kAckMethod, "ack",
|
||||
[&queue](const Void::RPC::CallContext& ctx,
|
||||
Void::RPC::OwnedBytes req)
|
||||
-> Void::Result::Result<Void::RPC::OwnedBytes> {
|
||||
Cursor c{req, 0};
|
||||
Receipt rec{};
|
||||
if (!c.read_receipt(rec) || !c.eof()) {
|
||||
return Void::Result::fail(Void::RPC::make_rpc_error(
|
||||
Void::RPC::RpcStatusCode::InvalidArgument,
|
||||
"MessagingRpc: malformed ack request"));
|
||||
}
|
||||
auto res =
|
||||
queue.ack(rec, Void::Async::OperationOptions{.stop = ctx.stop});
|
||||
if (!res.has_value()) {
|
||||
return Void::Result::fail(res.error());
|
||||
}
|
||||
return Void::RPC::OwnedBytes{};
|
||||
});
|
||||
!r.has_value()) {
|
||||
return r;
|
||||
}
|
||||
|
||||
if (auto r = bind_one(
|
||||
kNackMethod, "nack",
|
||||
[&queue](const Void::RPC::CallContext& ctx,
|
||||
Void::RPC::OwnedBytes req)
|
||||
-> Void::Result::Result<Void::RPC::OwnedBytes> {
|
||||
Cursor c{req, 0};
|
||||
Receipt rec{};
|
||||
std::uint8_t has_delay = 0;
|
||||
std::int64_t delay_ms = 0;
|
||||
if (!c.read_receipt(rec) || !c.read_u8(has_delay) ||
|
||||
!c.read_i64(delay_ms) || !c.eof()) {
|
||||
return Void::Result::fail(Void::RPC::make_rpc_error(
|
||||
Void::RPC::RpcStatusCode::InvalidArgument,
|
||||
"MessagingRpc: malformed nack request"));
|
||||
}
|
||||
std::optional<std::chrono::milliseconds> d{};
|
||||
if (has_delay != 0U) {
|
||||
d = std::chrono::milliseconds{delay_ms};
|
||||
}
|
||||
auto res = queue.nack(
|
||||
rec, d, Void::Async::OperationOptions{.stop = ctx.stop});
|
||||
if (!res.has_value()) {
|
||||
return Void::Result::fail(res.error());
|
||||
}
|
||||
return Void::RPC::OwnedBytes{};
|
||||
});
|
||||
!r.has_value()) {
|
||||
return r;
|
||||
}
|
||||
|
||||
if (auto r = bind_one(
|
||||
kQueryReceiptMethod, "query_receipt",
|
||||
[&queue](const Void::RPC::CallContext&,
|
||||
Void::RPC::OwnedBytes req)
|
||||
-> Void::Result::Result<Void::RPC::OwnedBytes> {
|
||||
Cursor c{req, 0};
|
||||
Receipt rec{};
|
||||
if (!c.read_receipt(rec) || !c.eof()) {
|
||||
return Void::Result::fail(Void::RPC::make_rpc_error(
|
||||
Void::RPC::RpcStatusCode::InvalidArgument,
|
||||
"MessagingRpc: malformed query_receipt request"));
|
||||
}
|
||||
auto res = queue.query_receipt(rec);
|
||||
if (!res.has_value()) {
|
||||
return Void::Result::fail(res.error());
|
||||
}
|
||||
Void::RPC::OwnedBytes out;
|
||||
put_u8(out, static_cast<std::uint8_t>(*res));
|
||||
return out;
|
||||
});
|
||||
!r.has_value()) {
|
||||
return r;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
// ── RpcQueueClient ────────────────────────────────────────────────────────
|
||||
|
||||
RpcQueueClient::RpcQueueClient(Void::RPC::ClientOptions options)
|
||||
: m_client(std::move(options))
|
||||
{
|
||||
}
|
||||
|
||||
Result<PublishReceipt> RpcQueueClient::publish(
|
||||
std::string_view queue, const Message& message,
|
||||
PublishDurability durability, const Void::RPC::CallOptions& options)
|
||||
{
|
||||
Void::RPC::OwnedBytes req;
|
||||
put_str(req, queue);
|
||||
put_u8(req, static_cast<std::uint8_t>(durability));
|
||||
put_message(req, message);
|
||||
|
||||
auto resp = m_client.call(kPublishMethod, std::move(req), options);
|
||||
if (!resp.has_value()) {
|
||||
return Void::Result::fail(map_rpc_err(resp.error()));
|
||||
}
|
||||
Cursor c{*resp, 0};
|
||||
PublishReceipt pr{};
|
||||
std::uint8_t dur = 0, dedup = 0;
|
||||
if (!c.read_str(pr.queue) || !c.read_str(pr.message_id) ||
|
||||
!c.read_u64(pr.sequence) || !c.read_u8(dur) || !c.read_u8(dedup) ||
|
||||
!c.eof()) {
|
||||
return Void::Result::fail(make_messaging_error(
|
||||
MessagingErrorCode::CorruptPayload,
|
||||
"MessagingRpc: corrupt publish response"));
|
||||
}
|
||||
pr.durability = static_cast<PublishDurability>(dur);
|
||||
pr.deduplicated = (dedup != 0U);
|
||||
return pr;
|
||||
}
|
||||
|
||||
Result<std::optional<Delivery>>
|
||||
RpcQueueClient::try_receive(std::string_view queue,
|
||||
std::string_view group,
|
||||
const Void::RPC::CallOptions& options)
|
||||
{
|
||||
Void::RPC::OwnedBytes req;
|
||||
put_str(req, queue);
|
||||
put_str(req, group);
|
||||
|
||||
auto resp = m_client.call(kTryReceiveMethod, std::move(req), options);
|
||||
if (!resp.has_value()) {
|
||||
return Void::Result::fail(map_rpc_err(resp.error()));
|
||||
}
|
||||
Cursor c{*resp, 0};
|
||||
std::uint8_t present = 0;
|
||||
if (!c.read_u8(present)) {
|
||||
return Void::Result::fail(make_messaging_error(
|
||||
MessagingErrorCode::CorruptPayload,
|
||||
"MessagingRpc: corrupt try_receive response"));
|
||||
}
|
||||
if (present == 0U) {
|
||||
return std::optional<Delivery>{std::nullopt};
|
||||
}
|
||||
Delivery d{};
|
||||
std::int64_t dl_ns = 0;
|
||||
if (!c.read_message(d.message) || !c.read_receipt(d.receipt) ||
|
||||
!c.read_u32(d.attempt) || !c.read_i64(dl_ns) || !c.eof()) {
|
||||
return Void::Result::fail(make_messaging_error(
|
||||
MessagingErrorCode::CorruptPayload,
|
||||
"MessagingRpc: corrupt delivery response"));
|
||||
}
|
||||
d.visibility_deadline = Void::Time::MonotonicClock::time_point{
|
||||
std::chrono::duration_cast<
|
||||
Void::Time::MonotonicClock::time_point::duration>(
|
||||
std::chrono::nanoseconds{dl_ns})};
|
||||
return std::optional<Delivery>{std::move(d)};
|
||||
}
|
||||
|
||||
Result<void> RpcQueueClient::ack(const Receipt& receipt,
|
||||
const Void::RPC::CallOptions& options)
|
||||
{
|
||||
Void::RPC::OwnedBytes req;
|
||||
put_receipt(req, receipt);
|
||||
auto resp = m_client.call(kAckMethod, std::move(req), options);
|
||||
if (!resp.has_value()) {
|
||||
return Void::Result::fail(map_rpc_err(resp.error()));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void>
|
||||
RpcQueueClient::nack(const Receipt& receipt,
|
||||
std::optional<std::chrono::milliseconds> delay,
|
||||
const Void::RPC::CallOptions& options)
|
||||
{
|
||||
Void::RPC::OwnedBytes req;
|
||||
put_receipt(req, receipt);
|
||||
put_u8(req, delay.has_value() ? 1U : 0U);
|
||||
put_i64(req, delay.has_value() ? delay->count() : 0LL);
|
||||
auto resp = m_client.call(kNackMethod, std::move(req), options);
|
||||
if (!resp.has_value()) {
|
||||
return Void::Result::fail(map_rpc_err(resp.error()));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<DeliveryState>
|
||||
RpcQueueClient::query_receipt(const Receipt& receipt,
|
||||
const Void::RPC::CallOptions& options)
|
||||
{
|
||||
Void::RPC::OwnedBytes req;
|
||||
put_receipt(req, receipt);
|
||||
auto resp = m_client.call(kQueryReceiptMethod, std::move(req), options);
|
||||
if (!resp.has_value()) {
|
||||
return Void::Result::fail(map_rpc_err(resp.error()));
|
||||
}
|
||||
Cursor c{*resp, 0};
|
||||
std::uint8_t st = 0;
|
||||
if (!c.read_u8(st) || !c.eof() ||
|
||||
st > static_cast<std::uint8_t>(DeliveryState::DeadLetter)) {
|
||||
return Void::Result::fail(make_messaging_error(
|
||||
MessagingErrorCode::CorruptPayload,
|
||||
"MessagingRpc: corrupt query_receipt response"));
|
||||
}
|
||||
return static_cast<DeliveryState>(st);
|
||||
}
|
||||
|
||||
} // namespace Void::MessagingRpc
|
||||
@@ -0,0 +1,3 @@
|
||||
void_add_module(MessagingRpc
|
||||
INTERNAL_DEPS Messaging RPC
|
||||
)
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,3 @@
|
||||
void_add_module(MessagingStorage
|
||||
INTERNAL_DEPS Messaging Storage
|
||||
)
|
||||
Reference in new issue
Block a user