143 lines
4.1 KiB
C++
143 lines
4.1 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/Net/Http.hpp>
|
|
#include <Void/Time/Clock.hpp>
|
|
|
|
#include <chrono>
|
|
#include <compare>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
namespace Void::Net
|
|
{
|
|
struct PoolLimits {
|
|
std::size_t max_total_connections{256};
|
|
std::size_t max_per_origin{8};
|
|
std::size_t max_idle_connections{128};
|
|
std::chrono::milliseconds idle_timeout{std::chrono::seconds{30}};
|
|
};
|
|
|
|
/// Pool partition key isolating origins, proxy identity, and TLS/trust
|
|
/// policy generations so connections never cross trust boundaries.
|
|
struct PoolKey {
|
|
std::string scheme{};
|
|
std::string host{};
|
|
std::uint16_t port{0};
|
|
std::string proxy_identity{};
|
|
std::uint64_t tls_policy_generation{0};
|
|
|
|
[[nodiscard]] auto
|
|
operator<=>(const PoolKey&) const noexcept = default;
|
|
};
|
|
|
|
/// Abstract pooled transport entry owned by ConnectionPool / leases.
|
|
class PooledConnection
|
|
{
|
|
public:
|
|
|
|
virtual ~PooledConnection() = default;
|
|
[[nodiscard]] virtual bool is_open() const noexcept = 0;
|
|
virtual void close() noexcept = 0;
|
|
};
|
|
|
|
using ConnectionFactory =
|
|
std::function<Void::Result::Result<std::unique_ptr<PooledConnection>>(
|
|
const PoolKey&)>;
|
|
|
|
struct PoolStats {
|
|
std::size_t total_connections{0};
|
|
std::size_t active_leases{0};
|
|
std::size_t idle_connections{0};
|
|
std::uint64_t total_acquires{0};
|
|
std::uint64_t idle_hits{0};
|
|
std::uint64_t idle_evictions{0};
|
|
std::uint64_t closed_dirty{0};
|
|
};
|
|
|
|
class ConnectionPool;
|
|
|
|
/// Move-only RAII lease for one HTTP/1.1 exchange. Only leases explicitly
|
|
/// marked reusable via `mark_reusable(true)` (after full body+trailers)
|
|
/// return to the idle pool on destruction; unmarked or dirty leases close.
|
|
class ConnectionLease final
|
|
{
|
|
public:
|
|
|
|
ConnectionLease() noexcept = default;
|
|
~ConnectionLease() noexcept;
|
|
|
|
ConnectionLease(ConnectionLease&& other) noexcept;
|
|
ConnectionLease& operator=(ConnectionLease&& other) noexcept;
|
|
ConnectionLease(const ConnectionLease&) = delete;
|
|
ConnectionLease& operator=(const ConnectionLease&) = delete;
|
|
|
|
[[nodiscard]] bool valid() const noexcept;
|
|
[[nodiscard]] bool reused() const noexcept;
|
|
[[nodiscard]] const PoolKey& key() const noexcept;
|
|
[[nodiscard]] PooledConnection* get() const noexcept;
|
|
[[nodiscard]] PooledConnection& operator*() const noexcept;
|
|
[[nodiscard]] PooledConnection* operator->() const noexcept;
|
|
|
|
void mark_reusable(bool reusable = true) noexcept;
|
|
void release() noexcept;
|
|
|
|
private:
|
|
|
|
friend class ConnectionPool;
|
|
|
|
struct Impl;
|
|
std::shared_ptr<void> m_pool_state{};
|
|
PoolKey m_key{};
|
|
std::unique_ptr<PooledConnection> m_conn{};
|
|
bool m_reused{false};
|
|
bool m_reusable{false};
|
|
bool m_active{false};
|
|
};
|
|
|
|
/// Bounded HTTP/1.1 connection pool keyed by origin, proxy identity, and
|
|
/// TLS policy generation, with deterministic clock-driven idle eviction.
|
|
class ConnectionPool final
|
|
{
|
|
public:
|
|
|
|
explicit ConnectionPool(
|
|
PoolLimits limits = {},
|
|
const Void::Time::MonotonicClock& clock =
|
|
Void::Time::SystemMonotonicClock::instance());
|
|
~ConnectionPool() noexcept;
|
|
|
|
ConnectionPool(ConnectionPool&&) noexcept;
|
|
ConnectionPool& operator=(ConnectionPool&&) noexcept;
|
|
ConnectionPool(const ConnectionPool&) = delete;
|
|
ConnectionPool& operator=(const ConnectionPool&) = delete;
|
|
|
|
/// Acquire an idle connection for `key` or invoke `factory(key)`
|
|
/// outside pool locks while enforcing total and per-origin limits.
|
|
[[nodiscard]] Void::Result::Result<ConnectionLease>
|
|
acquire(const PoolKey& key, const ConnectionFactory& factory);
|
|
|
|
/// Evict all idle connections older than `idle_timeout` or closed.
|
|
std::size_t evict_expired();
|
|
|
|
/// Close all idle connections (e.g. on trust-store rotation).
|
|
std::size_t clear_idle();
|
|
|
|
/// Prevent new leases and close all currently idle connections.
|
|
void shutdown() noexcept;
|
|
|
|
[[nodiscard]] PoolStats stats() const;
|
|
|
|
private:
|
|
|
|
friend class ConnectionLease;
|
|
struct State;
|
|
std::shared_ptr<State> m_state;
|
|
};
|
|
|
|
} // namespace Void::Net
|