| 34 | DEFINE_int32(backup_request_ms, 2, "Timeout for sending backup request"); |
| 35 | |
| 36 | int main(int argc, char* argv[]) { |
| 37 | // Parse gflags. We recommend you to use gflags as well. |
| 38 | GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); |
| 39 | |
| 40 | // A Channel represents a communication line to a Server. Notice that |
| 41 | // Channel is thread-safe and can be shared by all threads in your program. |
| 42 | brpc::Channel channel; |
| 43 | |
| 44 | // Initialize the channel, NULL means using default options. |
| 45 | brpc::ChannelOptions options; |
| 46 | options.protocol = FLAGS_protocol; |
| 47 | options.connection_type = FLAGS_connection_type; |
| 48 | options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/; |
| 49 | options.max_retry = FLAGS_max_retry; |
| 50 | options.backup_request_ms = FLAGS_backup_request_ms; |
| 51 | if (channel.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) { |
| 52 | LOG(ERROR) << "Fail to initialize channel"; |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | // Normally, you should not call a Channel directly, but instead construct |
| 57 | // a stub Service wrapping it. stub can be shared by all threads as well. |
| 58 | example::EchoService_Stub stub(&channel); |
| 59 | |
| 60 | // Send a request and wait for the response every 1 second. |
| 61 | int counter = 0; |
| 62 | while (!brpc::IsAskedToQuit()) { |
| 63 | // We will receive response synchronously, safe to put variables |
| 64 | // on stack. |
| 65 | example::EchoRequest request; |
| 66 | example::EchoResponse response; |
| 67 | brpc::Controller cntl; |
| 68 | |
| 69 | request.set_index(++counter); |
| 70 | |
| 71 | // Because `done'(last parameter) is NULL, this function waits until |
| 72 | // the response comes back or error occurs(including timedout). |
| 73 | stub.Echo(&cntl, &request, &response, NULL); |
| 74 | if (!cntl.Failed()) { |
| 75 | LOG(INFO) << "Received response[index=" << response.index() |
| 76 | << "] from " << cntl.remote_side() |
| 77 | << " to " << cntl.local_side() |
| 78 | << " latency=" << cntl.latency_us() << "us"; |
| 79 | } else { |
| 80 | LOG(WARNING) << cntl.ErrorText(); |
| 81 | } |
| 82 | sleep(1); |
| 83 | } |
| 84 | |
| 85 | LOG(INFO) << "EchoClient is going to quit"; |
| 86 | return 0; |
| 87 | } |
nothing calls this directly
no test coverage detected