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 02:07:40 UTC

[GitHub] [incubator-brpc] feng-y opened a new issue #1243: brpc::Join(CallId id) 支持带超时的等待

feng-y opened a new issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243


   **Describe the solution you'd like (描述你期望的解决方法)**
   `brpc::Join(CallId id) ` 增加一个参数支持,最长等待时间, `brpc::Join(CallId id, int64_t int64_t waiting_us) `
   
   **Describe alternatives you've considered (描述你想到的折衷方案)**
   `
   index 23046b25..caf99b40 100644
   --- a/src/bthread/id.cpp
   +++ b/src/bthread/id.cpp
   @@ -510,6 +510,10 @@ int bthread_id_cancel(bthread_id_t id) {
    }
   
    int bthread_id_join(bthread_id_t id) {
   +  return bthread_id_join(id, 0);
   +}
   +
   +int bthread_id_join(bthread_id_t id, int64_t us) {
        const bthread::IdResourceId slot = bthread::get_slot(id);
        bthread::Id* const meta = address_resource(slot);
        if (!meta) {
   @@ -526,9 +530,17 @@ int bthread_id_join(bthread_id_t id) {
            if (!has_ver) {
                break;
            }
   -        if (bthread::butex_wait(join_butex, expected_ver, NULL) < 0 &&
   -            errno != EWOULDBLOCK && errno != EINTR) {
   +        if (us > 0) {
   +          timespec abstime = butil::microseconds_to_timespec(us + butil::gettimeofday_ns());
   +          if (bthread::butex_wait(join_butex, expected_ver, &abstime) < 0 &&
   +              errno != EWOULDBLOCK && errno != EINTR) {
                return errno;
   +          }
   +        } else {
   +          if (bthread::butex_wait(join_butex, expected_ver, NULL) < 0 &&
   +              errno != EWOULDBLOCK && errno != EINTR) {
   +              return errno;
   +          }
            }
        }
        return 0;`
   
   **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.

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


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

Posted by GitBox <gi...@apache.org>.
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


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

Posted by GitBox <gi...@apache.org>.
feng-y commented on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693173569


   SB服务的超时依赖于SA的响应,所以SB的超时是不好控制的


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


[GitHub] [incubator-brpc] feng-y edited a comment on issue #1243: brpc::Join(CallId id) 支持带超时的等待

Posted by GitBox <gi...@apache.org>.
feng-y edited a comment on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693219844


   一个不太靠谱的基于future的实现
   ```
   void HandleEchoResponse(
           brpc::Controller* cntl,
           example::EchoResponse* response,
           std::shared_ptr<std::promise<bool>> waiter) {
       waiter->set_value(true);
   }
         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 {
           .......
          }
   
   ```


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


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

