| 85 | } |
| 86 | |
| 87 | int main(int argc, char* argv[]) { |
| 88 | // Parse gflags. We recommend you to use gflags as well. |
| 89 | GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); |
| 90 | |
| 91 | // A Channel represents a communication line to a Server. Notice that |
| 92 | // Channel is thread-safe and can be shared by all threads in your program. |
| 93 | brpc::SelectiveChannel channel; |
| 94 | brpc::ChannelOptions schan_options; |
| 95 | schan_options.timeout_ms = FLAGS_timeout_ms; |
| 96 | schan_options.backup_request_ms = FLAGS_backup_ms; |
| 97 | schan_options.max_retry = FLAGS_max_retry; |
| 98 | if (channel.Init(FLAGS_load_balancer.c_str(), &schan_options) != 0) { |
| 99 | LOG(ERROR) << "Fail to init SelectiveChannel"; |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | // Add sub channels. |
| 104 | // ================ |
| 105 | std::vector<brpc::ChannelBase*> sub_channels; |
| 106 | |
| 107 | // Add an ordinary channel. |
| 108 | brpc::Channel* sub_channel1 = new brpc::Channel; |
| 109 | butil::EndPoint pt; |
| 110 | if (str2endpoint(FLAGS_starting_server.c_str(), &pt) != 0 && |
| 111 | hostname2endpoint(FLAGS_starting_server.c_str(), &pt) != 0) { |
| 112 | LOG(ERROR) << "Invalid address=`" << FLAGS_starting_server << "'"; |
| 113 | return -1; |
| 114 | } |
| 115 | brpc::ChannelOptions options; |
| 116 | options.protocol = FLAGS_protocol; |
| 117 | options.connection_type = FLAGS_connection_type; |
| 118 | std::ostringstream os; |
| 119 | os << "list://"; |
| 120 | for (int i = 0; i < 3; ++i) { |
| 121 | os << butil::EndPoint(pt.ip, pt.port++) << ","; |
| 122 | } |
| 123 | if (sub_channel1->Init(os.str().c_str(), FLAGS_load_balancer.c_str(), |
| 124 | &options) != 0) { |
| 125 | LOG(ERROR) << "Fail to init ordinary channel"; |
| 126 | return -1; |
| 127 | } |
| 128 | sub_channels.push_back(sub_channel1); |
| 129 | |
| 130 | // Add a parallel channel. |
| 131 | brpc::ParallelChannel* sub_channel2 = new brpc::ParallelChannel; |
| 132 | brpc::ParallelChannelOptions pchan_options; |
| 133 | pchan_options.fail_limit = 1; |
| 134 | if (sub_channel2->Init(&pchan_options) != 0) { |
| 135 | LOG(ERROR) << "Fail to init sub_channel2"; |
| 136 | return -1; |
| 137 | } |
| 138 | for (int i = 0; i < 3; ++i) { |
| 139 | brpc::ChannelOptions options; |
| 140 | options.protocol = FLAGS_protocol; |
| 141 | options.connection_type = FLAGS_connection_type; |
| 142 | brpc::Channel* c = new brpc::Channel; |
| 143 | if (c->Init(butil::EndPoint(pt.ip, pt.port++), &options) != 0) { |
| 144 | LOG(ERROR) << "Fail to init sub channel[" << i << "] of pchan"; |
nothing calls this directly
no test coverage detected