Simplify process_connection_writes condition

And comment explaining that we there's something more precise but more
complex available.
This commit is contained in:
2025-09-10 16:45:04 -04:00
parent f56ed2bfbe
commit 962a010724
3 changed files with 16 additions and 15 deletions

View File

@@ -162,7 +162,7 @@ void Server::receiveConnectionBack(std::unique_ptr<Connection> connection) {
// Re-add the connection to epoll for continued processing
struct epoll_event event{};
if (!connection->hasMessages()) {
if (!connection->has_messages()) {
event.events = EPOLLIN | EPOLLONESHOT;
} else {
event.events = EPOLLOUT | EPOLLONESHOT;
@@ -482,12 +482,13 @@ void Server::process_connection_reads(std::unique_ptr<Connection> &conn,
}
void Server::process_connection_writes(std::unique_ptr<Connection> &conn,
int events) {
int /*events*/) {
assert(conn);
// Send immediately if we have outgoing messages (either from EPOLLOUT or
// after reading)
if ((events & EPOLLOUT) || ((events & EPOLLIN) && conn->hasMessages())) {
bool had_messages = conn->hasMessages();
// For simplicity, we always attempt to write when an event fires. We could be
// more precise and skip the write if we detect that we've already seen EAGAIN
// on this connection and we don't have EPOLLOUT.
if (conn->has_messages()) {
bool had_messages = conn->has_messages();
bool error = conn->writeBytes();
if (error) {
@@ -504,7 +505,7 @@ void Server::process_connection_writes(std::unique_ptr<Connection> &conn,
}
// Check if buffer became empty (transition from non-empty -> empty)
if (had_messages && !conn->hasMessages()) {
if (had_messages && !conn->has_messages()) {
handler_.on_write_buffer_drained(conn);
// If handler took ownership (conn is now null), return
if (!conn) {
@@ -513,7 +514,7 @@ void Server::process_connection_writes(std::unique_ptr<Connection> &conn,
}
// Check if we should close the connection according to application
if (!conn->hasMessages() && conn->shouldClose()) {
if (!conn->has_messages() && conn->should_close()) {
conn.reset(); // Connection should be closed
return;
}
@@ -547,7 +548,7 @@ void Server::process_connection_batch(
int fd = conn_ptr->getFd();
struct epoll_event event{};
if (!conn_ptr->hasMessages()) {
if (!conn_ptr->has_messages()) {
event.events = EPOLLIN | EPOLLONESHOT;
} else {
event.events = EPOLLOUT | EPOLLONESHOT;