mysql & http refactor

This commit is contained in:
alice
2026-07-27 06:06:23 +02:00
parent 05be91f6bc
commit 4969d6f28f
7 changed files with 492 additions and 269 deletions
+21 -33
View File
@@ -16,55 +16,43 @@ namespace sql = sqlpp::mysql;
namespace mysql
{
constexpr auto max_connections = 256;
constexpr auto max_connections = 100;
constexpr auto connection_timeout = 200s;
using database_mutex_t = std::recursive_mutex;
using database_t = std::unique_ptr<sql::connection>;
struct connection_t
utils::concurrency::container<sql::connection_config>& get_config();
class connection
{
public:
connection() = default;
void check();
void cleanup();
database_t db;
database_mutex_t mutex;
std::chrono::high_resolution_clock::time_point start;
std::chrono::high_resolution_clock::time_point last_access;
private:
std::chrono::high_resolution_clock::time_point start_;
std::chrono::high_resolution_clock::time_point last_access_;
};
extern std::array<connection_t, max_connections> connection_pool;
utils::concurrency::container<sql::connection_config>& get_config();
connection* get_connection(std::unique_lock<database_mutex_t>& lock);
template <typename T = void, typename F>
T access(F&& accessor)
{
for (auto& connection : connection_pool)
std::unique_lock<database_mutex_t> lock;
auto conn = get_connection(lock);
if (conn == nullptr)
{
std::unique_lock<database_mutex_t> lock(connection.mutex, std::try_to_lock);
if (!lock.owns_lock())
{
continue;
}
const auto now = std::chrono::high_resolution_clock::now();
const auto diff = now - connection.start;
if (!connection.db.get() || !connection.db->ping_server() || diff >= 1h)
{
get_config().access([&](sql::connection_config& cfg)
{
connection.db = std::make_unique<sql::connection>(cfg);
connection.start = now;
});
}
connection.last_access = now;
return accessor(connection.db);
throw std::runtime_error("out of connections");
}
throw std::runtime_error("out of connections");
conn->check();
return accessor(conn->db);
}
void cleanup_connections();
void run_tasks();
}