Skip to content

Commit

Permalink
add fiber executor
Browse files Browse the repository at this point in the history
  • Loading branch information
felixguendling committed Nov 29, 2024
1 parent 6a457f5 commit 9c660c3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .pkg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[boost]
url=git@github.com:motis-project/boost.git
branch=master
commit=73549ebca677fe6214202a1ab580362b4f80e653
commit=082a2d83c827e43f3b7eb8d6f0a1102cddb897ad
[utl]
url=git@github.com:motis-project/utl.git
branch=master
Expand Down
36 changes: 35 additions & 1 deletion include/net/web_server/query_router.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

#include "utl/overloaded.h"

#include "boost/fiber/algo/work_stealing.hpp"
#include "boost/fiber/buffered_channel.hpp"
#include "boost/fiber/operations.hpp"
#include "boost/json.hpp"
#include "boost/url.hpp"

Expand Down Expand Up @@ -57,7 +60,7 @@ struct default_exec {
struct asio_exec {
asio_exec(boost::asio::io_context& io, boost::asio::io_context& worker_pool);

auto exec(auto&& f, web_server::http_res_cb_t cb) {
void exec(auto&& f, web_server::http_res_cb_t cb) {
worker_pool_.post([&, f = std::move(f), cb = std::move(cb)]() mutable {
try {
auto res = std::make_shared<web_server::http_res_t>(f());
Expand All @@ -84,6 +87,37 @@ struct asio_exec {
boost::asio::io_context& worker_pool_;
};

struct fiber_exec {
using task_t = std::function<void()>;
using channel_t = boost::fibers::buffered_channel<task_t>;

explicit fiber_exec(boost::asio::io_context& io, channel_t& ch)
: io_{io}, ch_{ch} {}

void exec(auto&& f, web_server::http_res_cb_t cb) {
ch_.try_push([&, f = std::move(f), cb = std::move(cb)]() {
auto res = std::make_shared<web_server::http_res_t>(f());
io_.post([cb = std::move(cb), res = std::move(res)]() mutable {
cb(std::move(*res));
});
});
}

static auto run(channel_t& ch, std::size_t const n_threads) {
return [n_threads, &ch]() {
boost::fibers::use_scheduling_algorithm<
boost::fibers::algo::work_stealing>(n_threads);
auto t = task_t{};
while (ch.pop(t) != boost::fibers::channel_op_status::closed) {
t();
}
};
}

boost::asio::io_context& io_;
channel_t& ch_;
};

using route_request_handler = std::function<reply(route_request, bool)>;
struct handler;

Expand Down
1 change: 1 addition & 0 deletions src/web_server/query_router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,6 @@ void query_router<Executor>::set_credentials(route_request& req) {

template struct query_router<default_exec>;
template struct query_router<asio_exec>;
template struct query_router<fiber_exec>;

} // namespace net

0 comments on commit 9c660c3

Please sign in to comment.