Add ConnectionHandler::on_post_batch

This commit is contained in:
2025-08-19 15:28:17 -04:00
parent 33fd8bb705
commit 6b7cc74a7c
3 changed files with 34 additions and 8 deletions

View File

@@ -228,10 +228,11 @@ void Server::start_network_threads() {
pthread_setname_np(pthread_self(),
("network-" + std::to_string(thread_id)).c_str());
std::vector<struct epoll_event> events(config_.server.event_batch_size);
struct epoll_event events[config_.server.event_batch_size];
std::unique_ptr<Connection> batch[config_.server.event_batch_size];
for (;;) {
int event_count = epoll_wait(network_epollfd_, events.data(),
int event_count = epoll_wait(network_epollfd_, events,
config_.server.event_batch_size, -1);
if (event_count == -1) {
if (errno == EINTR) {
@@ -241,6 +242,7 @@ void Server::start_network_threads() {
abort();
}
int batch_count = 0;
for (int i = 0; i < event_count; ++i) {
// Check for shutdown event
if (events[i].data.fd == shutdown_pipe_[0]) {
@@ -252,7 +254,6 @@ void Server::start_network_threads() {
static_cast<Connection *>(events[i].data.ptr)};
conn->tsan_acquire();
events[i].data.ptr = nullptr;
const int fd = conn->getFd();
if (events[i].events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) {
continue; // Connection closed - unique_ptr destructor cleans up
@@ -299,21 +300,31 @@ void Server::start_network_threads() {
continue;
}
}
batch[batch_count++] = std::move(conn);
}
handler_.on_post_batch({batch, (size_t)batch_count});
for (int i = 0; i < batch_count; ++i) {
auto &conn = batch[i];
if (!conn) {
continue;
}
// Determine next epoll interest
struct epoll_event event{};
if (!conn->hasMessages()) {
events[i].events = EPOLLIN | EPOLLONESHOT | EPOLLRDHUP;
event.events = EPOLLIN | EPOLLONESHOT | EPOLLRDHUP;
} else {
events[i].events = EPOLLOUT | EPOLLONESHOT | EPOLLRDHUP;
event.events = EPOLLOUT | EPOLLONESHOT | EPOLLRDHUP;
}
// Transfer ownership back to epoll
conn->tsan_release();
Connection *raw_conn = conn.release();
events[i].data.ptr = raw_conn;
event.data.ptr = raw_conn;
if (epoll_ctl(network_epollfd_, EPOLL_CTL_MOD, fd, &events[i]) ==
-1) {
if (epoll_ctl(network_epollfd_, EPOLL_CTL_MOD, raw_conn->getFd(),
&event) == -1) {
perror("epoll_ctl MOD");
delete raw_conn;
continue;