Skip to content

Commit 91aa248

Browse files
authored
Feature: Adds an interface to register and intelligently manage the lifecycle of admin handlers for TrpcApp (#71)
1 parent 67af860 commit 91aa248

File tree

6 files changed

+36
-16
lines changed

6 files changed

+36
-16
lines changed

docs/en/admin_service.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,8 @@ Usage:
528528
/// @param type operation type
529529
/// @param url path
530530
/// @param handler admin handler
531-
void RegisterCmd(trpc::http::OperationType type, const std::string& url, trpc::AdminHandlerBase* handler);
531+
void RegisterCmd(trpc::http::OperationType type, const std::string& url,
532+
const std::shared_ptr<trpc::AdminHandlerBase>& handler);
532533
};
533534
```
534535

@@ -539,7 +540,7 @@ Usage:
539540
public:
540541
// Register the commands during business initialization.
541542
int Initialize() override {
542-
RegisterCmd(::trpc::http::OperationType::GET, "/myhandler", new MyAdminHandler);
543+
RegisterCmd(::trpc::http::OperationType::GET, "/myhandler", std::make_shared<MyAdminHandler>());
543544
}
544545
};
545546
```

docs/zh/admin_service.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,8 @@ tRPC-Cpp允许用户自定义并注册管理命令,完成用户需要的其他
529529
/// @param type operation type
530530
/// @param url path
531531
/// @param handler admin handler
532-
void RegisterCmd(trpc::http::OperationType type, const std::string& url, trpc::AdminHandlerBase* handler);
532+
void RegisterCmd(trpc::http::OperationType type, const std::string& url,
533+
const std::shared_ptr<trpc::AdminHandlerBase>& handler);
533534
};
534535
```
535536

@@ -540,7 +541,7 @@ tRPC-Cpp允许用户自定义并注册管理命令,完成用户需要的其他
540541
public:
541542
// 在业务初始化时进行注册
542543
int Initialize() override {
543-
RegisterCmd(::trpc::http::OperationType::GET, "/myhandler", new MyAdminHandler);
544+
RegisterCmd(::trpc::http::OperationType::GET, "/myhandler", std::make_shared<MyAdminHandler>());
544545
}
545546
};
546547
```

examples/features/admin/proxy/forward_server.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ class ForwardServer : public ::trpc::TrpcApp {
6666
public:
6767
int Initialize() override {
6868
// register your own admin handler
69-
RegisterCmd(::trpc::http::OperationType::GET, "/cmds/myhandler1", new MyAdminHandler1);
70-
RegisterCmd(::trpc::http::OperationType::GET, "/cmds/myhandler2", new MyAdminHandler2);
69+
RegisterCmd(::trpc::http::OperationType::GET, "/cmds/myhandler1", std::make_shared<MyAdminHandler1>());
70+
RegisterCmd(::trpc::http::OperationType::GET, "/cmds/myhandler2", std::make_shared<MyAdminHandler2>());
7171

7272
// load custom config
7373
CustomConfig custom_config;

trpc/admin/admin_service.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ class AdminService : public HttpService {
3434
static bool BuildServiceAdapterOption(ServiceAdapterOption& option);
3535

3636
/// @brief Sets request route for admin commands.
37+
/// @note User need to manually manage the handler object, handler is not freed in AdminService destructor
3738
void RegisterCmd(http::OperationType type, const std::string url, AdminHandlerBase* handler);
39+
40+
/// @brief Sets request route for admin commands.
3841
void RegisterCmd(http::OperationType type, const std::string& path,
3942
const std::shared_ptr<http::HandlerBase>& handler);
4043

trpc/common/trpc_app.cc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ int TrpcApp::Main(int argc, char* argv[]) {
5454
return 0;
5555
}
5656

57-
void TrpcApp::Wait() {
58-
DestroyRuntime();
59-
}
57+
void TrpcApp::Wait() { DestroyRuntime(); }
6058

6159
void TrpcApp::Terminate() { terminate_.store(true, std::memory_order_release); }
6260

@@ -220,8 +218,16 @@ void TrpcApp::RegisterCmd(trpc::http::OperationType type, const std::string& url
220218
admin_service->RegisterCmd(type, url, handler);
221219
}
222220

223-
void TrpcApp::RegisterConfigUpdateNotifier(const std::string &notify_name,
224-
const std::function<void(const YAML::Node &)>& notify_cb) {
221+
void TrpcApp::RegisterCmd(trpc::http::OperationType type, const std::string& url,
222+
const std::shared_ptr<trpc::AdminHandlerBase>& handler) {
223+
auto admin_service = server_->GetAdminService();
224+
TRPC_ASSERT(admin_service != nullptr);
225+
226+
admin_service->RegisterCmd(type, url, handler);
227+
}
228+
229+
void TrpcApp::RegisterConfigUpdateNotifier(const std::string& notify_name,
230+
const std::function<void(const YAML::Node&)>& notify_cb) {
225231
ConfigHelper::GetInstance()->RegisterConfigUpdateNotifier(notify_name, notify_cb);
226232
}
227233

trpc/common/trpc_app.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
#include "trpc/common/trpc_version.h"
2525
#include "trpc/config/trpc_conf.h"
2626
#include "trpc/server/trpc_server.h"
27-
#include "trpc/util/log/logging.h"
2827
#include "trpc/util/http/common.h"
28+
#include "trpc/util/log/logging.h"
2929

3030
/// @mainpage tRPC-Cpp API
31-
///
31+
///
3232

3333
/// @brief Primary namespace of tRPC-Cpp.
3434
namespace trpc {
@@ -112,17 +112,26 @@ class TrpcApp {
112112
/// @return true: success; false: failed
113113
bool StopService(const std::string& service_name, bool clean_conn = false);
114114

115-
/// @brief Register custom admin command
115+
/// @brief Deprecated: use "RegisterCmd(trpc::http::OperationType type, const std::string& url,
116+
/// const std::shared_ptr<trpc::AdminHandlerBase>& handler)" instead.
116117
/// @param type operation type
117118
/// @param url path
118119
/// @param handler admin handler
120+
/// @note User need to manually manage the handler object, handler is not freed by framework
121+
[[deprecated("use shared_ptr interface for automatic object lifetime management")]]
119122
void RegisterCmd(trpc::http::OperationType type, const std::string& url, trpc::AdminHandlerBase* handler);
120123

124+
/// @brief Register custom admin command
125+
/// @param type operation type
126+
/// @param url path
127+
/// @param handler admin handler
128+
void RegisterCmd(trpc::http::OperationType type, const std::string& url,
129+
const std::shared_ptr<trpc::AdminHandlerBase>& handler);
130+
121131
/// @brief Register configuration update callback function
122132
/// @param name configuration item
123133
/// @param cb callback
124-
void RegisterConfigUpdateNotifier(const std::string& name,
125-
const std::function<void(const YAML::Node&)>& cb);
134+
void RegisterConfigUpdateNotifier(const std::string& name, const std::function<void(const YAML::Node&)>& cb);
126135

127136
protected:
128137
// Parsing framework configuration files

0 commit comments

Comments
 (0)