Skip to content

Commit 02b0eb8

Browse files
committed
fix(core): stabilize HTTP/WS runtime lifecycle and improve shutdown handling
- unify App, HTTPServer and Session lifecycle - improve SIGINT (Ctrl+C) responsiveness - ensure safe session teardown without blocking threads - remove unsafe background behaviors in Session - prepare foundation for executor-driven scheduling
1 parent 7063dd5 commit 02b0eb8

6 files changed

Lines changed: 29 additions & 38 deletions

File tree

include/vix/app/App.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <vector>
2727

2828
#include <vix/config/Config.hpp>
29-
#include <vix/executor/IExecutor.hpp>
29+
#include <vix/executor/RuntimeExecutor.hpp>
3030
#include <vix/http/RequestHandler.hpp>
3131
#include <vix/http/ResponseWrapper.hpp>
3232
#include <vix/router/Router.hpp>
@@ -120,7 +120,7 @@ namespace vix
120120
*
121121
* @param executor Shared executor used by the application.
122122
*/
123-
explicit App(std::shared_ptr<vix::executor::IExecutor> executor);
123+
explicit App(std::shared_ptr<vix::executor::RuntimeExecutor> executor);
124124

125125
/**
126126
* @brief Destroys the application and releases owned resources.
@@ -318,11 +318,11 @@ namespace vix
318318
}
319319

320320
/**
321-
* @brief Returns the executor used by the application.
321+
* @brief Returns the runtime executor used by the application.
322322
*
323-
* @return vix::executor::IExecutor& Executor.
323+
* @return vix::executor::RuntimeExecutor& Executor.
324324
*/
325-
vix::executor::IExecutor &executor() noexcept
325+
vix::executor::RuntimeExecutor &executor() noexcept
326326
{
327327
return *executor_;
328328
}
@@ -1011,7 +1011,7 @@ namespace vix
10111011
private:
10121012
vix::config::Config config_;
10131013
std::shared_ptr<vix::router::Router> router_;
1014-
std::shared_ptr<vix::executor::IExecutor> executor_;
1014+
std::shared_ptr<vix::executor::RuntimeExecutor> executor_;
10151015
vix::server::HTTPServer server_;
10161016

10171017
ShutdownCallback shutdown_cb_{};

include/vix/server/HTTPServer.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include <vix/async/core/task.hpp>
2828
#include <vix/async/net/tcp.hpp>
2929
#include <vix/config/Config.hpp>
30-
#include <vix/executor/IExecutor.hpp>
30+
#include <vix/executor/RuntimeExecutor.hpp>
3131
#include <vix/router/Router.hpp>
3232

3333
namespace vix::server
@@ -59,7 +59,6 @@ namespace vix::server
5959
*
6060
* This server does not depend on Boost.Asio or Boost.Beast.
6161
* Networking is provided by vix::async::net and application execution is
62-
* delegated through vix::executor::IExecutor.
6362
*/
6463
class HTTPServer
6564
{
@@ -74,7 +73,7 @@ namespace vix::server
7473
*/
7574
explicit HTTPServer(
7675
vix::config::Config &config,
77-
std::shared_ptr<vix::executor::IExecutor> executor);
76+
std::shared_ptr<vix::executor::RuntimeExecutor> executor);
7877

7978
HTTPServer(const HTTPServer &) = delete;
8079
HTTPServer &operator=(const HTTPServer &) = delete;
@@ -196,7 +195,7 @@ namespace vix::server
196195
*
197196
* @return Shared executor instance.
198197
*/
199-
[[nodiscard]] std::shared_ptr<vix::executor::IExecutor> executor() const noexcept
198+
std::shared_ptr<vix::executor::RuntimeExecutor> executor() const noexcept
200199
{
201200
return executor_;
202201
}
@@ -312,7 +311,7 @@ namespace vix::server
312311
/**
313312
* @brief Generic executor used for application work.
314313
*/
315-
std::shared_ptr<vix::executor::IExecutor> executor_;
314+
std::shared_ptr<vix::executor::RuntimeExecutor> executor_;
316315

