From 36d14b4ad1c602fbe3b917ff15d70933da5d576e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kiviyu=28=E4=BD=99=E5=86=9B=29?= Date: Fri, 29 Mar 2024 14:22:37 +0800 Subject: [PATCH] Feature: During graceful shutdown, more precise control over the readability and writability events of connections to reduce timeouts --- docs/en/architecture_design.md | 21 +++++++++++---------- docs/zh/architecture_design.md | 21 +++++++++++---------- trpc/server/trpc_server.cc | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/docs/en/architecture_design.md b/docs/en/architecture_design.md index 0f446258..41b88dfe 100644 --- a/docs/en/architecture_design.md +++ b/docs/en/architecture_design.md @@ -93,16 +93,17 @@ The request processing generally includes the following processes: The service exit process generally includes the following processes: -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); -2. Stop the listening and reading events of the server network connection; -3. Close the server network connections; -4. Call the `Destroy` method of the `TrpcApp` business subclass to stop the dynamic resources created by the business (for example: started threads); -5. Stop the dynamic resources created by the plugins (for example: threads started inside the plugins); -6. Stop the framework runtime environment runtime; -7. Release the resources inside the runtime environment of the framework; -8. Release the internal resources of the framework operating environment TrpcServer; -9. Release the internal resources of the framework operating environment TrpcClient; -10. The program exits; +1. Unregister all services from the name service to prevent future traffic from being routed to this node; +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; +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); +4. Close the server network connections; +5. Call the `Destroy` method of the `TrpcApp` business subclass to stop the dynamic resources created by the business (for example: started threads); +6. Stop the dynamic resources created by the plugins (for example: threads started inside the plugins); +7. Stop the framework runtime environment runtime; +8. Release the resources inside the runtime environment of the framework; +9. Release the internal resources of the framework operating environment TrpcServer; +10. Release the internal resources of the framework operating environment TrpcClient; +11. The program exits; ## Client diff --git a/docs/zh/architecture_design.md b/docs/zh/architecture_design.md index aa87a002..cb50f86f 100644 --- a/docs/zh/architecture_design.md +++ b/docs/zh/architecture_design.md @@ -92,16 +92,17 @@ Server 启动过程, 大致包括以下流程: 服务退出过程, 大致包括以下流程: -1. 等待服务端接收的请求都处理完成(为避免因等待无法退出,默认最多等待5秒钟,可通过yaml中的server::max_wait_time配置); -2. 停止服务端网络连接的监听和读事件; -3. 关闭服务端网络连接; -4. 调用 `TrpcApp` 业务子类的 `Destroy` 方法, 停止业务创建的动态资源(比如: 起的线程); -5. 停止插件创建的动态资源(比如: 插件内部起的线程); -6. 停止框架运行环境runtime; -7. 释放框架运行环境 runtime 内部的资源; -8. 释放框架运行环境 TrpcServer 内部的资源; -9. 释放框架运行环境 TrpcClient 内部的资源; -10. 程序退出; +1. 向名字服务反注册所有service,避免下次流量路由到此节点; +2. 禁用连接可读事件,包括 a.禁用监听socket的可读事件,避免创建新连接;b.禁用已连接socket的可读事件,避免收到该socket的新请求; +3. 等待服务端接收的请求都处理完成(为避免因等待无法退出,默认最多等待5秒钟,可通过yaml中的server::max_wait_time配置); +4. 关闭服务端网络连接; +5. 调用 `TrpcApp` 业务子类的 `Destroy` 方法, 停止业务创建的动态资源(比如: 起的线程); +6. 停止插件创建的动态资源(比如: 插件内部起的线程); +7. 停止框架运行环境runtime; +8. 释放框架运行环境 runtime 内部的资源; +9. 释放框架运行环境 TrpcServer 内部的资源; +10. 释放框架运行环境 TrpcClient 内部的资源; +11. 程序退出; ## 客户端 diff --git a/trpc/server/trpc_server.cc b/trpc/server/trpc_server.cc index cef00458..35f9c2df 100644 --- a/trpc/server/trpc_server.cc +++ b/trpc/server/trpc_server.cc @@ -213,6 +213,21 @@ void TrpcServer::Stop() { } } + // Disable the readable event for the listening socket to avoid creating new connections + // Disable the readable event for the connected socket to avoid receiving new requests + std::vector stop_listened_adapters; + for (const auto& iter : service_adapters_) { + TRPC_LOG_DEBUG(iter.first << " start to StopListen..."); + auto stop_listened_adapter_it = + std::find(stop_listened_adapters.begin(), stop_listened_adapters.end(), iter.second); + if (stop_listened_adapter_it != stop_listened_adapters.end()) { + continue; + } + + iter.second->StopListen(true); + stop_listened_adapters.push_back(iter.second); + } + uint64_t begin_stop_time = trpc::time::GetMilliSeconds(); while (FrameStats::GetInstance()->GetServerStats().GetReqConcurrency() > 0) { TRPC_LOG_DEBUG("current ReqConcurrency:" << FrameStats::GetInstance()->GetServerStats().GetReqConcurrency()