181 lines
5.9 KiB
C++
181 lines
5.9 KiB
C++
#pragma once
|
|
|
|
#include <Void/Messaging/Types.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::Messaging
|
|
{
|
|
/// Abstract queue interface implemented by both explicitly nondurable
|
|
/// `InMemoryQueue` and durable `MessagingStorage::StorageQueue`.
|
|
class Queue
|
|
{
|
|
public:
|
|
|
|
virtual ~Queue() = default;
|
|
|
|
[[nodiscard]] virtual Result<void>
|
|
create_queue(QueueConfig config) = 0;
|
|
|
|
[[nodiscard]] virtual Result<void>
|
|
create_group(std::string_view queue, GroupConfig config) = 0;
|
|
|
|
[[nodiscard]] virtual Result<PublishReceipt> publish(
|
|
std::string_view queue, Message message,
|
|
PublishDurability durability,
|
|
Void::Async::OperationOptions options = {}) = 0;
|
|
|
|
[[nodiscard]] virtual Result<std::optional<Delivery>>
|
|
try_receive(std::string_view queue, std::string_view group) = 0;
|
|
|
|
[[nodiscard]] virtual Result<Delivery> receive(
|
|
std::string_view queue, std::string_view group,
|
|
Void::Async::OperationOptions options = {}) = 0;
|
|
|
|
[[nodiscard]] virtual Result<void>
|
|
ack(const Receipt& receipt,
|
|
Void::Async::OperationOptions options = {}) = 0;
|
|
|
|
[[nodiscard]] virtual Result<void> nack(
|
|
const Receipt& receipt,
|
|
std::optional<std::chrono::milliseconds> delay = std::nullopt,
|
|
Void::Async::OperationOptions options = {}) = 0;
|
|
|
|
[[nodiscard]] virtual Result<Void::Time::MonotonicClock::time_point>
|
|
extend_visibility(
|
|
const Receipt& receipt, std::chrono::milliseconds extension,
|
|
Void::Async::OperationOptions options = {}) = 0;
|
|
|
|
/// Quarantine an unparseable or schema-incompatible message into
|
|
/// DLQ without falsely acknowledging it as processed.
|
|
[[nodiscard]] virtual Result<void> quarantine_corrupt(
|
|
const Receipt& receipt, std::string reason,
|
|
Void::Async::OperationOptions options = {}) = 0;
|
|
|
|
[[nodiscard]] virtual Result<DeliveryState>
|
|
query_receipt(const Receipt& receipt) const = 0;
|
|
|
|
[[nodiscard]] virtual Result<std::vector<DeadLetterEntry>>
|
|
list_dead_letters(std::string_view queue,
|
|
std::string_view group,
|
|
std::size_t limit = 100) const = 0;
|
|
|
|
[[nodiscard]] virtual std::size_t
|
|
stuck_dlq_count(std::string_view queue,
|
|
std::string_view group) const noexcept = 0;
|
|
|
|
/// Reclaim expired `InFlight` deliveries into `Pending` (with retry
|
|
/// backoff) or atomically move exhausted messages to DLQ.
|
|
[[nodiscard]] virtual Result<std::size_t>
|
|
reap_expired_visibility() = 0;
|
|
|
|
/// Apply configured retention policy (paused when civil clock
|
|
/// health is degraded).
|
|
[[nodiscard]] virtual Result<std::size_t> expire_retention() = 0;
|
|
|
|
virtual void set_clock_healthy(bool healthy) noexcept = 0;
|
|
|
|
[[nodiscard]] virtual Result<ShutdownReport> close_and_join(
|
|
Void::Async::OperationOptions options = {}) = 0;
|
|
|
|
[[nodiscard]] virtual Result<QueueStats> stats() const = 0;
|
|
};
|
|
|
|
struct InMemoryQueueOptions {
|
|
QueueLimits limits{};
|
|
const Void::Time::MonotonicClock* monotonic_clock{
|
|
&Void::Time::SystemMonotonicClock::instance()};
|
|
const Void::Time::CivilClock* civil_clock{
|
|
&Void::Time::SystemCivilClock::instance()};
|
|
RandomU64Fn rng{};
|
|
};
|
|
|
|
/// Explicitly nondurable in-process queue implementing the full native
|
|
/// delivery state machine (`PublishDurability::Durable` is rejected with
|
|
/// `UnsupportedDurability`).
|
|
class InMemoryQueue final : public Queue
|
|
{
|
|
public:
|
|
|
|
explicit InMemoryQueue(InMemoryQueueOptions options = {});
|
|
~InMemoryQueue() noexcept override;
|
|
|
|
InMemoryQueue(InMemoryQueue&&) noexcept;
|
|
InMemoryQueue& operator=(InMemoryQueue&&) noexcept;
|
|
InMemoryQueue(const InMemoryQueue&) = delete;
|
|
InMemoryQueue& operator=(const InMemoryQueue&) = delete;
|
|
|
|
[[nodiscard]] Result<void> create_queue(QueueConfig config) override;
|
|
[[nodiscard]] Result<void>
|
|
create_group(std::string_view queue,
|
|
GroupConfig config) override;
|
|
|
|
[[nodiscard]] Result<PublishReceipt> publish(
|
|
std::string_view queue, Message message,
|
|
PublishDurability durability,
|
|
Void::Async::OperationOptions options = {}) override;
|
|
|
|
[[nodiscard]] Result<std::optional<Delivery>>
|
|
try_receive(std::string_view queue,
|
|
std::string_view group) override;
|
|
|
|
[[nodiscard]] Result<Delivery> receive(
|
|
std::string_view queue, std::string_view group,
|
|
Void::Async::OperationOptions options = {}) override;
|
|
|
|
[[nodiscard]] Result<void>
|
|
ack(const Receipt& receipt,
|
|
Void::Async::OperationOptions options = {}) override;
|
|
|
|
[[nodiscard]] Result<void> nack(
|
|
const Receipt& receipt,
|
|
std::optional<std::chrono::milliseconds> delay = std::nullopt,
|
|
Void::Async::OperationOptions options = {}) override;
|
|
|
|
[[nodiscard]] Result<Void::Time::MonotonicClock::time_point>
|
|
extend_visibility(
|
|
const Receipt& receipt, std::chrono::milliseconds extension,
|
|
Void::Async::OperationOptions options = {}) override;
|
|
|
|
[[nodiscard]] Result<void> quarantine_corrupt(
|
|
const Receipt& receipt, std::string reason,
|
|
Void::Async::OperationOptions options = {}) override;
|
|
|
|
[[nodiscard]] Result<DeliveryState>
|
|
query_receipt(const Receipt& receipt) const override;
|
|
|
|
[[nodiscard]] Result<std::vector<DeadLetterEntry>>
|
|
list_dead_letters(std::string_view queue,
|
|
std::string_view group,
|
|
std::size_t limit = 100) const override;
|
|
|
|
[[nodiscard]] std::size_t
|
|
stuck_dlq_count(std::string_view queue,
|
|
std::string_view group) const noexcept override;
|
|
|
|
[[nodiscard]] Result<std::size_t>
|
|
reap_expired_visibility() override;
|
|
|
|
[[nodiscard]] Result<std::size_t> expire_retention() override;
|
|
|
|
void set_clock_healthy(bool healthy) noexcept override;
|
|
|
|
[[nodiscard]] Result<ShutdownReport> close_and_join(
|
|
Void::Async::OperationOptions options = {}) override;
|
|
|
|
[[nodiscard]] Result<QueueStats> stats() const override;
|
|
|
|
private:
|
|
|
|
struct State;
|
|
std::shared_ptr<State> m_state;
|
|
};
|
|
|
|
} // namespace Void::Messaging
|