Skip to content

Commit 36d14b4

Browse files
yujun411522kiviyu
authored andcommitted
Feature: During graceful shutdown, more precise control over the readability and writability events of connections to reduce timeouts
1 parent 00af5f2 commit 36d14b4

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

docs/en/architecture_design.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,17 @@ The request processing generally includes the following processes:
9393

9494
The service exit process generally includes the following processes:
9595

96-
1. Wait for all requests received by the server to be processed(To avoid being unable to exit due to waiting, the default maximum waiting time is set to 5 seconds.It can be configured through server::stop_max_wait_time);
97-
2. Stop the listening and reading events of the server network connection;
98-
3. Close the server network connections;
99-
4. Call the `Destroy` method of the `TrpcApp` business subclass to stop the dynamic resources created by the business (for example: started threads);
100-
5. Stop the dynamic resources created by the plugins (for example: threads started inside the plugins);
101-
6. Stop the framework runtime environment runtime;
102-
7. Release the resources inside the runtime environment of the framework;
103-
8. Release the internal resources of the framework operating environment TrpcServer;
104-
9. Release the internal resources of the framework operating environment TrpcClient;
105-
10. The program exits;
96+
1. Unregister all services from the name service to prevent future traffic from being routed to this node;
97+
2. Disable the readable event for connections, including:a. Disable the readable event for the listening socket to avoid creating new connections,b. Disable the readable event for the connected socket to avoid receiving new requests from that socket;
98+
3. Wait for all requests received by the server to be processed(To avoid being unable to exit due to waiting, the default maximum waiting time is set to 5 seconds.It can be configured through server::stop_max_wait_time);
99+
4. Close the server network connections;
100+
5. Call the `Destroy` method of the `TrpcApp` business subclass to stop the dynamic resources created by the business (for example: started threads);
101+
6. Stop the dynamic resources created by the plugins (for example: threads started inside the plugins);
102+
7. Stop the framework runtime environment runtime;
103+
8. Release the resources inside the runtime environment of the framework;
104+
9. Release the internal resources of the framework operating environment TrpcServer;
105+
10. Release the internal resources of the framework operating environment TrpcClient;
106+
11. The program exits;
106107

107108
## Client
108109

docs/zh/architecture_design.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,17 @@ Server 启动过程, 大致包括以下流程:
9292

9393
服务退出过程, 大致包括以下流程:
9494

95-
1. 等待服务端接收的请求都处理完成(为避免因等待无法退出,默认最多等待5秒钟,可通过yaml中的server::max_wait_time配置);
96-
2. 停止服务端网络连接的监听和读事件;
97-
3. 关闭服务端网络连接;
98-
4. 调用 `TrpcApp` 业务子类的 `Destroy` 方法, 停止业务创建的动态资源(比如: 起的线程);
99-
5. 停止插件创建的动态资源(比如: 插件内部起的线程);
100-
6. 停止框架运行环境runtime;
101-
7. 释放框架运行环境 runtime 内部的资源;
102-
8. 释放框架运行环境 TrpcServer 内部的资源;
103-
9. 释放框架运行环境 TrpcClient 内部的资源;
104-
10. 程序退出;
95+
1. 向名字服务反注册所有service,避免下次流量路由到此节点;
96+
2. 禁用连接可读事件,包括 a.禁用监听socket的可读事件,避免创建新连接;b.禁用已连接socket的可读事件,避免收到该socket的新请求;
97+
3. 等待服务端接收的请求都处理完成(为避免因等待无法退出,默认最多等待5秒钟,可通过yaml中的server::max_wait_time配置);
98+
4. 关闭服务端网络连接;
99+
5. 调用 `TrpcApp` 业务子类的 `Destroy` 方法, 停止业务创建的动态资源(比如: 起的线程);
100+
6. 停止插件创建的动态资源(比如: 插件内部起的线程);
101+
7. 停止框架运行环境runtime;
102+
8. 释放框架运行环境 runtime 内部的资源;
103+
9. 释放框架运行环境 TrpcServer 内部的资源;
104+
10. 释放框架运行环境 TrpcClient 内部的资源;
105+
11. 程序退出;
105106

106107
## 客户端
107108

trpc/server/trpc_server.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,21 @@ void TrpcServer::Stop() {
213213
}
214214
}
215215

216+
// Disable the readable event for the listening socket to avoid creating new connections
217+
// Disable the readable event for the connected socket to avoid receiving new requests
218+
std::vector<ServiceAdapterPtr> stop_listened_adapters;
219+
for (const auto& iter : service_adapters_) {
220+
TRPC_LOG_DEBUG(iter.first << " start to StopListen...");
221+
auto stop_listened_adapter_it =
222+
std::find(stop_listened_adapters.begin(), stop_listened_adapters.end(), iter.second);
223+
if (stop_listened_adapter_it != stop_listened_adapters.end()) {
224+
continue;
225+
}
226+
227+
iter.second->StopListen(true);
228+
stop_listened_adapters.push_back(iter.second);
229+
}
230+
216231
uint64_t begin_stop_time = trpc::time::GetMilliSeconds();
217232
while (FrameStats::GetInstance()->GetServerStats().GetReqConcurrency() > 0) {
218233
TRPC_LOG_DEBUG("current ReqConcurrency:" << FrameStats::GetInstance()->GetServerStats().GetReqConcurrency()

0 commit comments

Comments
 (0)