From 91aa248210cda785a1582f8e99b7be4b8671da9c Mon Sep 17 00:00:00 2001 From: bochencwx Date: Wed, 1 Nov 2023 19:04:04 +0800 Subject: [PATCH] Feature: Adds an interface to register and intelligently manage the lifecycle of admin handlers for TrpcApp (#71) --- docs/en/admin_service.md | 5 +++-- docs/zh/admin_service.md | 5 +++-- .../features/admin/proxy/forward_server.cc | 4 ++-- trpc/admin/admin_service.h | 3 +++ trpc/common/trpc_app.cc | 16 +++++++++++----- trpc/common/trpc_app.h | 19 ++++++++++++++----- 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/docs/en/admin_service.md b/docs/en/admin_service.md index 08435395..e03c6ee6 100644 --- a/docs/en/admin_service.md +++ b/docs/en/admin_service.md @@ -528,7 +528,8 @@ Usage: /// @param type operation type /// @param url path /// @param handler admin handler - void RegisterCmd(trpc::http::OperationType type, const std::string& url, trpc::AdminHandlerBase* handler); + void RegisterCmd(trpc::http::OperationType type, const std::string& url, + const std::shared_ptr& handler); }; ``` @@ -539,7 +540,7 @@ Usage: public: // Register the commands during business initialization. int Initialize() override { - RegisterCmd(::trpc::http::OperationType::GET, "/myhandler", new MyAdminHandler); + RegisterCmd(::trpc::http::OperationType::GET, "/myhandler", std::make_shared()); } }; ``` diff --git a/docs/zh/admin_service.md b/docs/zh/admin_service.md index e44c57b6..32a7a02f 100644 --- a/docs/zh/admin_service.md +++ b/docs/zh/admin_service.md @@ -529,7 +529,8 @@ tRPC-Cpp允许用户自定义并注册管理命令,完成用户需要的其他 /// @param type operation type /// @param url path /// @param handler admin handler - void RegisterCmd(trpc::http::OperationType type, const std::string& url, trpc::AdminHandlerBase* handler); + void RegisterCmd(trpc::http::OperationType type, const std::string& url, + const std::shared_ptr& handler); }; ``` @@ -540,7 +541,7 @@ tRPC-Cpp允许用户自定义并注册管理命令,完成用户需要的其他 public: // 在业务初始化时进行注册 int Initialize() override { - RegisterCmd(::trpc::http::OperationType::GET, "/myhandler", new MyAdminHandler); + RegisterCmd(::trpc::http::OperationType::GET, "/myhandler", std::make_shared()); } }; ``` diff --git a/examples/features/admin/proxy/forward_server.cc b/examples/features/admin/proxy/forward_server.cc index 64cf2a94..fad104eb 100644 --- a/examples/features/admin/proxy/forward_server.cc +++ b/examples/features/admin/proxy/forward_server.cc @@ -66,8 +66,8 @@ class ForwardServer : public ::trpc::TrpcApp { public: int Initialize() override { // register your own admin handler - RegisterCmd(::trpc::http::OperationType::GET, "/cmds/myhandler1", new MyAdminHandler1); - RegisterCmd(::trpc::http::OperationType::GET, "/cmds/myhandler2", new MyAdminHandler2); + RegisterCmd(::trpc::http::OperationType::GET, "/cmds/myhandler1", std::make_shared()); + RegisterCmd(::trpc::http::OperationType::GET, "/cmds/myhandler2", std::make_shared()); // load custom config CustomConfig custom_config; diff --git a/trpc/admin/admin_service.h b/trpc/admin/admin_service.h index 14b9efdc..69a96d09 100644 --- a/trpc/admin/admin_service.h +++ b/trpc/admin/admin_service.h @@ -34,7 +34,10 @@ class AdminService : public HttpService { static bool BuildServiceAdapterOption(ServiceAdapterOption& option); /// @brief Sets request route for admin commands. + /// @note User need to manually manage the handler object, handler is not freed in AdminService destructor void RegisterCmd(http::OperationType type, const std::string url, AdminHandlerBase* handler); + + /// @brief Sets request route for admin commands. void RegisterCmd(http::OperationType type, const std::string& path, const std::shared_ptr& handler); diff --git a/trpc/common/trpc_app.cc b/trpc/common/trpc_app.cc index 0295c097..2801143a 100644 --- a/trpc/common/trpc_app.cc +++ b/trpc/common/trpc_app.cc @@ -54,9 +54,7 @@ int TrpcApp::Main(int argc, char* argv[]) { return 0; } -void TrpcApp::Wait() { - DestroyRuntime(); -} +void TrpcApp::Wait() { DestroyRuntime(); } void TrpcApp::Terminate() { terminate_.store(true, std::memory_order_release); } @@ -220,8 +218,16 @@ void TrpcApp::RegisterCmd(trpc::http::OperationType type, const std::string& url admin_service->RegisterCmd(type, url, handler); } -void TrpcApp::RegisterConfigUpdateNotifier(const std::string ¬ify_name, - const std::function& notify_cb) { +void TrpcApp::RegisterCmd(trpc::http::OperationType type, const std::string& url, + const std::shared_ptr& handler) { + auto admin_service = server_->GetAdminService(); + TRPC_ASSERT(admin_service != nullptr); + + admin_service->RegisterCmd(type, url, handler); +} + +void TrpcApp::RegisterConfigUpdateNotifier(const std::string& notify_name, + const std::function& notify_cb) { ConfigHelper::GetInstance()->RegisterConfigUpdateNotifier(notify_name, notify_cb); } diff --git a/trpc/common/trpc_app.h b/trpc/common/trpc_app.h index 6dfd6207..9e2afd1a 100644 --- a/trpc/common/trpc_app.h +++ b/trpc/common/trpc_app.h @@ -24,11 +24,11 @@ #include "trpc/common/trpc_version.h" #include "trpc/config/trpc_conf.h" #include "trpc/server/trpc_server.h" -#include "trpc/util/log/logging.h" #include "trpc/util/http/common.h" +#include "trpc/util/log/logging.h" /// @mainpage tRPC-Cpp API -/// +/// /// @brief Primary namespace of tRPC-Cpp. namespace trpc { @@ -112,17 +112,26 @@ class TrpcApp { /// @return true: success; false: failed bool StopService(const std::string& service_name, bool clean_conn = false); - /// @brief Register custom admin command + /// @brief Deprecated: use "RegisterCmd(trpc::http::OperationType type, const std::string& url, + /// const std::shared_ptr& handler)" instead. /// @param type operation type /// @param url path /// @param handler admin handler + /// @note User need to manually manage the handler object, handler is not freed by framework + [[deprecated("use shared_ptr interface for automatic object lifetime management")]] void RegisterCmd(trpc::http::OperationType type, const std::string& url, trpc::AdminHandlerBase* handler); + /// @brief Register custom admin command + /// @param type operation type + /// @param url path + /// @param handler admin handler + void RegisterCmd(trpc::http::OperationType type, const std::string& url, + const std::shared_ptr& handler); + /// @brief Register configuration update callback function /// @param name configuration item /// @param cb callback - void RegisterConfigUpdateNotifier(const std::string& name, - const std::function& cb); + void RegisterConfigUpdateNotifier(const std::string& name, const std::function& cb); protected: // Parsing framework configuration files