Posted by GitBox <gi...@apache.org>.
gydong commented on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693776704


   我们内部有个类似实现,仅供参考:
   
   ```cpp
   // This class is NOT thread safe.
   class BthreadGroup {
    public:
     class Task;
   
     BthreadGroup();
     ~BthreadGroup();
   
     // Always return true if call it before "Start()", return false otherwise.
     bool AddOneTask(std::unique_ptr<Task>&& task);
   
     void Start(int timeout_us = 0);
   
     // The signature of TCallable is " void (Task*)".
     // Note: if u start the group with a timeout, then use this function to
     // retrieve your tasks, or there is a race condition.
     template <typename TCallable>
     void ForeachFinishedTask(const TCallable& func);
   
    private:
     struct SharedPart;
   
     void StartBthread();
   
     // Ihis just set a "stop flag" simply, and the user logic should stop
     // according to Task::ShouldStop().
     void NotifyStop();
   
     bool is_started_ = false;
     std::shared_ptr<SharedPart> sp_;
     std::vector<std::unique_ptr<Task>> tasks_;
   };
   
   class BthreadGroup::Task {
    public:
     Task();
     virtual ~Task();
   
     virtual void Run() = 0;
   
    protected:
     // User should stop the task if this function return true. User can also
     // ignore this function, and then this task will stop and delete itself.
     bool ShouldStop() const;
   
    private:
     friend class BthreadGroup;
   
     static void* InternalRun(void* data);
   
     std::atomic<bool> is_finished_{false};
     std::atomic<bool> should_stop_{false};
     bthread_t bthread_ = INVALID_BTHREAD;
     std::weak_ptr<BthreadGroup::SharedPart> sp_;
   };
   
   template <typename TCallable>
   void BthreadGroup::ForeachFinishedTask(const TCallable& func) {
     for (std::unique_ptr<Task>& task : tasks_) {
       if (task && task->is_finished_.exchange(true, std::memory_order_seq_cst)) {
         func(task.get());
       } else {
         task.release();
       }
     }
   }
   ```
   
   ```cpp
   namespace {
   
   bvar::Adder<int> g_bthread_groups("bthread_group_count");
   bvar::Adder<int> g_bthread_group_timeout_count("bthread_group_timeout_count");
   bvar::Adder<int> g_bthread_group_tasks("bthread_group_task_count");
   
   }  // namespace
   
   struct BthreadGroup::SharedPart {
     SharedPart() : ce(0) { }
   
     bthread::CountdownEvent ce;
   };
   
   BthreadGroup::BthreadGroup() : sp_(std::make_shared<SharedPart>()) {
     g_bthread_groups << 1;
   }
   
   bool BthreadGroup::AddOneTask(std::unique_ptr<Task>&& task) {
     if (is_started_) {
       return false;
     }
   
     task->sp_ = sp_;
     tasks_.emplace_back(std::move(task));
     sp_->ce.add_count();
     return true;
   }
   
   void BthreadGroup::Start(int timeout_us) {
     StartBthread();
     is_started_ = true;
     if (timeout_us > 0) {
       const timespec end_time = butil::microseconds_from_now(timeout_us);
       const int status = sp_->ce.timed_wait(end_time);
       if (status == ETIMEDOUT) {
         g_bthread_group_timeout_count << 1;
       } else if (status != 0) {
         LOG(ERROR) << "bthread group is stopped du to: " << berror(status);
       }
     } else {
       const int status = sp_->ce.wait();
       if (status != 0) {
         LOG(ERROR) << "bthread group is stopped du to: " << berror(status);
       }
     }
     NotifyStop();
   }
   
   void BthreadGroup::StartBthread() {
     if (tasks_.empty()) {
       return;
     }
   
     for (size_t i = 0; i < tasks_.size() - 1; ++i) {
       CHECK_EQ(0, bthread_start_background(
               &tasks_[i]->bthread_, nullptr, Task::InternalRun, tasks_[i].get()))
           << "Failed to start a bthread, error: " << berror();
     }
     CHECK_EQ(0, bthread_start_urgent(&tasks_.back()->bthread_,
                                      nullptr,
                                      Task::InternalRun,
                                      tasks_.back().get()))
         << "Failed to start a bthread, error: " << berror();
   }
   
   void BthreadGroup::NotifyStop() {
     for (const std::unique_ptr<Task>& task : tasks_) {
       task->should_stop_.store(true, std::memory_order_release);
     }
   }
   
   BthreadGroup::~BthreadGroup() {
     // Ensure the task thread aware of the finish state.
     ForeachFinishedTask([](Task* dummy){});
     g_bthread_groups << -1;
   }
   
   BthreadGroup::Task::Task() {
     g_bthread_group_tasks << 1;
   }
   
   BthreadGroup::Task::~Task() {
     g_bthread_group_tasks << -1;
   }
   
   bool BthreadGroup::Task::ShouldStop() const {
     return should_stop_.load(std::memory_order_acquire) ||
         1 == bthread_stopped(bthread_self());
   }
   
   void* BthreadGroup::Task::InternalRun(void* data) {
     Task* task = static_cast<Task*>(data);
     task->Run();
     if (task->is_finished_.exchange(true, std::memory_order_seq_cst)) {
       delete task;
     } else if (std::shared_ptr<SharedPart> sp = task->sp_.lock()) {
       sp->ce.signal();
     }
   
     return nullptr;
   }
   ```


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


