Skip to content

Commit

Permalink
🐛fix epoll fd management
Browse files Browse the repository at this point in the history
🐛fix compiler error on some platforms
  • Loading branch information
Lord-Turmoil committed Dec 2, 2024
1 parent e07437d commit 59d5397
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
33 changes: 27 additions & 6 deletions src/components/MayhemServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,37 +105,49 @@ void MayhemServer::_Serve()
// There may be multiple incoming connections.
while (AcceptSocket(_listenFd, &data))
{
if (data.SocketFd >= MAX_FD)
{
_logger->Error("Unable to handle more connections");
network::CloseSocket(data.SocketFd);
break;
}

if (!_MonitorFd(data.SocketFd))
{
_logger->Error("Failed to monitor new connection");
network::CloseSocket(data.SocketFd);
}

_handles[data.SocketFd] = CreateRef<AsyncHttpContextBuilder>(data);
}
}
else if (events[i].events & EPOLLIN)
{
auto& handle = _handles[events[i].data.fd];
int fd = events[i].data.fd;

auto& handle = _handles[fd];
MINET_ASSERT(handle);

int r = handle->Parse();
if (r == 1)
{
_UnmonitorFd(fd); // prevent from triggering again

const Ref<HttpContext>& context = handle->GetContext();
_DecorateContext(context);
if (!_threadPool.Submit([this, context] { _onConnectionCallback(context); }))
{
_logger->Warn("Server overwhelmed, new connection refused");
network::CloseSocket(data.SocketFd);
_handles[events[i].data.fd].reset();
network::CloseSocket(fd);
_handles[fd].reset();
}
}
else if (r < 0)
{
// error occurred
_logger->Error("Failed to create HTTP context: {}", r);
network::CloseSocket(data.SocketFd);
_handles[events[i].data.fd].reset();
network::CloseSocket(fd);
_handles[fd].reset();
continue;
}
// else, continue parsing
Expand Down Expand Up @@ -226,7 +238,16 @@ bool MayhemServer::_MonitorFd(int fd)
_logger->Error("Failed to add fd to epoll");
return false;
}
_logger->Debug("New connection {} monitored", fd);
return true;
}

bool MayhemServer::_UnmonitorFd(int fd)
{
if (epoll::Unmonitor(_epollFd, fd) != 0)
{
_logger->Error("Failed to remove fd from epoll");
return false;
}
return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/components/MayhemServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class MayhemServer final : public IServer
void _OpenEpoll();
void _CloseEpoll();
bool _MonitorFd(int fd);
bool _UnmonitorFd(int fd);

private:
/**
Expand Down
4 changes: 3 additions & 1 deletion src/threading/Queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ namespace threading
{

// It is better to align critical data to avoid false sharing.
#if defined(__cpp_lib_hardware_interference_size)
// Some compiler may throw error for std::hardware_destructive_interference_size,
// so we use 64 as the default value.
#if defined(__cpp_lib_hardware_interference_size) && 0
static constexpr size_t HARDWARE_INTERFERENCE_SIZE = std::hardware_destructive_interference_size;
#else
static constexpr size_t HARDWARE_INTERFERENCE_SIZE = 64;
Expand Down
5 changes: 5 additions & 0 deletions src/utils/Epoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ int Monitor(int epfd, int fd, uint32_t events, void* data)
return epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event);
}

int Unmonitor(int epfd, int fd)
{
return epoll_ctl(epfd, EPOLL_CTL_DEL, fd, nullptr);
}

int Modify(int epfd, int fd, uint32_t events, void* data)
{
epoll_event event;
Expand Down
8 changes: 8 additions & 0 deletions src/utils/Epoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ int Monitor(int epfd, int fd, uint32_t events);
*/
int Monitor(int epfd, int fd, uint32_t events, void* data);

/**
* @brief Remove a monitored fd from epoll.
* @param epfd Epoll fd.
* @param fd The fd to unmonitor.
* @return 0 on success, -1 on failure.
*/
int Unmonitor(int epfd, int fd);

/**
* @brief Modify a monitored epoll event.
* @param epfd Epoll fd.
Expand Down

0 comments on commit 59d5397

Please sign in to comment.