Skip to content

Commit

Permalink
Feature: Adds an interface to register and intelligently manage the l…
Browse files Browse the repository at this point in the history
…ifecycle of admin handlers for TrpcApp (#71)
  • Loading branch information
bochencwx authored Nov 1, 2023
1 parent 67af860 commit 91aa248
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
5 changes: 3 additions & 2 deletions docs/en/admin_service.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<trpc::AdminHandlerBase>& handler);
};
```

Expand All @@ -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<MyAdminHandler>());
}
};
```
Expand Down
5 changes: 3 additions & 2 deletions docs/zh/admin_service.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<trpc::AdminHandlerBase>& handler);
};
```

Expand All @@ -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<MyAdminHandler>());
}
};
```
Expand Down
4 changes: 2 additions & 2 deletions examples/features/admin/proxy/forward_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyAdminHandler1>());
RegisterCmd(::trpc::http::OperationType::GET, "/cmds/myhandler2", std::make_shared<MyAdminHandler2>());

// load custom config
CustomConfig custom_config;
Expand Down
3 changes: 3 additions & 0 deletions trpc/admin/admin_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<http::HandlerBase>& handler);

Expand Down
16 changes: 11 additions & 5 deletions trpc/common/trpc_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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); }

Expand Down Expand Up @@ -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 &notify_name,
const std::function<void(const YAML::Node &)>& notify_cb) {
void TrpcApp::RegisterCmd(trpc::http::OperationType type, const std::string& url,
const std::shared_ptr<trpc::AdminHandlerBase>& 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<void(const YAML::Node&)>& notify_cb) {
ConfigHelper::GetInstance()->RegisterConfigUpdateNotifier(notify_name, notify_cb);
}

Expand Down
19 changes: 14 additions & 5 deletions trpc/common/trpc_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<trpc::AdminHandlerBase>& 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<trpc::AdminHandlerBase>& handler);

/// @brief Register configuration update callback function
/// @param name configuration item
/// @param cb callback
void RegisterConfigUpdateNotifier(const std::string& name,
const std::function<void(const YAML::Node&)>& cb);
void RegisterConfigUpdateNotifier(const std::string& name, const std::function<void(const YAML::Node&)>& cb);

protected:
// Parsing framework configuration files
Expand Down

0 comments on commit 91aa248

Please sign in to comment.