| 50 | _sleep_us = sleep_us; |
| 51 | } |
| 52 | virtual void Echo(google::protobuf::RpcController* cntl_base, |
| 53 | const example::EchoRequest* request, |
| 54 | example::EchoResponse* response, |
| 55 | google::protobuf::Closure* done) { |
| 56 | brpc::ClosureGuard done_guard(done); |
| 57 | brpc::Controller* cntl = |
| 58 | static_cast<brpc::Controller*>(cntl_base); |
| 59 | if (_sleep_us > 0) { |
| 60 | double delay = _sleep_us; |
| 61 | const double a = FLAGS_exception_ratio * 0.5; |
| 62 | if (a >= 0.0001) { |
| 63 | double x = butil::RandDouble(); |
| 64 | if (x < a) { |
| 65 | const double min_sleep_us = FLAGS_min_ratio * _sleep_us; |
| 66 | delay = min_sleep_us + (_sleep_us - min_sleep_us) * x / a; |
| 67 | } else if (x + a > 1) { |
| 68 | const double max_sleep_us = FLAGS_max_ratio * _sleep_us; |
| 69 | delay = _sleep_us + (max_sleep_us - _sleep_us) * (x + a - 1) / a; |
| 70 | } |
| 71 | } |
| 72 | if (FLAGS_spin) { |
| 73 | int64_t end_time = butil::cpuwide_time_us() + (int64_t)delay; |
| 74 | while (butil::cpuwide_time_us() < end_time) {} |
| 75 | } else { |
| 76 | bthread_usleep((int64_t)delay); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Fill response. |
| 81 | response->set_value(request->value() + 1); |
| 82 | if (FLAGS_send_attachment) { |
| 83 | // Set attachment which is wired to network directly instead of |
| 84 | // being serialized into protobuf messages. |
| 85 | cntl->response_attachment().append("bar"); |
| 86 | } |
| 87 | _nreq << 1; |
| 88 | } |
| 89 | |
| 90 | size_t num_requests() const { return _nreq.get_value(); } |
| 91 |
no test coverage detected