Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【腾讯犀牛鸟计划】实现过载保护插件 - 固定时间窗口 #162

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build --copt=-O2
#build --copt=-g --strip=never
build --jobs 16
#test --cache_test_results=no --test_output=errors
build --define trpc_include_overload_control=true
1 change: 1 addition & 0 deletions examples/helloworld/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ cc_binary(
":helloworld_proto",
"@com_github_fmtlib_fmt//:fmtlib",
"@trpc_cpp//trpc/common:trpc_app",
"@trpc_cpp//trpc/overload_control/fixedwindow_limiter:fixedwindow_limiter_server_filter",
],
)

Expand Down
3 changes: 2 additions & 1 deletion examples/helloworld/conf/trpc_cpp_fiber.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ server:
network: tcp # Network type, Support two types: tcp/udp
ip: 0.0.0.0 # Service bind ip
port: 12345 # Service bind port

service_filters:
- fixedwindow_limiter
plugins:
log:
default:
Expand Down
7 changes: 7 additions & 0 deletions examples/helloworld/helloworld_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "trpc/common/trpc_app.h"

#include "examples/helloworld/greeter_service.h"
#include "trpc/common/trpc_plugin.h"
#include "trpc/overload_control/fixedwindow_limiter/fixedwindow_limiter_server_filter.h"

namespace test {

Expand All @@ -37,6 +39,11 @@ class HelloWorldServer : public ::trpc::TrpcApp {

return 0;
}
int RegisterPlugins() {
auto server_filter = std::make_shared<trpc::overload_control::FixedTimeWindowServerFilter>();
trpc::TrpcPlugin::GetInstance()->RegisterServerFilter(server_filter);
return 0;
}

void Destroy() override {}
};
Expand Down
11 changes: 7 additions & 4 deletions examples/helloworld/test/fiber_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
DEFINE_string(client_config, "trpc_cpp.yaml", "framework client_config file, --client_config=trpc_cpp.yaml");
DEFINE_string(service_name, "trpc.test.helloworld.Greeter", "callee service name");

