Move activeConnections to Server

This commit is contained in:
2025-08-21 21:55:19 -04:00
parent 9ee23fdc46
commit 5e8fe590c1
8 changed files with 12 additions and 268 deletions

View File

@@ -21,7 +21,9 @@ Connection::Connection(struct sockaddr_storage addr, int fd, int64_t id,
ConnectionHandler *handler, std::weak_ptr<Server> server)
: fd_(fd), id_(id), addr_(addr), arena_(), handler_(handler),
server_(server) {
activeConnections.fetch_add(1, std::memory_order_relaxed);
if (auto server_ptr = server_.lock()) {
server_ptr->active_connections_.fetch_add(1, std::memory_order_relaxed);
}
if (handler_) {
handler_->on_connection_established(*this);
}
@@ -31,7 +33,9 @@ Connection::~Connection() {
if (handler_) {
handler_->on_connection_closed(*this);
}
activeConnections.fetch_sub(1, std::memory_order_relaxed);
if (auto server_ptr = server_.lock()) {
server_ptr->active_connections_.fetch_sub(1, std::memory_order_relaxed);
}
int e = close(fd_);
if (e == -1) {
perror("close");

View File

@@ -11,8 +11,6 @@
#include <sys/uio.h>
#include <unistd.h>
extern std::atomic<int> activeConnections;
#ifndef __has_feature
#define __has_feature(x) 0
#endif

View File

@@ -36,7 +36,9 @@ ConnectionRegistry::~ConnectionRegistry() {
for (size_t fd = 0; fd < max_fds_; ++fd) {
delete connections_[fd];
}
munmap(connections_, aligned_size_);
if (munmap(connections_, aligned_size_) == -1) {
perror("munmap");
}
}
}

View File

@@ -10,10 +10,6 @@
PERFETTO_TRACK_EVENT_STATIC_STORAGE();
// TODO this should be scoped to a particular Server, and it's definition should
// be in server.cpp or connection.cpp
std::atomic<int> activeConnections{0};
// Global server instance for signal handler access
static Server *g_server = nullptr;

View File

@@ -17,8 +17,6 @@
#include <unistd.h>
#include <vector>
extern std::atomic<int> activeConnections;
std::shared_ptr<Server> Server::create(const weaseldb::Config &config,
ConnectionHandler &handler) {
// Use std::shared_ptr constructor with private access
@@ -407,7 +405,7 @@ void Server::start_io_threads(std::vector<std::thread> &threads) {
// Check connection limit
if (config_.server.max_connections > 0 &&
activeConnections.load(std::memory_order_relaxed) >=
active_connections_.load(std::memory_order_relaxed) >=
config_.server.max_connections) {
close(fd);
continue;

View File

@@ -89,6 +89,7 @@ public:
static void releaseBackToServer(std::unique_ptr<Connection> connection);
private:
friend class Connection;
/**
* Private constructor - use create() factory method instead.
*
@@ -105,6 +106,7 @@ private:
// Connection management
std::atomic<int64_t> connection_id_{0};
std::atomic<int> active_connections_{0};
// Round-robin counter for connection distribution
std::atomic<size_t> connection_distribution_counter_{0};