| 38 | SleepyEchoService() : _count(0) {} |
| 39 | virtual ~SleepyEchoService() {} |
| 40 | virtual void Echo(google::protobuf::RpcController* cntl_base, |
| 41 | const EchoRequest* request, |
| 42 | EchoResponse* response, |
| 43 | google::protobuf::Closure* done) { |
| 44 | // This object helps you to call done->Run() in RAII style. If you need |
| 45 | // to process the request asynchronously, pass done_guard.release(). |
| 46 | brpc::ClosureGuard done_guard(done); |
| 47 | |
| 48 | brpc::Controller* cntl = |
| 49 | static_cast<brpc::Controller*>(cntl_base); |
| 50 | |
| 51 | // The purpose of following logs is to help you to understand |
| 52 | // how clients interact with servers more intuitively. You should |
| 53 | // remove these logs in performance-sensitive servers. |
| 54 | // The noflush prevents the log from being flushed immediately. |
| 55 | LOG(INFO) << "Received request[index=" << request->index() |
| 56 | << "] from " << cntl->remote_side() |
| 57 | << " to " << cntl->local_side() << noflush; |
| 58 | // Sleep a while for 0th, 2nd, 4th, 6th ... requests to trigger backup request |
| 59 | // at client-side. |
| 60 | bool do_sleep = (_count.fetch_add(1, butil::memory_order_relaxed) % 2 == 0); |
| 61 | if (do_sleep) { |
| 62 | LOG(INFO) << ", sleep " << FLAGS_sleep_ms |
| 63 | << " ms to trigger backup request" << noflush; |
| 64 | } |
| 65 | LOG(INFO); |
| 66 | |
| 67 | // Fill response. |
| 68 | response->set_index(request->index()); |
| 69 | |
| 70 | if (do_sleep) { |
| 71 | bthread_usleep(FLAGS_sleep_ms * 1000); |
| 72 | } |
| 73 | } |
| 74 | private: |
| 75 | butil::atomic<int> _count; |
| 76 | }; |
no test coverage detected