int DoRpcCall(const std::shared_ptr<::trpc::test::helloworld::GreeterServiceProxy>& proxy) {
int DoRpcCall(const std::shared_ptr<::trpc::test::helloworld::GreeterServiceProxy>& proxy,int idx) {
::trpc::ClientContextPtr client_ctx = ::trpc::MakeClientContext(proxy);
::trpc::test::helloworld::HelloRequest req;
req.set_msg("fiber");
req.set_msg("fiber, idx="+std::to_string(idx));
// req.set_msg("fiber");
::trpc::test::helloworld::HelloReply rsp;
::trpc::Status status = proxy->SayHello(client_ctx, req, &rsp);
if (!status.OK()) {
Expand All @@ -42,8 +43,10 @@ int DoRpcCall(const std::shared_ptr<::trpc::test::helloworld::GreeterServiceProx

int Run() {
auto proxy = ::trpc::GetTrpcClient()->GetProxy<::trpc::test::helloworld::GreeterServiceProxy>(FLAGS_service_name);

return DoRpcCall(proxy);
for (int i = 1; i <= 30; i++) {
DoRpcCall(proxy,i);
}
return 0;
}

void ParseClientConfig(int argc, char* argv[]) {
Expand Down
10 changes: 10 additions & 0 deletions run_validator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#! /bin/bash


bazel build //trpc/...

bazel build //examples/helloworld/...

./bazel-bin/examples/helloworld/helloworld_svr --config=./examples/helloworld/conf/trpc_cpp_fiber.yaml &
sleep 1
./bazel-bin/examples/helloworld/test/fiber_client --client_config=./examples/helloworld/test/conf/trpc_cpp_fiber.yaml
106 changes: 106 additions & 0 deletions trpc/overload_control/fixedwindow_limiter/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
licenses(["notice"])

package(default_visibility = ["//visibility:public"])

cc_library(
name = "fixedwindow_limiter_conf",
srcs = ["fixedwindow_limiter_conf.cc"],
hdrs = ["fixedwindow_limiter_conf.h"],
defines = [] +
select({
"//trpc:trpc_include_overload_control": ["TRPC_BUILD_INCLUDE_OVERLOAD_CONTROL"],
"//conditions:default": [],
}),
visibility = ["//visibility:public"],
deps = [
"//trpc/log:trpc_log",
"//trpc/overload_control:overload_control_defs",
"@com_github_jbeder_yaml_cpp//:yaml-cpp",
],
)

cc_test(
name = "fixedwindow_limiter_conf_test",
srcs = ["fixedwindow_limiter_conf_test.cc"],
deps = [
":fixedwindow_limiter_conf",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "fixedwindow_overload_controller",
srcs = ["fixedwindow_overload_controller.cc"],
hdrs = ["fixedwindow_overload_controller.h"],
defines = [] +
select({
"//trpc:trpc_include_overload_control": ["TRPC_BUILD_INCLUDE_OVERLOAD_CONTROL"],
"//conditions:default": [],
}),
visibility = ["//visibility:public"],
deps = [
"//trpc/log:trpc_log",
"//trpc/overload_control:overload_control_defs",
"//trpc/util:time",
"//trpc/overload_control:server_overload_controller",
],
)

cc_test(
name = "fixedwindow_overload_controller_test",
srcs = ["fixedwindow_overload_controller_test.cc"],
defines = [] +
select({
"//trpc:trpc_include_overload_control": ["TRPC_BUILD_INCLUDE_OVERLOAD_CONTROL"],
"//conditions:default": [],
}),
visibility = ["//visibility:public"],
deps = [
":fixedwindow_overload_controller",
"//trpc/codec/trpc:trpc_protocol",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "fixedwindow_limiter_server_filter",
srcs = ["fixedwindow_limiter_server_filter.cc"],
hdrs = ["fixedwindow_limiter_server_filter.h"],
defines = [] +
select({
"//trpc:trpc_include_overload_control": ["TRPC_BUILD_INCLUDE_OVERLOAD_CONTROL"],
"//conditions:default": [],
}),
visibility = ["//visibility:public"],
deps = [
":fixedwindow_limiter_conf",
":fixedwindow_overload_controller",
"//trpc/common/config:trpc_config",
"//trpc/filter",
"//trpc/filter:filter_manager",
"//trpc/log:trpc_log",
"//trpc/metrics:trpc_metrics",
"//trpc/overload_control:overload_control_defs",
"//trpc/overload_control/common:report",
"//trpc/runtime/common/stats:frame_stats",
"//trpc/server:server_context",
"//trpc/server:service_impl",
"@com_github_fmtlib_fmt//:fmtlib",
"//trpc/util:ref_ptr",
],
)

cc_test(
name = "fixedwindow_limiter_server_filter_test",
srcs = ["fixedwindow_limiter_server_filter_test.cc"],
data = ["fixedwindow_overload_ctrl.yaml"],
deps = [
":fixedwindow_limiter_server_filter",
"//trpc/codec/testing:protocol_testing",
"//trpc/common:trpc_plugin",
"//trpc/filter:filter_manager",
"//trpc/common/config:trpc_config",
"@com_google_googletest//:gtest_main",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
//
// 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.
//
//

#ifdef TRPC_BUILD_INCLUDE_OVERLOAD_CONTROL

#include "fixedwindow_limiter_conf.h"

#include "trpc/log/trpc_log.h"

namespace trpc::overload_control {

void FixedTimeWindowControlConf::Display() const {
TRPC_FMT_DEBUG("----------FixedWindowLimiterControlConf---------------");

TRPC_FMT_DEBUG("limit: {}", limit);
TRPC_FMT_DEBUG("window_size: {}", window_size);
TRPC_FMT_DEBUG("is_report: {}", is_report);
}

} // namespace trpc::overload_control

namespace YAML {

YAML::Node convert<trpc::overload_control::FixedTimeWindowControlConf>::encode(
const trpc::overload_control::FixedTimeWindowControlConf& config) {
YAML::Node node;

node["limit"] = config.limit;
node["window_size"] = config.window_size;
node["is_report"] = config.is_report;

return node;
}

bool convert<trpc::overload_control::FixedTimeWindowControlConf>::decode(
const YAML::Node& node, trpc::overload_control::FixedTimeWindowControlConf& config) {
if (node["limit"]) {
config.limit = node["limit"].as<uint32_t>();
}
if (node["window_size"]) {
config.window_size = node["window_size"].as<uint32_t>();
}
if (node["is_report"]) {
config.is_report = node["is_report"].as<bool>();
}

return true;
}

} // namespace YAML

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
//
// 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.
//
//

#ifdef TRPC_BUILD_INCLUDE_OVERLOAD_CONTROL

#pragma once

#include "yaml-cpp/yaml.h"

#include "trpc/overload_control/overload_control_defs.h"

namespace trpc::overload_control {

/// @brief Fixed Time Window configuration.
struct FixedTimeWindowControlConf {
uint32_t limit{1000}; ///< The maximum number of requests per window.
uint32_t window_size{1}; ///< The size of the time window in seconds.

bool is_report{false}; ///< Whether to report result to the monitoring plugin

/// @brief Display the value of the configuration field.
void Display() const;
};

} // namespace trpc::overload_control

namespace YAML {

template <>
struct convert<trpc::overload_control::FixedTimeWindowControlConf> {
static YAML::Node encode(const trpc::overload_control::FixedTimeWindowControlConf& config);

static bool decode(const YAML::Node& node, trpc::overload_control::FixedTimeWindowControlConf& config);
};

} // namespace YAML

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
//
// 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.
//
//

#ifdef TRPC_BUILD_INCLUDE_OVERLOAD_CONTROL

#include "fixedwindow_limiter_conf.h"
#include "gtest/gtest.h"

namespace trpc::overload_control::testing {
namespace testing
{



class FixedTimeWindowControlConfTest : public ::testing::Test {
protected:
void SetUp() override {
// 默认的配置,用于测试
default_conf.limit = 500;
default_conf.window_size = 2;
default_conf.is_report = true;
}

trpc::overload_control::FixedTimeWindowControlConf default_conf;
};

TEST_F(FixedTimeWindowControlConfTest, EncodeDecode) {
// 将默认配置编码为 YAML
YAML::Node node = YAML::convert<trpc::overload_control::FixedTimeWindowControlConf>::encode(default_conf);

// 从 YAML 解码回配置
trpc::overload_control::FixedTimeWindowControlConf decoded_conf;
YAML::convert<trpc::overload_control::FixedTimeWindowControlConf>::decode(node, decoded_conf);

// 验证编码和解码结果一致
ASSERT_EQ(default_conf.limit, decoded_conf.limit);
ASSERT_EQ(default_conf.window_size, decoded_conf.window_size);
ASSERT_EQ(default_conf.is_report, decoded_conf.is_report);
}

TEST_F(FixedTimeWindowControlConfTest, LoadFromYAML) {
// Simulate the contents of a YAML configuration file
std::string yaml_content = R"(
plugins:
overload_control:
fixedwindow_limter:
limit: 500
window_size: 2
is_report: true
)";

YAML::Node config = YAML::Load(yaml_content);

trpc::overload_control::FixedTimeWindowControlConf conf;
YAML::convert<trpc::overload_control::FixedTimeWindowControlConf>::decode(config["plugins"]["overload_control"]["fixedwindow_limter"], conf);

ASSERT_EQ(conf.limit, 500);
ASSERT_EQ(conf.window_size, 2);
ASSERT_EQ(conf.is_report, true);
}

} // namespace trpc::testing
} // namespace trpc::overload_control
#endif
Loading
Loading