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

Function main

example/memcache_c++/client.cpp:102–204  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

100}
101
102int main(int argc, char* argv[]) {
103 // Parse gflags. We recommend you to use gflags as well.
104 GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
105 if (FLAGS_exptime < 0) {
106 FLAGS_exptime = 0;
107 }
108
109 // A Channel represents a communication line to a Server. Notice that
110 // Channel is thread-safe and can be shared by all threads in your program.
111 brpc::Channel channel;
112
113 // Initialize the channel, NULL means using default options.
114 brpc::ChannelOptions options;
115 options.protocol = brpc::PROTOCOL_MEMCACHE;
116 options.connection_type = FLAGS_connection_type;
117 options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;
118 options.max_retry = FLAGS_max_retry;
119 if (FLAGS_use_couchbase && !FLAGS_bucket_name.empty()) {
120 brpc::policy::CouchbaseAuthenticator* auth =
121 new brpc::policy::CouchbaseAuthenticator(FLAGS_bucket_name,
122 FLAGS_bucket_password);
123 options.auth = auth;
124 }
125
126 if (channel.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) {
127 LOG(ERROR) << "Fail to initialize channel";
128 return -1;
129 }
130
131 // Pipeline #batch * #thread_num SET requests into memcache so that we
132 // have keys to get.
133 brpc::MemcacheRequest request;
134 brpc::MemcacheResponse response;
135 brpc::Controller cntl;
136 for (int i = 0; i < FLAGS_batch * FLAGS_thread_num; ++i) {
137 if (!request.Set(butil::string_printf("%s%d", FLAGS_key.c_str(), i),
138 butil::string_printf("%s%d", FLAGS_value.c_str(), i),
139 0xdeadbeef + i, FLAGS_exptime, 0)) {
140 LOG(ERROR) << "Fail to SET " << i << "th request";
141 return -1;
142 }
143 }
144 channel.CallMethod(NULL, &cntl, &request, &response, NULL);
145 if (cntl.Failed()) {
146 LOG(ERROR) << "Fail to access memcache, " << cntl.ErrorText();
147 return -1;
148 }
149 for (int i = 0; i < FLAGS_batch * FLAGS_thread_num; ++i) {
150 if (!response.PopSet(NULL)) {
151 LOG(ERROR) << "Fail to SET memcache, i=" << i
152 << ", " << response.LastError();
153 return -1;
154 }
155 }
156 if (FLAGS_exptime > 0) {
157 LOG(INFO) << "Set " << FLAGS_batch * FLAGS_thread_num
158 << " values, expired after " << FLAGS_exptime << " seconds";
159 } else {

Callers

nothing calls this directly

Calls 15

string_printfFunction · 0.85
bthread_start_backgroundFunction · 0.85
IsAskedToQuitFunction · 0.85
bthread_joinFunction · 0.85
ErrorTextMethod · 0.80
PopSetMethod · 0.80
latencyMethod · 0.80
emptyMethod · 0.45
InitMethod · 0.45
c_strMethod · 0.45
SetMethod · 0.45
CallMethodMethod · 0.45

Tested by

no test coverage detected