Skip to content

Commit c3ac767

Browse files
authored
Docs: Add overload control for fiber limiter (#38)
1 parent 5f6ed20 commit c3ac767

12 files changed

Lines changed: 299 additions & 28 deletions

docs/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ English | [中文](README.zh_CN.md)
4242
* Filter
4343
* [custom filter](./en/filter.md)
4444
* Flow-Control & Overload-Protect
45-
* [concurrency limiter](./en/overload_control_concurrency_limiter.md)
45+
* [concurrency requests limiter plugin](./en/overload_control_concurrency_limiter.md)
46+
* [concurrency fibers limiter plugin](./en/overload_control_filter_limiter.md)
4647
* Naming
4748
* [custom naming plugin](./en/custom_naming.md)
4849
* [mesh-polaris](https://github.com/trpc-ecosystem/cpp-naming-polarismesh/blob/main/README.md)

docs/README.zh_CN.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
* 拦截器
4242
* [开发自定义拦截器](./zh/filter.md)
4343
* 流控和过载保护
44-
* [基于并发请求的过载保护](./zh/overload_control_concurrency_limiter.md)
44+
* [基于并发请求的过载保护插件](./zh/overload_control_concurrency_limiter.md)
45+
* [基于并发 Fiber 个数的过载保护插件](./zh/overload_control_filter_limiter.md)
4546
* Naming插件
4647
* [开发自定义naming插件](./zh/custom_naming.md)
4748
* [mesh-polaris](https://github.com/trpc-ecosystem/cpp-naming-polarismesh/blob/main/README.zh_CN.md)

docs/en/overload_control_concurrency_limiter.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ build --define trpc_include_overload_control=true
3939

4040
## Configuration file
4141

42-
Concurrent request filter configuration. For detailed configuration reference, please refer to the [concurrency_overload_ctrl.yaml](../../trpc/overload_control/concurrency_limiter/concurrency_overload_ctrl.yaml)
42+
The configuration for the concurrent request filter is as follows(For detailed configuration reference, please refer to the [concurrency_overload_ctrl.yaml](../../trpc/overload_control/concurrency_limiter/concurrency_overload_ctrl.yaml)):
4343

4444
```yaml
4545
#Server configuration
@@ -74,9 +74,11 @@ The key points of the configuration are as follows:
7474
- concurrency_limiter: Name of the concurrent request overload protection filter
7575
- max_concurrency: Maximum concurrent requests configured for the user. When the current concurrent requests are greater than or equal to this value, the requests will be intercepted.
7676
- is_report: Whether to report monitoring data to the monitoring plugin. **Note that this configuration must be used together with a monitoring plugin (for example, configuring plugins->metrics->prometheus will report to Prometheus). If no monitoring plugin is configured, this option is meaningless**. The monitored data includes:
77-
`max_concurrency`: Reported maximum concurrent request number in the configuration, used to check if the configuration is consistent with the program's execution.
78-
`current_concurrency`: Current concurrent request number
79-
`/{callee_name}/{method}`: Monitoring name format, composed of the callee service (`callee_name`) and method name (`method`), such as: `/trpc.test.helloworld.Greeter/SayHello`.
77+
- `max_concurrency`: The maximum concurrent request number for reporting configuration is a fixed value used to check if the maximum request concurrency limit set in the program is effective.
78+
- `current_concurrency`: The current concurrent request number for reporting is a dynamic value that represents the number of requests being processed concurrently at a given moment.
79+
- `/{callee_name}/{method}`: The monitored RPC method name for reporting is a fixed value and is composed of the callee service name (callee_name) and the method name (method). For example: `/trpc.test.helloworld.Greeter/SayHello`.
80+
- `Pass`:The pass status for an individual request: 0 means intercepted, and 1 means passed.
81+
- `Limited`:The interception status for an individual request: 1 means intercepted, and 0 means passed. It is the opposite of the `Pass` monitoring attribute mentioned above.
8082

8183
# FAQ
8284

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
[中文](../zh/overload_control_filter_limiter.md)
2+
3+
# Overview
4+
5+
In the tRPC-Cpp framework, under the `Fiber (M:N)` thread model (refer to the [Fiber User Guide](./fiber_user_guide.md)), when resource exhaustion occurs in high-concurrency scenarios, it can lead to program freezing. Therefore, it is necessary to impose limits on the number of Fibers to prevent program freezing. This article primarily introduces an overload protection plugin based on the number of Fibers in concurrency.
6+
7+
# Principle
8+
9+
## Principle diagram of overload protection based on the number of Fibers in concurrency
10+
11+
![fiber_limiter](../images/fiber_limiter.png)
12+
The core point in the diagram is the `FiberLimiter`, which is primarily responsible for comparing the current number of concurrent Fibers (`curr_fiber_count`) in the framework with the maximum configured number of concurrent Fibers (`max_fiber_count`). Its main purpose is to determine whether to reject processing the current request (`curr_req`).
13+
14+
## Implementation code
15+
16+
The current `FiberLimiter` supports **both the client and the server**. Since the framework's overload protection is implemented based on filters (refer to [filter]((./filter.md))), in order to ensure flow control protection as early as possible, this filter is placed before the request is enqueued. The specific implementation of this filter is as follows:
17+
18+
```cpp
19+
// Client side
20+
std::vector<FilterPoint> FiberLimiterClientFilter::GetFilterPoint() {
21+
return {
22+
FilterPoint::CLIENT_PRE_SEND_MSG,
23+
...
24+
};
25+
}
26+
27+
// Server side
28+
std::vector<FilterPoint> FiberLimiterServerFilter::GetFilterPoint() {
29+
return {
30+
FilterPoint::SERVER_PRE_SCHED_RECV_MSG,
31+
...
32+
};
33+
}
34+
```
35+
36+
Source code reference: [fiber_limiter](../../trpc/overload_control/fiber_limiter/)
37+
38+
# Usage example
39+
40+
The overload protection filter based on the number of Fibers in concurrency supports both the client and the server. It must be used in the Fiber thread model. Users do not need to modify any code; they only need to modify the configuration, making it very convenient to use.
41+
42+
## Compilation options
43+
44+
Add the following line to the `.bazelrc` file.
45+
46+
```sh
47+
build --define trpc_include_overload_control=true
48+
```
49+
50+
## Configuration file
51+
52+
The configuration for the Fiber concurrency filter is as follows (for detailed configuration, refer to [fibers_overload_ctrl.yaml](../../trpc/overload_control/fiber_limiter/fibers_overload_ctrl.yaml))
53+
54+
```yaml
55+
#Global configuration (required)
56+
global:
57+
local_ip: 0.0.0.0 #Local IP, used for: not affecting the normal operation of the framework, used to obtain the local IP from the framework configuration.
58+
coroutine: #Coroutine configuration.
59+
enable: true #false: means not using coroutine; true: means using coroutine.
60+
threadmodel:
61+
fiber:
62+
- instance_name: fiber_instance
63+
concurrency_hint: 1
64+
scheduling_group_size: 1
65+
reactor_num_per_scheduling_group: 1
66+
67+
#Server configuration
68+
server:
69+
app: test #Business name
70+
server: route #Module name of the business
71+
admin_port: 18888 # Admin port
72+
admin_ip: 0.0.0.0 #Admin ip
73+
service: #Business service, can have multiple.
74+
- name: trpc.test.route.Forward #Service name, needs to be filled in according to the format, the first field is default to trpc, the second and third fields are the app and server configurations above, and the fourth field is the user-defined service_name.
75+
network: tcp #Network listening type: for example: TCP, UDP.
76+
ip: 0.0.0.0 #Listen ip
77+
port: 11112 #Listen port
78+
protocol: trpc #Service application layer protocol, for example: trpc, http.
79+
accept_thread_num: 1 #Number of threads for binding ports.
80+
filter:
81+
- fiber_limiter # Name of filter for this service
82+
83+
#Client configuration.
84+
client:
85+
service:
86+
- name: trpc.test.helloworld.Greeter
87+
target: 127.0.0.1:32345
88+
protocol: trpc #Service application layer protocol, for example: trpc, http.
89+
network: tcp #Network listening type: for example: TCP, UDP.
90+
selector_name: direct #Name service used for route selection, "direct" for direct connection.
91+
filter:
92+
- fiber_limiter # Name of filter for this client
93+
94+
#Plugin configuration.
95+
plugins:
96+
# metrics:
97+
# prometheus:
98+
# ...
99+
overload_control:
100+
fiber_limiter:
101+
max_fiber_count: 20 # Maximum number of fibers. Here, for unit testing purposes, it is set relatively small. In normal business scenarios, this value should be much larger than 20.
102+
is_report: true # Whether to report overload information.
103+
# ...
104+
```
105+
106+
The key points of the configuration are as follows:
107+
108+
- fiber_limiter: The name of the overload protection for Fiber concurrency.
109+
- max_fiber_count: The maximum number of concurrent requests configured by the user. When the current concurrent requests are greater than or equal to this value, the requests will be intercepted.
110+
- is_report: Whether to report monitoring data to the monitoring plugin. **Note that this configuration must be used together with a monitoring plugin (for example, configuring plugins->metrics->prometheus will report to Prometheus). If no monitoring plugin is configured, this option is meaningless**. The monitored data includes:
111+
- `max_fiber_count`: Reports the maximum Fiber concurrency configured by the user. It is a fixed value used to check if the configured maximum request concurrency is effective in the program.
112+
- `fibers_count`: Reports the current number of concurrent Fibers in the system.
113+
- `/{callee_name}/{method}`: The format for reporting RPC method monitoring name. It is a fixed value and consists of the callee service (callee_name) and the method name (method). For example: /trpc.test.helloworld.Greeter/SayHello. This format is **only effective for server-side rate limiting**.
114+
- `/{ip}/{port}`: The format for reporting monitoring items based on the IP and port of the callee service. For example: /127.0.0.1/22345. This format is **only effective for client-side rate limiting**. In the case of a client, when there are multiple nodes for the downstream service, it is not possible to differentiate the monitoring information for different nodes based on the callee service RPC. Therefore, the monitoring item is based on the combination of IP and port.
115+
- `Pass`:The pass status for an individual request: 0 means intercepted, and 1 means passed.
116+
- `Limited`:The interception status for an individual request: 1 means intercepted, and 0 means passed. It is the opposite of the `Pass` monitoring attribute mentioned above.
117+
118+
# FAQ
119+
120+
## Why is the Fiber overload protection filter not effective even after configuration?
121+
122+
Check if the Fiber thread model is being used.
123+
124+
## What are differences between overload protectors based on concurrent requests and overload protectors based on Fiber concurrency count?
125+
126+
In the tRPC-Cpp framework, the Fiber scheduling group is a global configuration. Therefore, in a relay scenario, whether acting as a client or a server, the same Fiber concurrency count can be obtained, allowing for rate limiting. However, the overload protector based on concurrent requests calculates the current concurrent requests based on the received requests. It is not reasonable to use this value when accessing backend nodes as a client. Therefore, the Fiber concurrency count overload protector supports both server-side and client-side, while the request concurrency overload protector is only applied to the server-side.
127+
128+
## Why is the concurrency still limited even when the number of concurrent requests is less than the maximum parallelism of Fiber?
129+
130+
The number of concurrent requests is not equivalent to the current parallelism of Fiber because some requests may be processed using multiple Fibers.

docs/en/plugin_management.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ The framework takes inspiration from Java's Aspect-Oriented Programming (AOP) pa
5555
* Flow-Control & Overload-Protect
5656

5757
It provides the ability for flow controlling and overload protecting. For more details, please refer to the **documentation of customizing Flow-Control & Overload-Protect plugin**.
58-
* [overload_control_concurrency_limiter](./overload_control_concurrency_limiter.md)
58+
* [Concurrent requests limiter](./overload_control_concurrency_limiter.md)
59+
* [Concurrent fibers limiter](./overload_control_filter_limiter.md)
5960

6061
## Other features
6162

docs/en/server_guide.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,5 @@ See [Transparent proxy](transparent_service.md).
239239

240240
### Flow-Control & Overload-Protect
241241

242-
* See [overload_control_concurrency_limiter](./overload_control_concurrency_limiter.md)
242+
* See [Concurrent requests limiter](./overload_control_concurrency_limiter.md)
243+
* See [Concurrent fibers limiter](./overload_control_filter_limiter.md)

docs/images/fiber_limiter.png

140 KB
Loading

docs/zh/fiber_user_guide.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
# 前言
55

6-
本文从开发者角度介绍如何使用Fiber,包括配置、常用类及接口、常见问题等几部分。关于Fiber原理可以阅读[Fiber](./fiber.md)
6+
本文从开发者角度介绍如何使用 Fiber,包括配置、常用类及接口、常见问题等几部分。关于 Fiber 原理可以阅读[Fiber](./fiber.md)
77

88
# 配置
99

10-
目前Fiber的功能需要在Fiber执行环境中运行,有Fiber执行环境第一步就需要正确的Fiber配置,这里只展示Fiber部分的配置。
10+
目前 Fiber 的功能需要在 Fiber 执行环境中运行,有 Fiber 执行环境第一步就需要正确的 Fiber 配置,这里只展示Fiber部分的配置。
1111

12-
为简化使用,开发者只需要填写一些必填的配置项就即可,以下是精简Fiber配置:
12+
为简化使用,开发者只需要填写一些必填的配置项就即可,以下是精简 Fiber 配置:
1313

1414
```yaml
1515
global:
@@ -20,9 +20,9 @@ global:
2020
xxx
2121
```
2222

23-
其中推荐手动填入concurrency_hint配置项,避免出现因读取系统配置而导致的问题。
23+
其中推荐手动填入 `concurrency_hint` 配置项,避免出现因读取系统配置而导致的问题。
2424

25-
除了精简版配置,还可以自定义高阶配置项,以下是完整Fiber配置:
25+
除了精简版配置,还可以自定义高阶配置项,以下是完整 Fiber 配置:
2626

2727
```yaml
2828
global:
@@ -60,7 +60,7 @@ global:
6060
bool StartFiberDetached(Fiber::Attributes&& attrs, Function<void()>&& start_proc);
6161
```
6262

63-
创建 Fiber 默认的运行属性是由框架发起调度(可能会在其他调度组)并执行,当然也可以自定义 Fiber 运行属性Fiber::Attributes:
63+
创建 Fiber 默认的运行属性是由框架发起调度(可能会在其他调度组)并执行,当然也可以自定义 Fiber 运行属性 Fiber::Attributes:
6464

6565
```cpp
6666
struct Attributes {
@@ -84,7 +84,7 @@ global:
8484
});
8585
```
8686

87-
也可以指定Fiber::Attributes,如指定Fiber在某个调度组中运行
87+
也可以指定 Fiber::Attributes,如指定 Fiber 在某个调度组中运行
8888

8989
```cpp
9090
trpc::Fiber::Attributes attr;
@@ -126,7 +126,7 @@ global:
126126

127127
## Fiber 间互斥访问
128128

129-
用于 Fiber 之间共享数据互斥访问FiberMutex,示例如:
129+
用于 Fiber 之间共享数据互斥访问 FiberMutex,示例如:
130130

131131
```cpp
132132
trpc::FiberLatch latch(10);
@@ -162,7 +162,7 @@ latch.wait();
162162
163163
## 共享数据读优先
164164
165-
用于 Fiber 之间共享数据读优先读写锁:FiberSharedMutex,示例如:
165+
用于 Fiber 之间共享数据读优先读写锁: FiberSharedMutex,示例如:
166166
167167
```cpp
168168
trpc::FiberSharedMutex rwlock_;
@@ -248,7 +248,7 @@ void Update(const std::string& key, const std::string& value){
248248

249249
## 定时任务
250250

251-
有需要再Fiber中执行定时任务的需求,目前提供以下几种方式:
251+
有需要在 Fiber 中执行定时任务的需求,目前提供以下几种方式:
252252

253253
1. SetFiberTimer 创建并启用 KillFiberTimer 手动释放:
254254

@@ -342,5 +342,6 @@ trpc::StartFiberDetached([&] {
342342

343343
```
344344
345-
# 4 FAQ
345+
# FAQ
346+
346347
查阅[Fiber FAQ](./faq/fiber_problem.md)

docs/zh/overload_control_concurrency_limiter.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
# 前言
44

5-
tRPC-Cpp框架应用于高并发的场景中,需要进行过载保护 ,以防业务程序出现不可预期的错误;虽然框架已经实现基于服务和方法的流控插件进行限流,但是该插件是运行在请求入队之后,不能及早进行过载保护; 本文主要介绍了一种基于并发请求量的过载保护插件,该插件运行在请求入队之前,实现策略简单易懂。
5+
tRPC-Cpp 框架应用于高并发的场景中,需要进行过载保护 ,以防业务程序出现不可预期的错误;虽然框架已经实现基于服务和方法的流控插件进行限流,但是该插件是运行在请求入队之后,不能及早进行过载保护; 本文主要介绍了一种基于并发请求量的过载保护插件,该插件运行在请求入队之前,实现策略简单易懂。
66

77
# 原理
88

99
## 基于并发请求的过载保护原理图
1010

1111
![concurrency_limiter](../images/concurrency_limiter.png)
12-
图中核心点就是`ConcurrencyLimiter` ,它的主要作用是获取框架当前的并发请求数(`concurrency_reqs`)与用户配置的最大并发数(`max_concurrency`)进行比较来判断是否拒绝当前接收的新请求(`curr_req`),逻辑很简单。
12+
图中核心点就是 `ConcurrencyLimiter` ,它的主要作用是获取框架当前的并发请求数(`concurrency_reqs`)与用户配置的最大并发数(`max_concurrency`)进行比较来判断是否拒绝当前接收的新请求(`curr_req`),逻辑很简单。
1313

1414
## 实现代码
1515

@@ -40,7 +40,7 @@ build --define trpc_include_overload_control=true
4040

4141
## 配置文件
4242

43-
并发请求过滤器配置,详细配置参考:[concurrency_overload_ctrl.yaml](../../trpc/overload_control/concurrency_limiter/concurrency_overload_ctrl.yaml)
43+
并发请求过滤器配置如下(详细配置参考:[concurrency_overload_ctrl.yaml](../../trpc/overload_control/concurrency_limiter/concurrency_overload_ctrl.yaml)):
4444

4545
```yaml
4646
#Server configuration
@@ -75,13 +75,15 @@ plugins:
7575
- concurrency_limiter:并发请求过载保护器的名称
7676
- max_concurrency:为用户配置的最大并发请求数,当当前并发请求大于等于该值的时候,会拦截请求
7777
- is_report:是否上报监控数据到监控插件,**注意,该配置必须与监控插件一起使用(例如配置:plugins->metrics->prometheus,则会上报到 prometheus 上),如果没有配置监控插件,该选项无意义**,被监控数据有:
78-
`max_concurrency`: 上报配置的最大并发请求数,用于检查配置是否和程序运行的一致
79-
`current_concurrency`: 当前并发请求数
80-
`/{callee_name}/{method}`: 监控名称格式,由被调服务(callee_name)和方法名(method)组成,例如:`/trpc.test.helloworld.Greeter/SayHello`。
78+
- `max_concurrency`: 上报配置的最大并发请求数,属于固定值,用于检查配置的最大请求并发数是否在程序中生效
79+
- `current_concurrency`: 上报当前并发请求数,属于动态值
80+
- `/{callee_name}/{method}`: 上报监控的 RPC 方法名,属于固定值;由被调服务(callee_name)和方法名(method)组成,例如:`/trpc.test.helloworld.Greeter/SayHello`。
81+
- `Pass`:单个请求的通过状态,0:拦截;1:通过
82+
- `Limited`:单个请求的拦截状态,1:拦截;0:通过。与上面的 `Pass` 监控属性是相反的
8183

8284
# FAQ
8385

84-
## Q1:按照配置配置后,并发过载保护器为什么依旧不生效?
86+
## 按照配置配置后,并发过载保护器为什么依旧不生效?
8587

8688
根据并发过载保护过滤器的实现原理,是根据当前并发请求与最大并发请求进行比较,其中当前并发请求数是根据服务端上下文的构造函数和析构函数进行增减的,如下(参考:[server_context](../../trpc/server/server_context.cc)):
8789

0 commit comments

Comments
 (0)