faster nonblocking io
Some checks failed
Docker Build & Publish / build (push) Failing after 1h24m16s

This commit is contained in:
Nicholas Orlowsky 2025-02-23 17:06:32 -05:00
parent 6c5feb8675
commit c07f3ebf81
8 changed files with 66 additions and 72 deletions

View file

@ -6,7 +6,9 @@
#include <chrono>
#include <mutex>
#include <pthread.h>
#include <sstream>
#include <syncstream>
#include "signal.h"
using std::chrono::duration;
using std::chrono::duration_cast;
@ -50,11 +52,6 @@ bool event_loop::event_handler(socket::anthracite_socket* sock)
sock->send_message(header);
sock->send_message(resp->content());
auto end = high_resolution_clock::now();
//auto ms_int = duration_cast<std::chrono::microseconds>(end - event.timestamp());
//log::logger.log_request_and_response(req, resp, 9);//ms_int.count());
resp.reset();
if (req.close_connection()) {
return false;
}
@ -62,26 +59,30 @@ bool event_loop::event_handler(socket::anthracite_socket* sock)
return true;
}
#define QATATIME (50)
void event_loop::worker_thread_loop(int threadno)
{
struct epoll_event* events = new struct epoll_event[_config.max_clients()];
int epoll_fd = _epoll_fds[threadno];
std::stringstream ss;
ss << "worker " << threadno;
pthread_setname_np(pthread_self(), ss.str().c_str());
std::osyncstream(log::info) << "Starting worker thread " << threadno << " on pid " << syscall(SYS_gettid) << std::endl;
struct epoll_event* events = new struct epoll_event[_config.max_clients()];
int timeout_ms = 1000;
if (_nonblocking) {
timeout_ms = 0;
}
log::info << "Starting worker thread " << threadno << std::endl;
while (_run) {
// Get event from queue
int ready_fds = epoll_wait(epoll_fd, events, _config.max_clients(), 1000);
int ready_fds = epoll_wait(_epoll_fd, events, _config.max_clients(), timeout_ms);
if (ready_fds > 0) {
std::lock_guard<std::mutex> lg(_event_mtx);
for (int i = 0; i < ready_fds; i++) {
socket::anthracite_socket* sockptr = reinterpret_cast<socket::anthracite_socket*>(events[i].data.ptr);
if (!event_handler(sockptr)) {
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, sockptr->csock(), &events[i]);
epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, sockptr->csock(), &events[i]);
sockptr->close_conn();
delete sockptr;
}
@ -89,29 +90,7 @@ void event_loop::worker_thread_loop(int threadno)
}
}
std::osyncstream(log::info) << "Stopping worker thread " << threadno << std::endl;
}
void event_loop::eventer_thread_loop()
{
//struct epoll_event* events = new struct epoll_event[_config.max_clients()];
//std::osyncstream(log::info) << "epoll() thread started on pid " << getpid() << std::endl;
//while (_run) {
// int ready_fds = epoll_wait(_epoll_fd, events, _config.max_clients(), 1000);
// if (ready_fds > 0) {
// std::lock_guard<std::mutex> lg(_event_mtx);
// for (int i = 0; i < ready_fds; i++) {
// socket::anthracite_socket* sockptr = reinterpret_cast<socket::anthracite_socket*>(events[i].data.ptr);
// struct epoll_event ev;
// epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, sockptr->csock(), &events[i]);
// _events.push(event(sockptr, std::chrono::high_resolution_clock::now()));
// }
// _event_cv.notify_all();
// }
//}
//delete[] events;
//std::osyncstream(log::info) << "epoll() thread exited" << std::endl;
log::info << "Stopping worker thread " << threadno << std::endl;
}
void event_loop::listener_thread_loop(config::http_config& http_config)
@ -129,9 +108,8 @@ void event_loop::listener_thread_loop(config::http_config& http_config)
socket = new socket::anthracite_socket(http_ptr->port());
}
std::osyncstream(log::info) << "Listening for " << (is_tls ? "HTTPS" : "HTTP") << " connections on port " << http_ptr->port() << " on pid " << getpid() << std::endl;
std::osyncstream(log::info) << "Listening for " << (is_tls ? "HTTPS" : "HTTP") << " connections on port " << http_ptr->port() << std::endl;
int assign_thread = 0;
while (_run) {
if (socket->wait_for_conn()) {
socket::anthracite_socket* client_sock;
@ -144,10 +122,9 @@ void event_loop::listener_thread_loop(config::http_config& http_config)
}
struct epoll_event event;
event.events = EPOLLIN;
event.events = EPOLLIN | EPOLLEXCLUSIVE;// | EPOLLET;
event.data.ptr = client_sock;
epoll_ctl(_epoll_fds[assign_thread], EPOLL_CTL_ADD, client_sock->csock(), &event);
assign_thread = (assign_thread + 1) % _epoll_fds.size();
epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, client_sock->csock(), &event);
}
}
@ -158,15 +135,16 @@ void event_loop::listener_thread_loop(config::http_config& http_config)
void event_loop::start()
{
signal(SIGPIPE, SIG_IGN);
log::info << "Starting event_loop Thread Manager" << std::endl;
_run = true;
_epoll_fd = epoll_create(1);
std::vector<std::thread> listener_threads;
std::vector<std::thread> worker_threads;
for (int i = 0; i < _config.worker_threads(); i++) {
_epoll_fds.push_back(epoll_create(1));
auto thread = std::thread(&event_loop::worker_thread_loop, this, i);
worker_threads.push_back(std::move(thread));
}
@ -181,11 +159,6 @@ void event_loop::start()
listener_threads.push_back(std::move(thread));
}
//{
// auto thread = std::thread(&event_loop::eventer_thread_loop, this);
// listener_threads.push_back(std::move(thread));
//}
for (std::thread& t : worker_threads) {
t.join();
}
@ -198,7 +171,5 @@ void event_loop::start()
void event_loop::stop()
{
_run = false;
std::lock_guard<std::mutex> lg(_event_mtx);
_event_cv.notify_all();
}
}

View file

@ -17,11 +17,10 @@ namespace anthracite::thread_mgr {
std::chrono::time_point<std::chrono::high_resolution_clock>& timestamp();
};
std::vector<int> _epoll_fds;
int _epoll_fd;
std::mutex _event_mtx;
std::condition_variable _event_cv;
std::queue<event> _events;
backends::file_backend _error_backend;
bool _nonblocking;
void worker_thread_loop(int threadno);
void listener_thread_loop(config::http_config& http_config);