Skip to content

Commit

Permalink
Pass reference timeout parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy-peak committed Apr 17, 2024
1 parent 7a4a521 commit 397e233
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 29 deletions.
18 changes: 9 additions & 9 deletions out/bi_web/include/frpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ class BiChannel final {

template <typename T>
void send(T&& snd_msgs,
std::chrono::milliseconds timeout,
const std::chrono::milliseconds& timeout,
std::function<void()> cb) {
std::lock_guard lk(m_mutex);
if (!zmq::send_multipart(m_send, std::forward<decltype(snd_msgs)>(snd_msgs))) {
Expand All @@ -411,7 +411,7 @@ class BiChannel final {
{static_cast<void*>(m_socket), 0, ZMQ_POLLIN | ZMQ_POLLERR, 0},
{static_cast<void*>(m_recv), 0, ZMQ_POLLIN | ZMQ_POLLERR, 0},
};
std::chrono::milliseconds interval(200);
std::chrono::milliseconds interval(100);
std::multimap<std::chrono::system_clock::time_point, std::function<void()>> timeout_task;
while (m_running.load()) {
zmq::poll(items, interval);
Expand Down Expand Up @@ -733,7 +733,7 @@ class HelloWorldClient final {

void hello_world(BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional<std::string> date,
std::function<void(std::string, Info, uint64_t, std::optional<std::string>)> cb,
std::chrono::milliseconds timeout,
const std::chrono::milliseconds& timeout,
std::function<void()> timeout_cb) {
auto req_id = m_req_id.fetch_add(1);
auto header = std::make_tuple(req_id, HelloWorldClientHelloWorldServer::hello_world);
Expand All @@ -749,7 +749,7 @@ class HelloWorldClient final {
m_timeout_cb.emplace(req_id, std::move(timeout_cb));
}
m_channel->send(std::move(snd_bufs),
std::move(timeout),
timeout,
[this, req_id]() mutable {
std::unique_lock lk(m_mtx);
#if __cplusplus >= 202302L
Expand Down Expand Up @@ -786,9 +786,9 @@ class HelloWorldClient final {
}

template <asio::completion_token_for<void(std::optional<std::tuple<std::string, Info, uint64_t, std::optional<std::string>>>)> CompletionToken>
auto hello_world_coro(BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional<std::string> date, std::chrono::milliseconds timeout, CompletionToken&& token) {
auto hello_world_coro(BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional<std::string> date, const std::chrono::milliseconds& timeout, CompletionToken&& token) {
return asio::async_initiate<CompletionToken, void(std::optional<std::tuple<std::string, Info, uint64_t, std::optional<std::string>>>)>(
[this]<typename Handler>(Handler&& handler, BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional<std::string> date, auto timeout) mutable {
[this]<typename Handler>(Handler&& handler, BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional<std::string> date, const auto& timeout) mutable {
auto handler_ptr = std::make_shared<Handler>(std::move(handler));
this->hello_world(
std::move(bank_info), std::move(bank_name), blance, std::move(date),
Expand All @@ -798,16 +798,16 @@ class HelloWorldClient final {
(*handler_ptr)(std::make_tuple(std::move(reply), std::move(info), count, std::move(date)));
});
},
std::move(timeout),
[handler_ptr] {
timeout,
[handler_ptr]() mutable {
auto ex = asio::get_associated_executor(*handler_ptr);
asio::post(ex, [=, handler_ptr = std::move(handler_ptr)]() mutable -> void {
(*handler_ptr)(std::nullopt);
});
});
},
token,
std::move(bank_info), std::move(bank_name), blance, std::move(date), std::move(timeout));
std::move(bank_info), std::move(bank_name), blance, std::move(date), timeout);
}
#endif

Expand Down
3 changes: 2 additions & 1 deletion out/bi_web/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct HelloWorldApi final {
auto bank_name = request["bank_name"].template get<std::string>();
auto blance = request["blance"].template get<uint64_t>();
auto date = request["date"].template get<std::optional<std::string>>();
static std::chrono::milliseconds timeout(9000);
m_client->hello_world(
std::move(bank_info), std::move(bank_name), blance, std::move(date),
[callback](std::string reply, Info info, uint64_t count, std::optional<std::string> date) mutable {
Expand All @@ -58,7 +59,7 @@ struct HelloWorldApi final {
resp->setBody(json.dump());
callback(resp);
},
std::chrono::milliseconds(9000),
timeout,
[callback] {
auto resp = drogon::HttpResponse::newHttpResponse(
drogon::HttpStatusCode::k408RequestTimeout,
Expand Down
18 changes: 9 additions & 9 deletions out/frpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ class BiChannel final {

template <typename T>
void send(T&& snd_msgs,
std::chrono::milliseconds timeout,
const std::chrono::milliseconds& timeout,
std::function<void()> cb) {
std::lock_guard lk(m_mutex);
if (!zmq::send_multipart(m_send, std::forward<decltype(snd_msgs)>(snd_msgs))) {
Expand All @@ -411,7 +411,7 @@ class BiChannel final {
{static_cast<void*>(m_socket), 0, ZMQ_POLLIN | ZMQ_POLLERR, 0},
{static_cast<void*>(m_recv), 0, ZMQ_POLLIN | ZMQ_POLLERR, 0},
};
std::chrono::milliseconds interval(200);
std::chrono::milliseconds interval(100);
std::multimap<std::chrono::system_clock::time_point, std::function<void()>> timeout_task;
while (m_running.load()) {
zmq::poll(items, interval);
Expand Down Expand Up @@ -733,7 +733,7 @@ class HelloWorldClient final {

void hello_world(BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional<std::string> date,
std::function<void(std::string, Info, uint64_t, std::optional<std::string>)> cb,
std::chrono::milliseconds timeout,
const std::chrono::milliseconds& timeout,
std::function<void()> timeout_cb) {
auto req_id = m_req_id.fetch_add(1);
auto header = std::make_tuple(req_id, HelloWorldClientHelloWorldServer::hello_world);
Expand All @@ -749,7 +749,7 @@ class HelloWorldClient final {
m_timeout_cb.emplace(req_id, std::move(timeout_cb));
}
m_channel->send(std::move(snd_bufs),
std::move(timeout),
timeout,
[this, req_id]() mutable {
std::unique_lock lk(m_mtx);
#if __cplusplus >= 202302L
Expand Down Expand Up @@ -786,9 +786,9 @@ class HelloWorldClient final {
}

template <asio::completion_token_for<void(std::optional<std::tuple<std::string, Info, uint64_t, std::optional<std::string>>>)> CompletionToken>
auto hello_world_coro(BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional<std::string> date, std::chrono::milliseconds timeout, CompletionToken&& token) {
auto hello_world_coro(BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional<std::string> date, const std::chrono::milliseconds& timeout, CompletionToken&& token) {
return asio::async_initiate<CompletionToken, void(std::optional<std::tuple<std::string, Info, uint64_t, std::optional<std::string>>>)>(
[this]<typename Handler>(Handler&& handler, BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional<std::string> date, auto timeout) mutable {
[this]<typename Handler>(Handler&& handler, BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional<std::string> date, const auto& timeout) mutable {
auto handler_ptr = std::make_shared<Handler>(std::move(handler));
this->hello_world(
std::move(bank_info), std::move(bank_name), blance, std::move(date),
Expand All @@ -798,16 +798,16 @@ class HelloWorldClient final {
(*handler_ptr)(std::make_tuple(std::move(reply), std::move(info), count, std::move(date)));
});
},
std::move(timeout),
[handler_ptr] {
timeout,
[handler_ptr]() mutable {
auto ex = asio::get_associated_executor(*handler_ptr);
asio::post(ex, [=, handler_ptr = std::move(handler_ptr)]() mutable -> void {
(*handler_ptr)(std::nullopt);
});
});
},
token,
std::move(bank_info), std::move(bank_name), blance, std::move(date), std::move(timeout));
std::move(bank_info), std::move(bank_name), blance, std::move(date), timeout);
}
#endif

Expand Down
14 changes: 7 additions & 7 deletions template/cpp/bi.inja
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public:

void {{func.func_name}}({{_format_args(func.inputs)}},
std::function<void({{_format_args_type(func.outputs)}})> cb,
std::chrono::milliseconds timeout,
const std::chrono::milliseconds& timeout,
std::function<void()> timeout_cb) {
auto req_id = m_req_id.fetch_add(1);
auto header = std::make_tuple(req_id, {{value.caller}}{{value.callee}}::{{func.func_name}});
Expand All @@ -69,7 +69,7 @@ public:
m_timeout_cb.emplace(req_id, std::move(timeout_cb));
}
m_channel->send(std::move(snd_bufs),
std::move(timeout),
timeout,
[this, req_id]() mutable {
std::unique_lock lk(m_mtx);
#if __cplusplus >= 202302L
Expand Down Expand Up @@ -106,9 +106,9 @@ public:
}

template <asio::completion_token_for<void(std::optional<std::tuple<{{_format_args_type(func.outputs)}}>>)> CompletionToken>
auto {{func.func_name}}_coro({{_format_args(func.inputs)}}, std::chrono::milliseconds timeout, CompletionToken&& token) {
auto {{func.func_name}}_coro({{_format_args(func.inputs)}}, const std::chrono::milliseconds& timeout, CompletionToken&& token) {
return asio::async_initiate<CompletionToken, void(std::optional<std::tuple<{{_format_args_type(func.outputs)}}>>)>(
[this]<typename Handler>(Handler&& handler, {{_format_args(func.inputs)}}, auto timeout) mutable {
[this]<typename Handler>(Handler&& handler, {{_format_args(func.inputs)}}, const auto& timeout) mutable {
auto handler_ptr = std::make_shared<Handler>(std::move(handler));
this->{{func.func_name}}(
{{_format_args_name_and_move(func.inputs)}},
Expand All @@ -118,16 +118,16 @@ public:
(*handler_ptr)(std::make_tuple({{_format_args_name_and_move(func.outputs)}}));
});
},
std::move(timeout),
[handler_ptr] {
timeout,
[handler_ptr] () mutable {
auto ex = asio::get_associated_executor(*handler_ptr);
asio::post(ex, [=, handler_ptr = std::move(handler_ptr)] () mutable -> void {
(*handler_ptr)(std::nullopt);
});
});
},
token,
{{_format_args_name_and_move(func.inputs)}}, std::move(timeout));
{{_format_args_name_and_move(func.inputs)}}, timeout);
}
#endif

Expand Down
4 changes: 2 additions & 2 deletions template/cpp/bi_channel.inja
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public:

template <typename T>
void send(T&& snd_msgs,
std::chrono::milliseconds timeout,
const std::chrono::milliseconds& timeout,
std::function<void()> cb) {
std::lock_guard lk(m_mutex);
if (!zmq::send_multipart(m_send, std::forward<decltype(snd_msgs)>(snd_msgs))) {
Expand All @@ -80,7 +80,7 @@ public:
{static_cast<void*>(m_socket), 0, ZMQ_POLLIN | ZMQ_POLLERR, 0},
{static_cast<void*>(m_recv), 0, ZMQ_POLLIN | ZMQ_POLLERR, 0},
};
std::chrono::milliseconds interval(200);
std::chrono::milliseconds interval(100);
std::multimap<std::chrono::system_clock::time_point, std::function<void()>> timeout_task;
while (m_running.load()) {
zmq::poll(items, interval);
Expand Down
3 changes: 2 additions & 1 deletion template/web/bi/src/main.cpp.inja
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void from_json(const nlohmann::json& j, std::optional<T>& v) {
{% for input in func.inputs %}
auto {{input.name}} = request["{{input.name}}"].template get<{{input.type}}>();
{% endfor %}
static std::chrono::milliseconds timeout({{func.web.timeout}});
m_client->{{func.func_name}}(
{{_format_args_name_and_move(func.inputs)}},
[callback]({{_format_args(func.outputs)}}) mutable {
Expand All @@ -68,7 +69,7 @@ void from_json(const nlohmann::json& j, std::optional<T>& v) {
resp->setBody(json.dump());
callback(resp);
},
std::chrono::milliseconds({{func.web.timeout}}),
timeout,
[callback] {
auto resp = drogon::HttpResponse::newHttpResponse(
drogon::HttpStatusCode::k408RequestTimeout,
Expand Down

0 comments on commit 397e233

Please sign in to comment.