From ffb402b633274608a76ba67ee1f42aa5af7a2676 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Thu, 16 Oct 2025 11:08:58 +0800 Subject: [PATCH] update and clean --- README.md | 12 +++++--- tests/test_rest_rpc.cpp | 63 ++++------------------------------------- 2 files changed, 14 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index 5e3d785..975c39d 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,9 @@ int main(){ } ``` -## 发布订阅 +## 酷 +rest_rpc 比较有特色的一个功能是支持订阅发布。 + 以订阅某个topic为例: server 端代码: @@ -140,7 +142,8 @@ void publish() { break; } - co_await server.publish("topic1", str);// 向客户端发布一个string,你也可以发布一个对象,内部会自动序列化 + // 向客户端发布一个string,你也可以发布一个对象,内部会自动序列化 + co_await server.publish("topic1", str); } }; @@ -178,7 +181,9 @@ rest_rpc是目前最快的rpc库,具体和grpc和brpc做了性能对比测试 https://github.com/qicosmos/rest_rpc/tree/master/tests/bench.cpp -## 使用自己的序列化库 +## 活 +rest_rpc 也是比较灵活的,允许用户替换默认的序列化库。 + rest_rpc 默认使用yalantinglibs的struct_pack 去做系列化/反序列化的,它的性能非常好。 rest_rpc 也支持用户使用自己的序列化库,只需要去实现一个序列化和一个反序列化函数。 @@ -216,7 +221,6 @@ rest_rpc 也支持用户使用自己的序列化库,只需要去实现一个 https://github.com/qicosmos/rest_rpc/tree/master/examples - ## 社区和群 purecpp.cn diff --git a/tests/test_rest_rpc.cpp b/tests/test_rest_rpc.cpp index f1a7eb5..1edc3a9 100644 --- a/tests/test_rest_rpc.cpp +++ b/tests/test_rest_rpc.cpp @@ -67,32 +67,6 @@ template asio::awaitable response(auto ctx) { CHECK(ec); } -std::string_view delay_response(std::string_view str) { - rpc_context ctx; - // set_delay before response in another thread - - // std::thread thd([ctx = std::move(ctx)]() mutable { - // std::this_thread::sleep_for(std::chrono::seconds(2)); - // // auto ec = ctx.sync_response("it is from a detached thread"); - // // if (ec) { - // // REST_LOG_ERROR << "response error: " << ec.message(); - // // } - // auto executor = ctx.get_executor(); - // // async_start(executor, response(std::move(ctx))); - // asio::co_spawn(executor, response(std::move(ctx)), - // asio::detached); - // }); - // thd.detach(); - - auto executor = ctx.get_executor(); - asio::co_spawn(executor, response(std::move(ctx)), - asio::detached); - - // this return value is meaningless, because it will response later, the - // return type is important for client, so just return an empty value here. - return ""; -} - std::string echo(std::string str) { return str; } asio::awaitable echo_coro(std::string str) { co_return str; } @@ -123,11 +97,13 @@ asio::awaitable delay_response2(std::string_view str) { }; sync_wait(get_global_executor(), coro()); - rpc_context ctx; + rpc_context ctx; // right, created in io thread. co_await ctx.response(str); auto ret = co_await ctx.response(str); CHECK(ret); - + + // this return value is meaningless, because it will response later, the + // return type is important for client, so just return an empty value here. co_return ""; } @@ -312,7 +288,6 @@ TEST_CASE("test server start") { server.register_handler(); server.register_handler(); server.register_handler(); - server.register_handler(); server.register_handler(); server.register_handler(); server.set_conn_max_age(std::chrono::seconds(5)); @@ -330,21 +305,9 @@ TEST_CASE("test server start") { auto s3 = rpc_codec::pack_args(std::string("test2")); auto s5 = rpc_codec::pack_args(123); - // auto future = asio::co_spawn(cl.get_executor(), - // cl.connect("127.0.0.1:9005"), asio::use_future); auto conn_ec = - // future.get(); auto conn_ec = sync_wait(cl.get_executor(), cl.connect("127.0.0.1:9005")); - // async_start(cl.get_executor(), cl.connect("127.0.0.1:9005"), [](auto - // exptr, auto r){}); async_start(cl.get_executor(), - // cl.connect("127.0.0.1:9005")); auto future = - // async_future(cl.get_executor(), cl.connect("127.0.0.1:9005")); auto ec1 = - // future.get(); - // - // auto future1 = async_future(cl.get_executor(), - // void_returning_coroutine()); future1.get(); - - // auto future1 = asio::co_spawn(cl.get_executor(), cl.call(1, 2), - // asio::use_future); auto result = future1.get(); + CHECK(!conn_ec); + { auto result = sync_wait(cl.get_executor(), cl.call_for(std::chrono::minutes(2), 1, 2)); @@ -364,20 +327,6 @@ TEST_CASE("test server start") { CHECK(result1.value.name == "jack"); } { - // auto result = sync_wait(cl.get_executor(), - // cl.call_for(std::chrono::minutes(2))); - // CHECK(result.ec==rpc_errc::ok); - auto result0 = - sync_wait(cl.get_executor(), - cl.call_for(std::chrono::minutes(2), "test")); - REST_LOG_INFO << make_error_code(result0.ec).message(); - CHECK(result0.ec == rpc_errc::ok); - - result0 = - sync_wait(cl.get_executor(), - cl.call_for(std::chrono::minutes(2), "test")); - CHECK(result0.ec == rpc_errc::ok); - auto result = sync_wait(cl.get_executor(), cl.call_for(std::chrono::minutes(2), "test"));