forked from trpc-group/trpc-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrpc_plugin.h
212 lines (160 loc) · 6.58 KB
/
trpc_plugin.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
#pragma once
#include <memory>
#include <mutex>
#include <string>
#include <utility>
#include <vector>
#include "trpc/auth/auth.h"
#include "trpc/codec/client_codec.h"
#include "trpc/codec/server_codec.h"
#include "trpc/compressor/compressor.h"
#include "trpc/config/codec.h"
#include "trpc/config/config.h"
#include "trpc/config/provider.h"
#include "trpc/filter/filter.h"
#include "trpc/metrics/metrics.h"
#include "trpc/naming/limiter.h"
#include "trpc/naming/load_balance.h"
#include "trpc/naming/registry.h"
#include "trpc/naming/selector.h"
#include "trpc/serialization/serialization.h"
#include "trpc/telemetry/telemetry.h"
#include "trpc/tracing/tracing.h"
namespace trpc {
/// @brief Plugin management class
class TrpcPlugin {
public:
static TrpcPlugin* GetInstance() {
static TrpcPlugin instance;
return &instance;
}
/////////////////////////////////////
/// @brief Install plugin
int RegisterPlugins();
/// @brief Uninstall plugin
int UnregisterPlugins();
/////////////////////////////////////
// The interface for registering custom codec/serialization/compress plugin
/// @brief Register custom server codec plugin
bool RegisterServerCodec(const ServerCodecPtr& codec);
/// @brief Register custom client codec plugin
bool RegisterClientCodec(const ClientCodecPtr& codec);
/// @brief Register custom compress plugin
bool RegisterCompress(const compressor::CompressorPtr& compressor);
/// @brief Register custom serialization plugin
bool RegisterSerialization(const serialization::SerializationPtr& serialization);
/////////////////////////////////////
// The interface for registering custom filter
/// @brief Register custom server filter
bool RegisterServerFilter(const MessageServerFilterPtr& filter);
/// @brief Register custom client filter
bool RegisterClientFilter(const MessageClientFilterPtr& filter);
/////////////////////////////////////
// The interface for registering custom registry/selector/loadbalance/limiter plugin
/// @brief Register custom server registry plugin
bool RegisterRegistry(const RegistryPtr& registry);
/// @brief Register custom client selector plugin
bool RegisterSelector(const SelectorPtr& selector);
/// @brief Register custom client loadbalance plugin
/// In design, different selectors do not reuse the same loadbalance object.
/// If two selectors use the same loadbalance algorithm, you need to create and register two loadbalance plugins.
/// It is recommended that the name start with the selector name corresponding to `loadbalance name` to distinguish
bool RegisterLoadBalance(const LoadBalancePtr& load_balance);
/// @brief Register custom limiter plugin
bool RegisterLimiter(const LimiterPtr& limiter);
/////////////////////////////////////
/// @brief Register custom metrics plugin
bool RegisterMetrics(const MetricsPtr& metrics);
/////////////////////////////////////
/// @brief Register custom config-codec plugin
bool RegisterConfigCodec(const config::CodecPtr& codec);
/// @brief Register custom config-provider plugin
bool RegisterConfigProvider(const config::ProviderPtr& provider);
/// @brief Register custom config-center plugin(has deprecated)
bool RegisterConfig(const ConfigPtr& config);
/////////////////////////////////////
/// @brief Register the remote logging plugin
bool RegisterLogging(const LoggingPtr& logging);
/////////////////////////////////////
/// @brief Register custom tracing plugin
bool RegisterTracing(const TracingPtr& tracing);
/// @brief Register custom telemetry plugin
bool RegisterTelemetry(const TelemetryPtr& telemetry);
/////////////////////////////////////
/// @brief Register custom auth plugin
bool RegisterAuth(const AuthPtr& auth);
/// @brief Register custom auth-follower plugin
bool RegisterAuthFollower(const AuthCenterFollowerPtr& follower);
/////////////////////////////////////
/// @brief Whether the plugin register/unregister is invoked by framework
/// @note This interface only used by framework
void SetInvokeByFramework();
/// @brief Destroy plugin-related resources
/// @note This interface only used by framework
void DestroyResource();
private:
struct PluginInfo {
PluginPtr plugin;
bool is_inited{false};
bool is_started{false};
bool is_stoped{false};
bool is_destroyed{false};
};
TrpcPlugin() = default;
TrpcPlugin(const TrpcPlugin&) = delete;
TrpcPlugin& operator=(const TrpcPlugin&) = delete;
void CollectPlugins();
void InitPlugins();
void InitPlugin(PluginInfo& plugin_info);
void StartPlugins();
void StartPlugin(PluginInfo& plugin_info);
void StopPlugins();
void StopPlugin(PluginInfo& plugin_info);
void DestroyPlugins();
void DestroyPlugin(PluginInfo& plugin_info);
std::unordered_map<std::string, PluginInfo>::iterator FindPlugin(const std::string& name);
bool IsDepPluginNameValid(const std::vector<std::string>& dep_plugin_names);
template <typename T>
bool AddPlugins(const std::unordered_map<std::string, T>& plugins) {
for (const auto& it : plugins) {
PluginInfo plugin_info;
plugin_info.plugin = it.second;
plugin_info.is_inited = false;
plugin_info.is_destroyed = false;
std::string plugin_name = it.second->Name() + "#" + std::to_string(static_cast<int>(it.second->Type()));
plugins_.emplace(plugin_name, std::move(plugin_info));
std::vector<std::string> dep_plugin_names;
it.second->GetDependencies(dep_plugin_names);
// Check if the dependent plugin name is valid
TRPC_ASSERT(IsDepPluginNameValid(dep_plugin_names) && "Dependent plugin name invalid");
for (auto& dep_plugin_name : dep_plugin_names) {
plugins_reverse_deps_[dep_plugin_name].push_back(plugin_name);
}
}
return true;
}
bool InitRuntime_();
void DestroyRuntime_();
private:
std::mutex mutex_;
std::unordered_map<std::string, PluginInfo> plugins_;
// The reverse dependency relationship of the plugins.
std::unordered_map<std::string, std::vector<std::string>> plugins_reverse_deps_;
bool is_all_inited_{false};
bool is_all_destroyed_{false};
// `RegisterPlugins`/`RegisterPlugins` is invoked by framework
bool is_invoke_by_framework_{false};
};
} // namespace trpc