21 #define GRPC_BACKOFF(expression__, status__) \ 22 GRPC_BACKOFF_TIMEOUT(expression__, status__, 64, 0) 24 #define GRPC_BACKOFF_D(expression__, status__, deadline__) \ 25 GRPC_BACKOFF_TIMEOUT(expression__, status__, 64, deadline__) 27 #define GRPC_BACKOFF_TIMEOUT(expression__, status__, timeout__, deadline__) \ 29 int sleep_debt__ = 1; \ 31 grpc::ClientContext ctx; \ 32 if (deadline__ > 0) { \ 33 std::chrono::system_clock::time_point deadline = \ 34 std::chrono::system_clock::now() + \ 35 std::chrono::seconds(deadline__); \ 36 ctx.set_deadline(deadline); \ 38 const grpc::Status result__ = (expression__); \ 39 if (result__.error_code() == grpc::StatusCode::UNAVAILABLE) { \ 40 double sleep_time__ = \ 41 (sleep_debt__ + (static_cast<double>(rand()) / RAND_MAX)); \ 42 if (sleep_debt__ < (timeout__)) { \ 45 LOG(WARNING) << "GRPC_BACKOFF: reached max backoff."; \ 46 status__ = result__; \ 49 LOG(WARNING) << "GRPC_BACKOFF: transient failure, sleeping for " \ 50 << sleep_time__ << " seconds."; \ 51 usleep(sleep_time__ * 1000000); \ 54 status__ = result__; \ 59 template <
class ServiceImpl>
63 virtual void Handle(ServiceImpl* service) = 0;
67 enum class State { Received, Sent, Cancelled };
69 Tag(
BaseCall* call, State state) : call_(call), state_(state) {}
75 const State& get_state() {
79 void Advance(ServiceImpl* service) {
81 case State::Received: {
82 call_->Handle(service);
89 case State::Cancelled: {
104 template <
class ServiceImpl,
class Request,
class Reply>
106 using HandleFunction =
109 Call(
const std::string& _name, HandleFunction _handler)
110 : handler(_handler), responder(&ctx) {
114 void Handle(ServiceImpl* service)
override {
115 (service->*handler)(
this);
118 void Respond(grpc::Status status) {
119 responder.Finish(reply, status, &sent_tag);
122 HandleFunction handler;
123 grpc::ServerContext ctx;
126 grpc::ServerAsyncResponseWriter<Reply> responder;
130 Tag received_tag{
this, Tag::State::Received};
131 Tag sent_tag{
this, Tag::State::Sent};
132 Tag cancelled_tag{
this, Tag::State::Cancelled};
Definition: database.cpp:36