MCPcopy Create free account
hub / github.com/apache/brpc / Echo

Method Echo

example/backup_request_c++/server.cpp:40–73  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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 }
74private:
75 butil::atomic<int> _count;
76};

Callers 1

mainFunction · 0.45

Calls 5

bthread_usleepFunction · 0.85
fetch_addMethod · 0.80
remote_sideMethod · 0.45
local_sideMethod · 0.45
set_indexMethod · 0.45

Tested by

no test coverage detected