Recommended epoll/tcp options
This commit is contained in:
20
src/main.cpp
20
src/main.cpp
@@ -84,6 +84,13 @@ int getListenFd(const char *node, const char *service) {
|
||||
continue; // Try next address
|
||||
}
|
||||
|
||||
// Enable TCP_NODELAY for low latency (disable Nagle's algorithm)
|
||||
if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) == -1) {
|
||||
perror("setsockopt TCP_NODELAY");
|
||||
close(sfd);
|
||||
continue; // Try next address
|
||||
}
|
||||
|
||||
// Set socket to non-blocking for graceful shutdown
|
||||
int flags = fcntl(sfd, F_GETFL, 0);
|
||||
if (flags == -1) {
|
||||
@@ -482,9 +489,10 @@ int main(int argc, char *argv[]) {
|
||||
abort();
|
||||
}
|
||||
|
||||
// Add listen socket to accept epoll
|
||||
// Add listen socket to accept epoll with EPOLLEXCLUSIVE for better load
|
||||
// balancing
|
||||
struct epoll_event listen_event;
|
||||
listen_event.events = EPOLLIN;
|
||||
listen_event.events = EPOLLIN | EPOLLEXCLUSIVE;
|
||||
listen_event.data.fd = sockfd;
|
||||
if (epoll_ctl(accept_epollfd, EPOLL_CTL_ADD, sockfd, &listen_event) == -1) {
|
||||
perror("epoll_ctl listen socket");
|
||||
@@ -545,6 +553,14 @@ int main(int argc, char *argv[]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Enable keepalive to detect dead connections
|
||||
int keepalive = 1;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive,
|
||||
sizeof(keepalive)) == -1) {
|
||||
perror("setsockopt SO_KEEPALIVE");
|
||||
// Continue anyway - not critical
|
||||
}
|
||||
|
||||
auto conn = std::make_unique<Connection>(
|
||||
addr, fd,
|
||||
connectionId.fetch_add(1, std::memory_order_relaxed));
|
||||
|
||||
Reference in New Issue
Block a user