diff --git a/out/bi_web/include/frpc.hpp b/out/bi_web/include/frpc.hpp index abc1498..fb7844c 100644 --- a/out/bi_web/include/frpc.hpp +++ b/out/bi_web/include/frpc.hpp @@ -394,7 +394,7 @@ class BiChannel final { template void send(T&& snd_msgs, - std::chrono::milliseconds timeout, + const std::chrono::milliseconds& timeout, std::function cb) { std::lock_guard lk(m_mutex); if (!zmq::send_multipart(m_send, std::forward(snd_msgs))) { @@ -411,7 +411,7 @@ class BiChannel final { {static_cast(m_socket), 0, ZMQ_POLLIN | ZMQ_POLLERR, 0}, {static_cast(m_recv), 0, ZMQ_POLLIN | ZMQ_POLLERR, 0}, }; - std::chrono::milliseconds interval(200); + std::chrono::milliseconds interval(100); std::multimap> timeout_task; while (m_running.load()) { zmq::poll(items, interval); @@ -733,7 +733,7 @@ class HelloWorldClient final { void hello_world(BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional date, std::function)> cb, - std::chrono::milliseconds timeout, + const std::chrono::milliseconds& timeout, std::function timeout_cb) { auto req_id = m_req_id.fetch_add(1); auto header = std::make_tuple(req_id, HelloWorldClientHelloWorldServer::hello_world); @@ -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 @@ -786,9 +786,9 @@ class HelloWorldClient final { } template >>)> CompletionToken> - auto hello_world_coro(BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional date, std::chrono::milliseconds timeout, CompletionToken&& token) { + auto hello_world_coro(BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional date, const std::chrono::milliseconds& timeout, CompletionToken&& token) { return asio::async_initiate>>)>( - [this](Handler&& handler, BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional date, auto timeout) mutable { + [this](Handler&& handler, BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional date, const auto& timeout) mutable { auto handler_ptr = std::make_shared(std::move(handler)); this->hello_world( std::move(bank_info), std::move(bank_name), blance, std::move(date), @@ -798,8 +798,8 @@ 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); @@ -807,7 +807,7 @@ class HelloWorldClient final { }); }, 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 diff --git a/out/bi_web/src/main.cpp b/out/bi_web/src/main.cpp index 326f8e8..ecadbf6 100644 --- a/out/bi_web/src/main.cpp +++ b/out/bi_web/src/main.cpp @@ -46,6 +46,7 @@ struct HelloWorldApi final { auto bank_name = request["bank_name"].template get(); auto blance = request["blance"].template get(); auto date = request["date"].template get>(); + 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 date) mutable { @@ -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, diff --git a/out/frpc.hpp b/out/frpc.hpp index abc1498..fb7844c 100644 --- a/out/frpc.hpp +++ b/out/frpc.hpp @@ -394,7 +394,7 @@ class BiChannel final { template void send(T&& snd_msgs, - std::chrono::milliseconds timeout, + const std::chrono::milliseconds& timeout, std::function cb) { std::lock_guard lk(m_mutex); if (!zmq::send_multipart(m_send, std::forward(snd_msgs))) { @@ -411,7 +411,7 @@ class BiChannel final { {static_cast(m_socket), 0, ZMQ_POLLIN | ZMQ_POLLERR, 0}, {static_cast(m_recv), 0, ZMQ_POLLIN | ZMQ_POLLERR, 0}, }; - std::chrono::milliseconds interval(200); + std::chrono::milliseconds interval(100); std::multimap> timeout_task; while (m_running.load()) { zmq::poll(items, interval); @@ -733,7 +733,7 @@ class HelloWorldClient final { void hello_world(BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional date, std::function)> cb, - std::chrono::milliseconds timeout, + const std::chrono::milliseconds& timeout, std::function timeout_cb) { auto req_id = m_req_id.fetch_add(1); auto header = std::make_tuple(req_id, HelloWorldClientHelloWorldServer::hello_world); @@ -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 @@ -786,9 +786,9 @@ class HelloWorldClient final { } template >>)> CompletionToken> - auto hello_world_coro(BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional date, std::chrono::milliseconds timeout, CompletionToken&& token) { + auto hello_world_coro(BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional date, const std::chrono::milliseconds& timeout, CompletionToken&& token) { return asio::async_initiate>>)>( - [this](Handler&& handler, BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional date, auto timeout) mutable { + [this](Handler&& handler, BankInfo bank_info, std::string bank_name, uint64_t blance, std::optional date, const auto& timeout) mutable { auto handler_ptr = std::make_shared(std::move(handler)); this->hello_world( std::move(bank_info), std::move(bank_name), blance, std::move(date), @@ -798,8 +798,8 @@ 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); @@ -807,7 +807,7 @@ class HelloWorldClient final { }); }, 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 diff --git a/template/cpp/bi.inja b/template/cpp/bi.inja index 19e0612..3314062 100644 --- a/template/cpp/bi.inja +++ b/template/cpp/bi.inja @@ -53,7 +53,7 @@ public: void {{func.func_name}}({{_format_args(func.inputs)}}, std::function cb, - std::chrono::milliseconds timeout, + const std::chrono::milliseconds& timeout, std::function 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}}); @@ -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 @@ -106,9 +106,9 @@ public: } template >)> 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>)>( - [this](Handler&& handler, {{_format_args(func.inputs)}}, auto timeout) mutable { + [this](Handler&& handler, {{_format_args(func.inputs)}}, const auto& timeout) mutable { auto handler_ptr = std::make_shared(std::move(handler)); this->{{func.func_name}}( {{_format_args_name_and_move(func.inputs)}}, @@ -118,8 +118,8 @@ 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); @@ -127,7 +127,7 @@ public: }); }, token, - {{_format_args_name_and_move(func.inputs)}}, std::move(timeout)); + {{_format_args_name_and_move(func.inputs)}}, timeout); } #endif diff --git a/template/cpp/bi_channel.inja b/template/cpp/bi_channel.inja index aca9760..b6ba2a1 100644 --- a/template/cpp/bi_channel.inja +++ b/template/cpp/bi_channel.inja @@ -63,7 +63,7 @@ public: template void send(T&& snd_msgs, - std::chrono::milliseconds timeout, + const std::chrono::milliseconds& timeout, std::function cb) { std::lock_guard lk(m_mutex); if (!zmq::send_multipart(m_send, std::forward(snd_msgs))) { @@ -80,7 +80,7 @@ public: {static_cast(m_socket), 0, ZMQ_POLLIN | ZMQ_POLLERR, 0}, {static_cast(m_recv), 0, ZMQ_POLLIN | ZMQ_POLLERR, 0}, }; - std::chrono::milliseconds interval(200); + std::chrono::milliseconds interval(100); std::multimap> timeout_task; while (m_running.load()) { zmq::poll(items, interval); diff --git a/template/web/bi/src/main.cpp.inja b/template/web/bi/src/main.cpp.inja index 26d39c6..564f2c1 100644 --- a/template/web/bi/src/main.cpp.inja +++ b/template/web/bi/src/main.cpp.inja @@ -57,6 +57,7 @@ void from_json(const nlohmann::json& j, std::optional& 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 { @@ -68,7 +69,7 @@ void from_json(const nlohmann::json& j, std::optional& v) { resp->setBody(json.dump()); callback(resp); }, - std::chrono::milliseconds({{func.web.timeout}}), + timeout, [callback] { auto resp = drogon::HttpResponse::newHttpResponse( drogon::HttpStatusCode::k408RequestTimeout,