493 lines
13 KiB
C++
493 lines
13 KiB
C++
#include <Void/Net/Pool.hpp>
|
|
|
|
#include <deque>
|
|
#include <map>
|
|
#include <mutex>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace Void::Net
|
|
{
|
|
struct ConnectionPool::State {
|
|
struct IdleEntry {
|
|
std::unique_ptr<PooledConnection> conn;
|
|
Void::Time::MonotonicClock::time_point idle_since;
|
|
};
|
|
|
|
PoolLimits limits{};
|
|
const Void::Time::MonotonicClock* clock{nullptr};
|
|
|
|
mutable std::mutex mutex{};
|
|
bool shut_down{false};
|
|
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};
|
|
std::map<PoolKey, std::size_t> per_origin_count{};
|
|
std::map<PoolKey, std::deque<IdleEntry>> idle_by_key{};
|
|
|
|
void evict_expired_locked(
|
|
Void::Time::MonotonicClock::time_point now,
|
|
std::vector<std::unique_ptr<PooledConnection>>& to_close)
|
|
{
|
|
for (auto it = idle_by_key.begin(); it != idle_by_key.end();) {
|
|
auto& dq = it->second;
|
|
while (!dq.empty()) {
|
|
const bool expired =
|
|
limits.idle_timeout >
|
|
std::chrono::milliseconds::zero() &&
|
|
(now - dq.front().idle_since) >=
|
|
limits.idle_timeout;
|
|
const bool closed =
|
|
!dq.front().conn || !dq.front().conn->is_open();
|
|
if (!expired && !closed) {
|
|
break;
|
|
}
|
|
to_close.push_back(std::move(dq.front().conn));
|
|
dq.pop_front();
|
|
--idle_connections;
|
|
--total_connections;
|
|
++idle_evictions;
|
|
auto oit = per_origin_count.find(it->first);
|
|
if (oit != per_origin_count.end()) {
|
|
if (--oit->second == 0) {
|
|
per_origin_count.erase(oit);
|
|
}
|
|
}
|
|
}
|
|
if (dq.empty()) {
|
|
it = idle_by_key.erase(it);
|
|
} else {
|
|
++it;
|
|
}
|
|
}
|
|
}
|
|
|
|
void evict_oldest_idle_locked(
|
|
std::vector<std::unique_ptr<PooledConnection>>& to_close)
|
|
{
|
|
auto oldest_it = idle_by_key.end();
|
|
for (auto it = idle_by_key.begin(); it != idle_by_key.end();
|
|
++it) {
|
|
if (it->second.empty()) {
|
|
continue;
|
|
}
|
|
if (oldest_it == idle_by_key.end() ||
|
|
it->second.front().idle_since <
|
|
oldest_it->second.front().idle_since) {
|
|
oldest_it = it;
|
|
}
|
|
}
|
|
if (oldest_it == idle_by_key.end()) {
|
|
return;
|
|
}
|
|
to_close.push_back(std::move(oldest_it->second.front().conn));
|
|
oldest_it->second.pop_front();
|
|
--idle_connections;
|
|
--total_connections;
|
|
++idle_evictions;
|
|
auto oit = per_origin_count.find(oldest_it->first);
|
|
if (oit != per_origin_count.end()) {
|
|
if (--oit->second == 0) {
|
|
per_origin_count.erase(oit);
|
|
}
|
|
}
|
|
if (oldest_it->second.empty()) {
|
|
idle_by_key.erase(oldest_it);
|
|
}
|
|
}
|
|
|
|
void return_lease(const PoolKey& key,
|
|
std::unique_ptr<PooledConnection> conn,
|
|
bool reusable) noexcept
|
|
{
|
|
std::vector<std::unique_ptr<PooledConnection>> to_close;
|
|
{
|
|
std::lock_guard lock{mutex};
|
|
const auto now = clock->now();
|
|
if (active_leases > 0) {
|
|
--active_leases;
|
|
}
|
|
evict_expired_locked(now, to_close);
|
|
|
|
const bool can_keep = !shut_down && reusable &&
|
|
conn != nullptr && conn->is_open() &&
|
|
limits.max_idle_connections > 0;
|
|
if (can_keep) {
|
|
while (idle_connections >=
|
|
limits.max_idle_connections) {
|
|
evict_oldest_idle_locked(to_close);
|
|
}
|
|
idle_by_key[key].push_back(
|
|
IdleEntry{std::move(conn), now});
|
|
++idle_connections;
|
|
} else {
|
|
if (conn != nullptr) {
|
|
to_close.push_back(std::move(conn));
|
|
}
|
|
if (!reusable) {
|
|
++closed_dirty;
|
|
}
|
|
if (total_connections > 0) {
|
|
--total_connections;
|
|
}
|
|
auto oit = per_origin_count.find(key);
|
|
if (oit != per_origin_count.end()) {
|
|
if (--oit->second == 0) {
|
|
per_origin_count.erase(oit);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for (auto& c : to_close) {
|
|
if (c) {
|
|
c->close();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
// ── ConnectionLease ─────────────────────────────────────────────────────
|
|
|
|
ConnectionLease::~ConnectionLease() noexcept { release(); }
|
|
|
|
ConnectionLease::ConnectionLease(ConnectionLease&& other) noexcept
|
|
: m_pool_state(std::move(other.m_pool_state)),
|
|
m_key(std::move(other.m_key)), m_conn(std::move(other.m_conn)),
|
|
m_reused(std::exchange(other.m_reused, false)),
|
|
m_reusable(std::exchange(other.m_reusable, false)),
|
|
m_active(std::exchange(other.m_active, false))
|
|
{
|
|
}
|
|
|
|
ConnectionLease& ConnectionLease::operator=(ConnectionLease&& other) noexcept
|
|
{
|
|
if (this != &other) {
|
|
release();
|
|
m_pool_state = std::move(other.m_pool_state);
|
|
m_key = std::move(other.m_key);
|
|
m_conn = std::move(other.m_conn);
|
|
m_reused = std::exchange(other.m_reused, false);
|
|
m_reusable = std::exchange(other.m_reusable, false);
|
|
m_active = std::exchange(other.m_active, false);
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
bool ConnectionLease::valid() const noexcept
|
|
{
|
|
return m_active && m_conn != nullptr;
|
|
}
|
|
|
|
bool ConnectionLease::reused() const noexcept { return m_reused; }
|
|
|
|
const PoolKey& ConnectionLease::key() const noexcept { return m_key; }
|
|
|
|
PooledConnection* ConnectionLease::get() const noexcept
|
|
{
|
|
return m_conn.get();
|
|
}
|
|
|
|
PooledConnection& ConnectionLease::operator*() const noexcept
|
|
{
|
|
return *m_conn;
|
|
}
|
|
|
|
PooledConnection* ConnectionLease::operator->() const noexcept
|
|
{
|
|
return m_conn.get();
|
|
}
|
|
|
|
void ConnectionLease::mark_reusable(bool reusable) noexcept
|
|
{
|
|
m_reusable = reusable;
|
|
}
|
|
|
|
void ConnectionLease::release() noexcept
|
|
{
|
|
if (!m_active) {
|
|
return;
|
|
}
|
|
m_active = false;
|
|
auto state = std::static_pointer_cast<ConnectionPool::State>(
|
|
std::move(m_pool_state));
|
|
auto conn = std::move(m_conn);
|
|
const bool reusable = std::exchange(m_reusable, false);
|
|
if (state) {
|
|
state->return_lease(m_key, std::move(conn), reusable);
|
|
} else if (conn) {
|
|
conn->close();
|
|
}
|
|
}
|
|
|
|
// ── ConnectionPool ──────────────────────────────────────────────────────
|
|
|
|
ConnectionPool::ConnectionPool(PoolLimits limits,
|
|
const Void::Time::MonotonicClock& clock)
|
|
: m_state(std::make_shared<State>())
|
|
{
|
|
m_state->limits = limits;
|
|
m_state->clock = &clock;
|
|
}
|
|
|
|
ConnectionPool::~ConnectionPool() noexcept { shutdown(); }
|
|
ConnectionPool::ConnectionPool(ConnectionPool&&) noexcept = default;
|
|
ConnectionPool&
|
|
ConnectionPool::operator=(ConnectionPool&&) noexcept = default;
|
|
|
|
Void::Result::Result<ConnectionLease>
|
|
ConnectionPool::acquire(const PoolKey& key,
|
|
const ConnectionFactory& factory)
|
|
{
|
|
if (!m_state) {
|
|
return std::unexpected{make_net_error(
|
|
NetErrorCode::invalid_argument,
|
|
"Net::ConnectionPool: moved-from pool")};
|
|
}
|
|
if (!factory) {
|
|
return std::unexpected{make_net_error(
|
|
NetErrorCode::invalid_argument,
|
|
"Net::ConnectionPool: empty connection factory")};
|
|
}
|
|
if (key.host.empty() || key.port == 0) {
|
|
return std::unexpected{make_net_error(
|
|
NetErrorCode::invalid_argument,
|
|
"Net::ConnectionPool: invalid pool key")};
|
|
}
|
|
|
|
std::vector<std::unique_ptr<PooledConnection>> to_close;
|
|
std::unique_ptr<PooledConnection> reused_conn;
|
|
{
|
|
std::lock_guard lock{m_state->mutex};
|
|
if (m_state->shut_down) {
|
|
return std::unexpected{make_net_error(
|
|
NetErrorCode::cancelled,
|
|
"Net::ConnectionPool: pool is shut down")};
|
|
}
|
|
const auto now = m_state->clock->now();
|
|
m_state->evict_expired_locked(now, to_close);
|
|
|
|
// Try LIFO idle connection for this exact key.
|
|
auto it = m_state->idle_by_key.find(key);
|
|
if (it != m_state->idle_by_key.end()) {
|
|
while (!it->second.empty()) {
|
|
auto cand = std::move(it->second.back().conn);
|
|
it->second.pop_back();
|
|
--m_state->idle_connections;
|
|
if (cand && cand->is_open()) {
|
|
reused_conn = std::move(cand);
|
|
++m_state->active_leases;
|
|
++m_state->total_acquires;
|
|
++m_state->idle_hits;
|
|
break;
|
|
}
|
|
to_close.push_back(std::move(cand));
|
|
--m_state->total_connections;
|
|
++m_state->idle_evictions;
|
|
auto oit = m_state->per_origin_count.find(key);
|
|
if (oit != m_state->per_origin_count.end()) {
|
|
if (--oit->second == 0) {
|
|
m_state->per_origin_count.erase(oit);
|
|
}
|
|
}
|
|
}
|
|
if (it->second.empty()) {
|
|
m_state->idle_by_key.erase(it);
|
|
}
|
|
}
|
|
|
|
if (!reused_conn) {
|
|
// Enforce per-origin and total connection capacity.
|
|
const std::size_t cur_origin =
|
|
m_state->per_origin_count[key];
|
|
if (cur_origin >= m_state->limits.max_per_origin) {
|
|
return std::unexpected{Void::Result::Error{
|
|
Void::Result::ErrorKind::overloaded,
|
|
"Net::ConnectionPool: per-origin connection limit "
|
|
"reached",
|
|
Void::Result::ErrorCode{
|
|
Void::Result::kNetErrorDomain,
|
|
static_cast<std::int32_t>(
|
|
NetErrorCode::quota_exceeded)}}};
|
|
}
|
|
if (m_state->total_connections >=
|
|
m_state->limits.max_total_connections) {
|
|
// Evict an idle connection from another origin if possible.
|
|
if (m_state->idle_connections > 0) {
|
|
m_state->evict_oldest_idle_locked(to_close);
|
|
}
|
|
}
|
|
if (m_state->total_connections >=
|
|
m_state->limits.max_total_connections) {
|
|
return std::unexpected{Void::Result::Error{
|
|
Void::Result::ErrorKind::overloaded,
|
|
"Net::ConnectionPool: total connection limit reached",
|
|
Void::Result::ErrorCode{
|
|
Void::Result::kNetErrorDomain,
|
|
static_cast<std::int32_t>(
|
|
NetErrorCode::quota_exceeded)}}};
|
|
}
|
|
// Reserve slot before calling factory outside lock.
|
|
++m_state->total_connections;
|
|
++m_state->active_leases;
|
|
++m_state->per_origin_count[key];
|
|
++m_state->total_acquires;
|
|
}
|
|
}
|
|
|
|
for (auto& c : to_close) {
|
|
if (c) {
|
|
c->close();
|
|
}
|
|
}
|
|
|
|
if (reused_conn) {
|
|
ConnectionLease lease;
|
|
lease.m_pool_state = m_state;
|
|
lease.m_key = key;
|
|
lease.m_conn = std::move(reused_conn);
|
|
lease.m_reused = true;
|
|
lease.m_reusable = false;
|
|
lease.m_active = true;
|
|
return lease;
|
|
}
|
|
|
|
// Create new connection outside pool lock.
|
|
auto created = factory(key);
|
|
if (!created.has_value() || !(*created) || !(*created)->is_open()) {
|
|
// Roll back reservation.
|
|
std::lock_guard lock{m_state->mutex};
|
|
if (m_state->active_leases > 0) {
|
|
--m_state->active_leases;
|
|
}
|
|
if (m_state->total_connections > 0) {
|
|
--m_state->total_connections;
|
|
}
|
|
auto oit = m_state->per_origin_count.find(key);
|
|
if (oit != m_state->per_origin_count.end()) {
|
|
if (--oit->second == 0) {
|
|
m_state->per_origin_count.erase(oit);
|
|
}
|
|
}
|
|
if (!created.has_value()) {
|
|
return std::unexpected{std::move(created.error())};
|
|
}
|
|
return std::unexpected{make_net_error(
|
|
NetErrorCode::connect_failed,
|
|
"Net::ConnectionPool: factory returned closed/null "
|
|
"connection")};
|
|
}
|
|
|
|
ConnectionLease lease;
|
|
lease.m_pool_state = m_state;
|
|
lease.m_key = key;
|
|
lease.m_conn = std::move(*created);
|
|
lease.m_reused = false;
|
|
lease.m_reusable = false;
|
|
lease.m_active = true;
|
|
return lease;
|
|
}
|
|
|
|
std::size_t ConnectionPool::evict_expired()
|
|
{
|
|
if (!m_state) {
|
|
return 0;
|
|
}
|
|
std::vector<std::unique_ptr<PooledConnection>> to_close;
|
|
{
|
|
std::lock_guard lock{m_state->mutex};
|
|
m_state->evict_expired_locked(m_state->clock->now(), to_close);
|
|
}
|
|
for (auto& c : to_close) {
|
|
if (c) {
|
|
c->close();
|
|
}
|
|
}
|
|
return to_close.size();
|
|
}
|
|
|
|
std::size_t ConnectionPool::clear_idle()
|
|
{
|
|
if (!m_state) {
|
|
return 0;
|
|
}
|
|
std::vector<std::unique_ptr<PooledConnection>> to_close;
|
|
{
|
|
std::lock_guard lock{m_state->mutex};
|
|
for (auto& [key, dq] : m_state->idle_by_key) {
|
|
for (auto& entry : dq) {
|
|
to_close.push_back(std::move(entry.conn));
|
|
--m_state->idle_connections;
|
|
--m_state->total_connections;
|
|
++m_state->idle_evictions;
|
|
auto oit = m_state->per_origin_count.find(key);
|
|
if (oit != m_state->per_origin_count.end()) {
|
|
if (--oit->second == 0) {
|
|
m_state->per_origin_count.erase(oit);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
m_state->idle_by_key.clear();
|
|
}
|
|
for (auto& c : to_close) {
|
|
if (c) {
|
|
c->close();
|
|
}
|
|
}
|
|
return to_close.size();
|
|
}
|
|
|
|
void ConnectionPool::shutdown() noexcept
|
|
{
|
|
if (!m_state) {
|
|
return;
|
|
}
|
|
std::vector<std::unique_ptr<PooledConnection>> to_close;
|
|
{
|
|
std::lock_guard lock{m_state->mutex};
|
|
m_state->shut_down = true;
|
|
for (auto& [key, dq] : m_state->idle_by_key) {
|
|
for (auto& entry : dq) {
|
|
to_close.push_back(std::move(entry.conn));
|
|
--m_state->idle_connections;
|
|
--m_state->total_connections;
|
|
auto oit = m_state->per_origin_count.find(key);
|
|
if (oit != m_state->per_origin_count.end()) {
|
|
if (--oit->second == 0) {
|
|
m_state->per_origin_count.erase(oit);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
m_state->idle_by_key.clear();
|
|
}
|
|
for (auto& c : to_close) {
|
|
if (c) {
|
|
c->close();
|
|
}
|
|
}
|
|
}
|
|
|
|
PoolStats ConnectionPool::stats() const
|
|
{
|
|
if (!m_state) {
|
|
return {};
|
|
}
|
|
std::lock_guard lock{m_state->mutex};
|
|
return PoolStats{
|
|
.total_connections = m_state->total_connections,
|
|
.active_leases = m_state->active_leases,
|
|
.idle_connections = m_state->idle_connections,
|
|
.total_acquires = m_state->total_acquires,
|
|
.idle_hits = m_state->idle_hits,
|
|
.idle_evictions = m_state->idle_evictions,
|
|
.closed_dirty = m_state->closed_dirty,
|
|
};
|
|
}
|
|
|
|
} // namespace Void::Net
|