chenBright opened a new issue, #2143:
URL: https://github.com/apache/brpc/issues/2143

   **Describe the bug (描述bug)**
   
   
Channel/Controller共享LoadBalancerWithNaming,LoadBalancerWithNaming之间共享NamingServiceThread(来自g_nsthread_map)。
   
   
如下例子,当Channel和Controller是service回调函数的栈变量,且ns需要通过rpc获取节点信息时,Channel析构,会将LoadBalancerWithNaming析构,而如果此时NamingServiceThread的引用计数为1的话,NamingServiceThread会析构。NamingServiceThread的析构时需要等(Join)ns
 bthread退出。如果刚好LoadBalancerWithNaming的ns 
bthread在等rpc完成的话,则LoadBalancerWithNaming、Channel需要等到ns 
bthread的rpc完成才能完成析构,总体的表现就是EchoService的一次rpc请求要等ns 
bthread的rpc完成才能回包,耗时会上涨,甚至client端可能会超时。特别consul ns里的rpc是long 
polling,rpc耗时比较久,最长是1分钟。
   
   ```c++
   class EchoServiceImpl : public EchoService {
   public:
       EchoServiceImpl() {}
       virtual ~EchoServiceImpl() {}
       virtual void Echo(google::protobuf::RpcController* cntl_base,
                         const EchoRequest* request,
                         EchoResponse* response,
                         google::protobuf::Closure* done) {
           brpc::ClosureGuard done_guard(done);
   
           brpc::Channel ch;
           brpc::ChannelOptions options;
           brpc::Controller cntl;
           ch.Init("consul://xxx", "rr", options);
           BackEchoService_Stub stub(&channel);
           stub.Echo(...);
   
           response->set_message(request->message());
       }
   };
   ```
   
   **To Reproduce (复现方法)**
   
   
   **Expected behavior (期望行为)**
   
   
希望Channel/Controller的析构与NamingServiceThread的析构解耦,不需要等LoadBalancerWithNaming的ns 
bthread在等rpc完成
   
   **初步方案**:
   
   1. 
g_nsthread_map持有一个NamingServiceThread的引用,这样LoadBalancerWithNaming析构的时候,就不会将NamingServiceThread析构了,Channel/Controller的析构就不用等NamingServiceThread析构了。
   2. 
起一个协程,定期加锁扫描g_nsthread_map,将引用计数为1的NamingServiceThread拷贝到锁外进行析构。还可以加一个idle的限制,超过idle且引用计数为1的NamingServiceThread才会被析构。
   
   
   **Versions (各种版本)**
   OS:
   Compiler:
   brpc:
   protobuf:
   
   **Additional context/screenshots (更多上下文/截图)**
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to