From cbc137529b2528331328fbeb73893536296bcad8 Mon Sep 17 00:00:00 2001 From: emeric Date: Sun, 3 Oct 2021 14:07:33 +0200 Subject: [PATCH] Made connection count aligned with thread count --- src/libs/database/impl/Db.cpp | 4 ++-- src/libs/database/include/database/Db.hpp | 2 +- src/lms/main.cpp | 19 ++++++++++++------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/libs/database/impl/Db.cpp b/src/libs/database/impl/Db.cpp index 9854facb..5035466c 100644 --- a/src/libs/database/impl/Db.cpp +++ b/src/libs/database/impl/Db.cpp @@ -29,7 +29,7 @@ namespace Database { // Session living class handling the database and the login -Db::Db(const std::filesystem::path& dbPath) +Db::Db(const std::filesystem::path& dbPath, std::size_t connectionCount) { LMS_LOG(DB, INFO) << "Creating connection pool on file " << dbPath.string(); @@ -38,7 +38,7 @@ Db::Db(const std::filesystem::path& dbPath) connection->executeSql("pragma journal_mode=WAL"); connection->executeSql("pragma synchronous=normal"); - auto connectionPool = std::make_unique(std::move(connection), 10); + auto connectionPool = std::make_unique(std::move(connection), connectionCount); connectionPool->setTimeout(std::chrono::seconds(10)); _connectionPool = std::move(connectionPool); diff --git a/src/libs/database/include/database/Db.hpp b/src/libs/database/include/database/Db.hpp index bdcd20b3..5effb856 100644 --- a/src/libs/database/include/database/Db.hpp +++ b/src/libs/database/include/database/Db.hpp @@ -32,7 +32,7 @@ class Db { public: - Db(const std::filesystem::path& dbPath); + Db(const std::filesystem::path& dbPath, std::size_t connectionCount = 10); ~Db(); Db(const Db&) = delete; diff --git a/src/lms/main.cpp b/src/lms/main.cpp index e14c3573..26da8ef4 100644 --- a/src/lms/main.cpp +++ b/src/lms/main.cpp @@ -44,6 +44,16 @@ #include "utils/String.hpp" #include "utils/WtLogger.hpp" +static +std::size_t +getThreadCount() +{ + const unsigned long configHttpServerThreadCount {Service::get()->getULong("http-server-thread-count", 0)}; + + // Reserve at least 2 threads since we still have some blocking IO (for example when reading from ffmpeg) + return configHttpServerThreadCount ? configHttpServerThreadCount : std::max(2, std::thread::hardware_concurrency()); +} + static std::vector generateWtConfig(std::string execPath) @@ -54,7 +64,6 @@ generateWtConfig(std::string execPath) const std::filesystem::path wtLogFilePath {Service::get()->getPath("log-file", "/var/log/lms.log")}; const std::filesystem::path wtAccessLogFilePath {Service::get()->getPath("access-log-file", "/var/log/lms.access.log")}; const std::filesystem::path wtResourcesPath {Service::get()->getPath("wt-resources", "/usr/share/Wt/resources")}; - const unsigned long configHttpServerThreadCount {Service::get()->getULong("http-server-thread-count", 0)}; args.push_back(execPath); args.push_back("--config=" + wtConfigPath.string()); @@ -81,11 +90,7 @@ generateWtConfig(std::string execPath) if (!wtAccessLogFilePath.empty()) args.push_back("--accesslog=" + wtAccessLogFilePath.string()); - { - // Reserve at least 2 threads since we still have some blocking IO (for example when reading from ffmpeg) - const unsigned long httpServerThreadCount {configHttpServerThreadCount ? configHttpServerThreadCount : std::max(2, std::thread::hardware_concurrency())}; - args.push_back("--threads=" + std::to_string(httpServerThreadCount)); - } + args.push_back("--threads=" + std::to_string(getThreadCount())); // Generate the wt_config.xml file boost::property_tree::ptree pt; @@ -221,7 +226,7 @@ int main(int argc, char* argv[]) IOContextRunner ioContextRunner {ioContext, std::max(2, std::thread::hardware_concurrency())}; // Initializing a connection pool to the database that will be shared along services - Database::Db database {config->getPath("working-dir") / "lms.db"}; + Database::Db database {config->getPath("working-dir") / "lms.db", getThreadCount()}; { Database::Session session {database}; session.prepareTables();