Simplify connection registry

This commit is contained in:
2025-08-21 19:53:18 -04:00
parent d1b1e6d589
commit 9ee23fdc46
4 changed files with 43 additions and 102 deletions

View File

@@ -30,14 +30,32 @@ Server::Server(const weaseldb::Config &config, ConnectionHandler &handler)
: config_(config), handler_(handler), connection_registry_() {}
Server::~Server() {
// CRITICAL: All I/O threads are guaranteed to be joined before the destructor
// is called because they are owned by the run() method's call frame.
// This eliminates any possibility of race conditions during connection
// cleanup.
if (shutdown_pipe_[0] != -1) {
close(shutdown_pipe_[0]);
shutdown_pipe_[0] = -1;
}
if (shutdown_pipe_[1] != -1) {
close(shutdown_pipe_[1]);
shutdown_pipe_[1] = -1;
}
// Clean up any remaining connections using proper ordering
connection_registry_.shutdown_cleanup();
cleanup_resources();
// Close all epoll instances
for (int epollfd : epoll_fds_) {
if (epollfd != -1) {
close(epollfd);
}
}
epoll_fds_.clear();
if (listen_sockfd_ != -1) {
close(listen_sockfd_);
listen_sockfd_ = -1;
}
// Clean up unix socket file if it exists
if (!config_.server.unix_socket_path.empty()) {
unlink(config_.server.unix_socket_path.c_str());
}
}
void Server::run() {
@@ -525,32 +543,3 @@ void Server::process_connection_batch(
}
}
}
void Server::cleanup_resources() {
if (shutdown_pipe_[0] != -1) {
close(shutdown_pipe_[0]);
shutdown_pipe_[0] = -1;
}
if (shutdown_pipe_[1] != -1) {
close(shutdown_pipe_[1]);
shutdown_pipe_[1] = -1;
}
// Close all epoll instances
for (int epollfd : epoll_fds_) {
if (epollfd != -1) {
close(epollfd);
}
}
epoll_fds_.clear();
if (listen_sockfd_ != -1) {
close(listen_sockfd_);
listen_sockfd_ = -1;
}
// Clean up unix socket file if it exists
if (!config_.server.unix_socket_path.empty()) {
unlink(config_.server.unix_socket_path.c_str());
}
}