event loop with threadmgr
Some checks failed
Docker Build & Publish / build (push) Failing after 1h11m38s

This commit is contained in:
Nicholas Orlowsky 2025-02-21 14:09:16 -05:00
parent ca05aa1e5a
commit 058c395095
7 changed files with 2481 additions and 0 deletions

19
.direnv/bin/nix-direnv-reload Executable file
View file

@ -0,0 +1,19 @@
#!/usr/bin/env bash
set -e
if [[ ! -d "/home/nickorlow/programming/personal/anthracite" ]]; then
echo "Cannot find source directory; Did you move it?"
echo "(Looking for "/home/nickorlow/programming/personal/anthracite")"
echo 'Cannot force reload with this script - use "direnv reload" manually and then try again'
exit 1
fi
# rebuild the cache forcefully
_nix_direnv_force_reload=1 direnv exec "/home/nickorlow/programming/personal/anthracite" true
# Update the mtime for .envrc.
# This will cause direnv to reload again - but without re-building.
touch "/home/nickorlow/programming/personal/anthracite/.envrc"
# Also update the timestamp of whatever profile_rc we have.
# This makes sure that we know we are up to date.
touch -r "/home/nickorlow/programming/personal/anthracite/.envrc" "/home/nickorlow/programming/personal/anthracite/.direnv"/*.rc

View file

@ -0,0 +1 @@
/nix/store/w578af1s1zz4s6s3q2mhr5m2x7jq0cpi-nix-shell-env

File diff suppressed because it is too large Load diff

54
lib/config/config.hpp Normal file
View file

@ -0,0 +1,54 @@
#pragma once
#include <optional>
#include <inttypes.h>
#include <string>
namespace anthracite::config {
class http_config {
uint16_t _port;
public:
http_config(uint16_t port) : _port(port) {};
virtual ~http_config() {};
uint16_t port() { return _port; }
};
class https_config : public http_config {
std::string _cert_path;
std::string _key_path;
public:
https_config(uint16_t port, std::string cert_path, std::string key_path) :
http_config(port), _cert_path(cert_path), _key_path(key_path) {};
};
class config {
uint16_t _worker_threads;
std::optional<http_config> _http_config;
std::optional<https_config> _https_config;
public:
config(uint16_t worker_threads) : _worker_threads(worker_threads) {
}
void add_http_config(http_config config) {
_http_config = config;
}
void add_https_config(https_config config) {
_https_config = config;
}
uint16_t worker_threads() {
return _worker_threads;
}
std::optional<http_config>& http_cfg() {
return _http_config;
}
std::optional<https_config>& https_cfg() {
return _https_config;
}
};
};

View file

@ -0,0 +1,176 @@
#include "./event_loop.hpp"
#include "../log/log.hpp"
#include "../socket/openssl_socket.hpp"
#include <mutex>
#include <chrono>
#include <syncstream>
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
namespace anthracite::thread_mgr {
event_loop::event::event(socket::anthracite_socket* socket) :
_socket(socket) {}
socket::anthracite_socket* event_loop::event::socket() {
return _socket;
}
event_loop::event_loop(backends::backend& backend, config::config& config) : thread_mgr(backend, config), _error_backend("./www") {}
bool event_loop::event_handler(event& event) {
std::string raw_request = event.socket()->recv_message(http::HEADER_BYTES);
// We're doing the start here even though it would ideally be done
// before the first line since if we leave the connection open for
// HTTP 1.1, we can spend a bit of time waiting
auto start = high_resolution_clock::now();
if (raw_request == "") {
event.socket()->close_conn();
delete event.socket();
return false;
}
http::request req(raw_request, event.socket()->get_client_ip());
std::unique_ptr<http::response> resp = req.is_supported_version() ? _backend.handle_request(req) : _error_backend.handle_error(http::status_codes::HTTP_VERSION_NOT_SUPPORTED);
std::string header = resp->header_to_string();
event.socket()->send_message(header);
event.socket()->send_message(resp->content());
auto end = high_resolution_clock::now();
auto ms_int = duration_cast<std::chrono::microseconds>(end-start);
//log_request_and_response(req, resp , ms_int.count());
resp.reset();
if (req.close_connection()) {
event.socket()->close_conn();
delete event.socket();
return false;
}
return true;
}
void event_loop::worker_thread_loop(int threadno) {
unsigned char buf[sizeof(class event)];
std::osyncstream(log::info) << "Starting worker thread " << threadno << std::endl;
while(_run) {
// Get event from queue
std::unique_lock lock(_event_mtx);
event* ev = nullptr;
if (_events.size() > 0) {
ev = new (buf) event(_events.back());
_events.pop();
lock.unlock();
} else {
_event_cv.wait(lock, [this]{ return this->_events.size() > 0 || !_run; });
if (!_run) {
break;
}
ev = new (buf) event(_events.back());
_events.pop();
lock.unlock();
}
// process
bool requeue = event_handler(*ev);
// if necessary, requeue
if (requeue) {
{
std::lock_guard<std::mutex> lg(_event_mtx);
_events.push(*ev);
}
_event_cv.notify_one();
}
}
std::osyncstream(log::info) << "Stopping worker thread " << threadno << std::endl;
}
void event_loop::listener_thread_loop(config::http_config& http_config) {
socket::anthracite_socket* socket;
config::http_config* http_ptr = &http_config;
config::https_config* https_ptr = dynamic_cast<config::https_config*>(http_ptr);
bool is_tls = https_ptr != nullptr;
if (is_tls){
socket = new socket::openssl_socket(https_ptr->port());
} else {
socket = new socket::anthracite_socket(http_ptr->port());
}
std::osyncstream(log::info) << "Listening for " << (is_tls ? "HTTPS" : "HTTP" ) << " connections on port " << http_ptr->port() << std::endl;
while (_run) {
if(socket->wait_for_conn()) {
socket::anthracite_socket* client_sock;
if (is_tls){
socket::openssl_socket* ssl_sock = dynamic_cast<socket::openssl_socket*>(socket);
client_sock = new socket::openssl_socket(*ssl_sock);
} else {
client_sock = new socket::anthracite_socket(*socket);
}
std::lock_guard<std::mutex> lg(_event_mtx);
_events.push(event(client_sock));
_event_cv.notify_one();
}
}
std::osyncstream(log::info) << "Stopping listening for " << (is_tls ? "HTTPS" : "HTTP") << " connections on port " << http_ptr->port() << std::endl;
delete socket;
}
void event_loop::start() {
log::info << "Starting event_loop Thread Manager" << std::endl;
_run = true;
std::vector<std::thread> listener_threads;
std::vector<std::thread> worker_threads;
for(int i = 0; i < _config.worker_threads(); i++) {
auto thread = std::thread(&event_loop::worker_thread_loop, this, i);
worker_threads.push_back(std::move(thread));
}
if (_config.http_cfg().has_value()) {
auto thread = std::thread(&event_loop::listener_thread_loop, this, std::ref(_config.http_cfg().value()));
listener_threads.push_back(std::move(thread));
}
if (_config.https_cfg().has_value()) {
auto thread = std::thread(&event_loop::listener_thread_loop, this, std::ref(_config.https_cfg().value()));
listener_threads.push_back(std::move(thread));
}
for(std::thread& t : worker_threads) {
t.join();
}
for(std::thread& t : listener_threads) {
t.join();
}
}
void event_loop::stop() {
_run = false;
std::lock_guard<std::mutex> lg(_event_mtx);
_event_cv.notify_all();
}
}

View file

@ -0,0 +1,31 @@
#include "./thread_mgr.hpp"
#include "../socket/socket.hpp"
#include "../backends/file_backend.hpp"
#include <condition_variable>
#include <mutex>
#include <queue>
namespace anthracite::thread_mgr {
class event_loop : public virtual thread_mgr {
class event {
socket::anthracite_socket* _socket;
public:
event(socket::anthracite_socket* socket);
socket::anthracite_socket* socket();
};
std::mutex _event_mtx;
std::condition_variable _event_cv;
std::queue<event> _events;
backends::file_backend _error_backend;
void worker_thread_loop(int threadno);
void listener_thread_loop(config::http_config& http_config);
bool event_handler(event& ev);
public:
event_loop(backends::backend& backend, config::config& config);
void start() override;
void stop() override;
};
};

View file

@ -0,0 +1,18 @@
#pragma once
#include "../backends/backend.hpp"
#include "../config/config.hpp"
namespace anthracite::thread_mgr {
class thread_mgr {
protected:
bool _run;
backends::backend& _backend;
config::config& _config;
public:
thread_mgr(backends::backend& backend, config::config& config): _backend(backend), _config(config) {}
virtual ~thread_mgr() = default;
virtual void start() = 0;
virtual void stop() = 0;
};
};