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

Function main

example/selective_echo_c++/server.cpp:94–172  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

92};
93
94int main(int argc, char* argv[]) {
95 // Parse gflags. We recommend you to use gflags as well.
96 GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
97
98 if (FLAGS_server_num <= 0) {
99 LOG(ERROR) << "server_num must be positive";
100 return -1;
101 }
102
103 // We need multiple servers in this example.
104 brpc::Server* servers = new brpc::Server[FLAGS_server_num];
105 // For more options see `brpc/server.h'.
106 brpc::ServerOptions options;
107 options.idle_timeout_sec = FLAGS_idle_timeout_s;
108 options.max_concurrency = FLAGS_max_concurrency;
109
110 butil::StringSplitter sp(FLAGS_sleep_us.c_str(), ',');
111 std::vector<int64_t> sleep_list;
112 for (; sp; ++sp) {
113 sleep_list.push_back(strtoll(sp.field(), NULL, 10));
114 }
115 if (sleep_list.empty()) {
116 sleep_list.push_back(0);
117 }
118
119 // Instance of your services.
120 EchoServiceImpl* echo_service_impls = new EchoServiceImpl[FLAGS_server_num];
121 // Add the service into servers. Notice the second parameter, because the
122 // service is put on stack, we don't want server to delete it, otherwise
123 // use brpc::SERVER_OWNS_SERVICE.
124 for (int i = 0; i < FLAGS_server_num; ++i) {
125 int64_t sleep_us = sleep_list[(size_t)i < sleep_list.size() ? i : (sleep_list.size() - 1)];
126 echo_service_impls[i].set_index(i, sleep_us);
127 // will be shown on /version page
128 servers[i].set_version(butil::string_printf(
129 "example/selective_echo_c++[%d]", i));
130 if (servers[i].AddService(&echo_service_impls[i],
131 brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
132 LOG(ERROR) << "Fail to add service";
133 return -1;
134 }
135 // Start the server.
136 int port = FLAGS_port + i;
137 if (servers[i].Start(port, &options) != 0) {
138 LOG(ERROR) << "Fail to start EchoServer";
139 return -1;
140 }
141 }
142
143 // Service logic are running in separate worker threads, for main thread,
144 // we don't have much to do, just spinning.
145 std::vector<size_t> last_num_requests(FLAGS_server_num);
146 while (!brpc::IsAskedToQuit()) {
147 sleep(1);
148
149 size_t cur_total = 0;
150 for (int i = 0; i < FLAGS_server_num; ++i) {
151 const size_t current_num_requests =

Callers

nothing calls this directly

Calls 14

string_printfFunction · 0.85
IsAskedToQuitFunction · 0.85
fieldMethod · 0.80
AddServiceMethod · 0.80
c_strMethod · 0.45
push_backMethod · 0.45
emptyMethod · 0.45
sizeMethod · 0.45
set_indexMethod · 0.45
set_versionMethod · 0.45
StartMethod · 0.45
num_requestsMethod · 0.45

Tested by

no test coverage detected