317316
/**
318317
* @brief Threads running the native async I/O context.

include/vix/session/Session.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <vix/async/core/timer.hpp>
2929
#include <vix/async/net/tcp.hpp>
3030
#include <vix/config/Config.hpp>
31-
#include <vix/executor/IExecutor.hpp>
31+
#include <vix/executor/RuntimeExecutor.hpp>
3232
#include <vix/http/Request.hpp>
3333
#include <vix/http/Response.hpp>
3434
#include <vix/router/Router.hpp>
@@ -77,10 +77,11 @@ namespace vix::session
7777
* @param config Server configuration.
7878
* @param executor Executor used for handler execution.
7979
*/
80-
explicit Session(std::unique_ptr<tcp_stream> stream,
81-
vix::router::Router &router,
82-
const vix::config::Config &config,
83-
std::shared_ptr<vix::executor::IExecutor> executor);
80+
explicit Session(
81+
std::unique_ptr<tcp_stream> stream,
82+
vix::router::Router &router,
83+
const vix::config::Config &config,
84+
std::shared_ptr<vix::executor::RuntimeExecutor> executor);
8485

8586
/** @brief Destroy the session. */
8687
~Session() = default;
@@ -195,7 +196,7 @@ namespace vix::session
195196
std::unique_ptr<tcp_stream> stream_;
196197
vix::router::Router &router_;
197198
const vix::config::Config &config_;
198-
std::shared_ptr<vix::executor::IExecutor> executor_;
199+
std::shared_ptr<vix::executor::RuntimeExecutor> executor_;
199200

200201
std::string read_buffer_{};
201202
std::shared_ptr<io_context> io_context_{};

src/app/App.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*/
1414
#include <vix/app/App.hpp>
1515

16-
#include <vix/experimental/ThreadPoolExecutor.hpp>
1716
#include <vix/openapi/register_docs.hpp>
1817
#include <vix/router/Router.hpp>
1918
#include <vix/utils/Env.hpp>
@@ -125,24 +124,13 @@ namespace
125124
return static_cast<std::size_t>(hc);
126125
}
127126

128-
std::shared_ptr<vix::executor::IExecutor> make_default_executor()
127+
std::shared_ptr<vix::executor::RuntimeExecutor> make_default_executor()
129128
{
130-
const std::size_t threads = compute_executor_threads();
131-
const std::size_t max_threads = threads;
132-
constexpr int default_priority = 0;
129+
const auto threads = static_cast<std::uint32_t>(compute_executor_threads());
133130

134-
std::unique_ptr<vix::executor::IExecutor> exec =
135-
vix::experimental::make_threadpool_executor(
136-
threads,
137-
max_threads,
138-
default_priority);
139-
140-
if (!exec)
141-
{
142-
throw std::runtime_error("failed to create default thread pool executor");
143-
}
144-
145-
return std::shared_ptr<vix::executor::IExecutor>(std::move(exec));
131+
auto exec = std::make_shared<vix::executor::RuntimeExecutor>(threads);
132+
exec->start();
133+
return exec;
146134
}
147135

148136
void register_bench_route(vix::router::Router &router)
@@ -244,7 +232,7 @@ namespace vix
244232
}
245233
}
246234

247-
App::App(std::shared_ptr<vix::executor::IExecutor> executor)
235+
App::App(std::shared_ptr<vix::executor::RuntimeExecutor> executor)
248236
: config_(),
249237
router_(nullptr),
250238
executor_(std::move(executor)),
@@ -255,6 +243,8 @@ namespace vix
255243
log().throwError("App: executor cannot be null");
256244
}
257245

246+
executor_->start();
247+
258248
log().setPattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v");
259249
log().setLevel(parse_log_level_from_env());
260250

src/server/HTTPServer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ namespace vix::server
7575
}
7676
} // namespace
7777

78-
HTTPServer::HTTPServer(vix::config::Config &config,
79-
std::shared_ptr<vix::executor::IExecutor> executor)
78+
HTTPServer::HTTPServer(
79+
vix::config::Config &config,
80+
std::shared_ptr<vix::executor::RuntimeExecutor> executor)
8081
: config_(config),
8182
io_context_(std::make_shared<vix::async::core::io_context>()),
8283
listener_(nullptr),

src/session/Session.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ namespace vix::session
187187
Session::Session(std::unique_ptr<tcp_stream> stream,
188188
vix::router::Router &router,
189189
const vix::config::Config &config,
190-
std::shared_ptr<vix::executor::IExecutor> executor)
190+
std::shared_ptr<vix::executor::RuntimeExecutor> executor)
191191
: stream_(std::move(stream)),
192192
router_(router),
193193
config_(config),

0 commit comments

Comments
 (0)