[GitHub] [incubator-brpc] feng-y closed issue #1243: brpc::Join(CallId id) 支持带超时的等待

Posted by GitBox <gi...@apache.org>.
feng-y closed issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243


   


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


[GitHub] [incubator-brpc] feng-y edited a comment on issue #1243: brpc::Join(CallId id) 支持带超时的等待

Posted by GitBox <gi...@apache.org>.
feng-y edited a comment on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693761761


   > 启动一个bthread sleep超时的时间后 brpc::StartCancel就可以,并不复杂。
   
   wait 和 sleep 不是一个语义,和复杂没有关系,哪里需要sleep 多长时间呢,假设是5ms, 可能这个请求在1ms 后就返回了,需要的是异步通知机制, 我看看用 bthread::CountdownEvent来实现看看


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


[GitHub] [incubator-brpc] feng-y edited a comment on issue #1243: brpc::Join(CallId id) 支持带超时的等待

Posted by GitBox <gi...@apache.org>.
feng-y edited a comment on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693219844


   一个不太靠谱的基于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 {
           .......
          }
   
   ```


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


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

Posted by GitBox <gi...@apache.org>.
feng-y commented on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693197866


   还是关注问题本身吧!  一个带超时的逻辑还需要启动一个定时任务去cancel,我理解一个超时等待的功能也算是一个合理的需求吧
   呼唤大神来看看 @jamesge @zyearn 


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


[GitHub] [incubator-brpc] feng-y closed issue #1243: brpc::Join(CallId id) 支持带超时的等待

Posted by GitBox <gi...@apache.org>.
feng-y closed issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243


   


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


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

Posted by GitBox <gi...@apache.org>.
feng-y commented on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693219844


   一个不太靠谱的基于future的实现
   ```
   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 {
           .......
          }
   ```


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


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

Posted by GitBox <gi...@apache.org>.
gydong commented on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693790906


   > @zyearn 感谢提供的方案,另外再请教一个问题,cancel 和正常的请求 在 回调里面有办法区分么?
   
   ErrorCode 会是 ECANCELED 的


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


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

Posted by GitBox <gi...@apache.org>.
feng-y commented on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693147740


   哪怎么保证是安全的呢?


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


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

Posted by GitBox <gi...@apache.org>.
cdjingit commented on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693343012


   启动一个bthread sleep超时的时间后 brpc::StartCancel就可以,并不复杂。


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


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

Posted by GitBox <gi...@apache.org>.
feng-y commented on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693761761


   > 启动一个bthread sleep超时的时间后 brpc::StartCancel就可以,并不复杂。
   
   wait 和 sleep 不是一个语义,和复杂没有关系,哪里需要sleep 多长时间呢,加速5ms, 可能这个请求在1ms 后就返回了,需要的是异步通知机制, 我看看用 bthread::CountdownEvent来实现看看


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


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

Posted by GitBox <gi...@apache.org>.
cdjingit commented on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693136308


   超时后,rpc实际没有结束,需要cancel掉才可以,否则会有资源访问问题。原来的语义join后,rpc一定是结束了,后续处理是安全的。
   


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


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

Posted by GitBox <gi...@apache.org>.
gydong commented on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693155824


   相关逻辑都在用户代码中,要做到安全比较难把控。你的场景,通过灵活调整SB的超时时间不可以吗?


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


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

Posted by GitBox <gi...@apache.org>.
cdjingit commented on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693175268


   启动一个定时任务cancel掉这个rpc


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


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

Posted by GitBox <gi...@apache.org>.
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:
users@infra.apache.org



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


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

Posted by GitBox <gi...@apache.org>.
feng-y commented on issue #1243:
URL: https://github.com/apache/incubator-brpc/issues/1243#issuecomment-693768005


   @zyearn 感谢提供的方案,另外再请教一个问题,cancel 和正常的请求 在 回调里面有办法区分么?


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