Skip to content

Commit 5abc4a1

Browse files
committed
fix(core): make session timeout configurable via Config
1 parent 0be4577 commit 5abc4a1

4 files changed

Lines changed: 16 additions & 5 deletions

File tree

include/vix/config/Config.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ namespace vix::config
6363
const std::string &getWafMode() const noexcept; // "off"|"basic"|"strict"
6464
int getWafMaxTargetLen() const noexcept;
6565
int getWafMaxBodyBytes() const noexcept;
66+
int getSessionTimeoutSec() const noexcept;
6667

6768
private:
6869
static constexpr const char *DEFAULT_DB_HOST = "localhost";
@@ -99,6 +100,9 @@ namespace vix::config
99100
std::string waf_mode_;
100101
int waf_max_target_len_;
101102
int waf_max_body_bytes_;
103+
104+
static constexpr int DEFAULT_SESSION_TIMEOUT_SEC = 20;
105+
int session_timeout_sec_;
102106
};
103107
}
104108

include/vix/session/Session.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ namespace vix::session
4949
using tcp = net::ip::tcp;
5050

5151
constexpr size_t MAX_REQUEST_BODY_SIZE = 10 * 1024 * 1024;
52-
constexpr auto REQUEST_TIMEOUT = std::chrono::seconds(20);
5352

5453
class Session : public std::enable_shared_from_this<Session>
5554
{

src/config/Config.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace vix::config
4242
db_host(DEFAULT_DB_HOST), db_user(DEFAULT_DB_USER), db_pass(DEFAULT_DB_PASS),
4343
db_name(DEFAULT_DB_NAME), db_port(DEFAULT_DB_PORT),
4444
server_port(DEFAULT_SERVER_PORT), request_timeout(DEFAULT_REQUEST_TIMEOUT),
45-
rawConfig_(nlohmann::json::object()), io_threads_(DEFAULT_IO_THREADS), log_async_(DEFAULT_LOG_ASYNC), log_queue_max_(DEFAULT_LOG_QUEUE_MAX), log_drop_on_overflow_(DEFAULT_LOG_DROP_ON_OVERFLOW), waf_mode_(DEFAULT_WAF_MODE), waf_max_target_len_(DEFAULT_WAF_MAX_TARGET_LEN), waf_max_body_bytes_(DEFAULT_WAF_MAX_BODY_BYTES)
45+
rawConfig_(nlohmann::json::object()), io_threads_(DEFAULT_IO_THREADS), log_async_(DEFAULT_LOG_ASYNC), log_queue_max_(DEFAULT_LOG_QUEUE_MAX), log_drop_on_overflow_(DEFAULT_LOG_DROP_ON_OVERFLOW), waf_mode_(DEFAULT_WAF_MODE), waf_max_target_len_(DEFAULT_WAF_MAX_TARGET_LEN), waf_max_body_bytes_(DEFAULT_WAF_MAX_BODY_BYTES), session_timeout_sec_(DEFAULT_SESSION_TIMEOUT_SEC)
4646
{
4747
std::vector<fs::path> candidate_paths;
4848

@@ -123,6 +123,9 @@ namespace vix::config
123123

124124
rawConfig_ = cfg;
125125

126+
// maintenant tu peux lire via getInt()
127+
session_timeout_sec_ = getInt("server.session_timeout_sec", DEFAULT_SESSION_TIMEOUT_SEC);
128+
126129
if (cfg.contains("database") && cfg["database"].contains("default"))
127130
{
128131
const auto &db = cfg["database"]["default"];
@@ -309,5 +312,6 @@ namespace vix::config
309312
const std::string &Config::getWafMode() const noexcept { return waf_mode_; }
310313
int Config::getWafMaxTargetLen() const noexcept { return waf_max_target_len_; }
311314
int Config::getWafMaxBodyBytes() const noexcept { return waf_max_body_bytes_; }
315+
int Config::getSessionTimeoutSec() const noexcept { return session_timeout_sec_; }
312316

313317
}

src/session/Session.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,24 @@ namespace vix::session
6565
void Session::start_timer()
6666
{
6767
timer_ = std::make_shared<net::steady_timer>(socket_->get_executor());
68-
timer_->expires_after(REQUEST_TIMEOUT);
68+
const auto timeout = std::chrono::seconds(config_.getSessionTimeoutSec());
69+
timer_->expires_after(timeout);
6970

7071
std::weak_ptr<net::steady_timer> weak_timer = timer_;
7172
auto self = shared_from_this();
73+
7274
timer_->async_wait(
73-
[this, self, weak_timer](const boost::system::error_code &ec)
75+
[this, self, weak_timer, timeout](const boost::system::error_code &ec)
7476
{
7577
auto t = weak_timer.lock();
7678
if (!t)
7779
return;
7880

7981
if (!ec)
8082
{
81-
log().log(Logger::Level::WARN, "[Session] Timeout ({}s), closing socket", REQUEST_TIMEOUT.count());
83+
log().log(Logger::Level::WARN,
84+
"[Session] Timeout ({}s), closing socket",
85+
timeout.count());
8286
close_socket_gracefully();
8387
}
8488
});

0 commit comments

Comments
 (0)