feng-y commented on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693767387


   > > 一个不太靠谱的基于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:
[email protected]



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

Reply via email to