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