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

Function main

example/cascade_echo_c++/client.cpp:88–149  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

86}
87
88int main(int argc, char* argv[]) {
89 // Parse gflags. We recommend you to use gflags as well.
90 GFLAGS_NAMESPACE::SetUsageMessage("Send EchoRequest to server every second");
91 GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
92
93 // A Channel represents a communication line to a Server. Notice that
94 // Channel is thread-safe and can be shared by all threads in your program.
95 brpc::Channel channel;
96 brpc::ChannelOptions options;
97 options.protocol = FLAGS_protocol;
98 options.connection_type = FLAGS_connection_type;
99 options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;
100 options.max_retry = FLAGS_max_retry;
101
102 // Initialize the channel, NULL means using default options.
103 // options, see `brpc/channel.h'.
104 if (channel.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) {
105 LOG(ERROR) << "Fail to initialize channel";
106 return -1;
107 }
108
109 std::vector<bthread_t> bids;
110 std::vector<pthread_t> pids;
111 if (!FLAGS_use_bthread) {
112 pids.resize(FLAGS_thread_num);
113 for (int i = 0; i < FLAGS_thread_num; ++i) {
114 if (pthread_create(&pids[i], NULL, sender, &channel) != 0) {
115 LOG(ERROR) << "Fail to create pthread";
116 return -1;
117 }
118 }
119 } else {
120 bids.resize(FLAGS_thread_num);
121 for (int i = 0; i < FLAGS_thread_num; ++i) {
122 if (bthread_start_background(
123 &bids[i], NULL, sender, &channel) != 0) {
124 LOG(ERROR) << "Fail to create bthread";
125 return -1;
126 }
127 }
128 }
129
130 if (FLAGS_dummy_port >= 0) {
131 brpc::StartDummyServerAt(FLAGS_dummy_port);
132 }
133
134 while (!brpc::IsAskedToQuit()) {
135 sleep(1);
136 LOG(INFO) << "Sending EchoRequest at qps=" << g_latency_recorder.qps(1)
137 << " latency=" << g_latency_recorder.latency(1);
138 }
139
140 LOG(INFO) << "EchoClient is going to quit";
141 for (int i = 0; i < FLAGS_thread_num; ++i) {
142 if (!FLAGS_use_bthread) {
143 pthread_join(pids[i], NULL);
144 } else {
145 bthread_join(bids[i], NULL);

Callers

nothing calls this directly

Calls 9

bthread_start_backgroundFunction · 0.85
StartDummyServerAtFunction · 0.85
IsAskedToQuitFunction · 0.85
bthread_joinFunction · 0.85
latencyMethod · 0.80
InitMethod · 0.45
c_strMethod · 0.45
resizeMethod · 0.45
qpsMethod · 0.45

Tested by

no test coverage detected