You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by GitBox <gi...@apache.org> on 2020/09/16 14:30:18 UTC

[GitHub] [incubator-brpc] zyearn commented on issue #1243: brpc::Join(CallId id) 支持带超时的等待

zyearn commented on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693444506


   > 一个不太靠谱的基于future的实现
   > callback 里面设置 future
   > 
   > ```
   > void HandleEchoResponse(
   >         brpc::Controller* cntl,
   >         example::EchoResponse* response,
   >         std::shared_ptr<std::promise<bool>> waiter) {
   >     waiter->set_value(true);
   > }
   > ```
   > 
   > 等待的时候不join, 等future ,如果futre 不成功就 cancel 请求
   > 
   > ```
   >       std::shared_ptr<std::promise<bool>> waiter(std::make_shared<std::promise<bool>>());
   > 
   >         // Because `done'(last parameter) is NULL, this function waits until
   >         // the response comes back or error occurs(including timedout).
   >         google::protobuf::Closure* done = brpc::NewCallback(
   >             &HandleEchoResponse, &cntl, &response, waiter);
   > 
   >         auto call_id = cntl.call_id();
   >         stub.Echo(&cntl, &request, &response, done);
   >         LOG(INFO) << "waiting Join:" << errno << ", " << EINVAL;
   >         // brpc::Join(call_id);
   >         if (waiter->get_future().wait_for(std::chrono::microseconds(1)) != std::future_status::ready) {
   >           LOG(INFO) << "timeout";
   >           brpc::StartCancel(call_id);
   >         } else {
   >         .......
   >        }
   > ```
   
   这里用future来实现不太合适,future.wait会阻塞brpc的worker线程。
   
   Join的语义暗含的意思是当Join返回的时候,被Join的对象已经处于一个结束的状态,这里加个timeout会有点破坏语义。
   
    这个需求也可以通过bthread::CountdownEvent来实现,在原来future set_value的地方改成signal,在Join的地方改成timed_wait。
   


----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org