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

Function sender

example/memcache_c++/client.cpp:48–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

46butil::static_atomic<int> g_sender_count = BUTIL_STATIC_ATOMIC_INIT(0);
47
48static void* sender(void* arg) {
49 google::protobuf::RpcChannel* channel =
50 static_cast<google::protobuf::RpcChannel*>(arg);
51 const int base_index = g_sender_count.fetch_add(1, butil::memory_order_relaxed);
52
53 std::string value;
54 std::vector<std::pair<std::string, std::string> > kvs;
55 kvs.resize(FLAGS_batch);
56 for (int i = 0; i < FLAGS_batch; ++i) {
57 kvs[i].first = butil::string_printf("%s%d", FLAGS_key.c_str(), base_index + i);
58 kvs[i].second = butil::string_printf("%s%d", FLAGS_value.c_str(), base_index + i);
59 }
60 brpc::MemcacheRequest request;
61 for (int i = 0; i < FLAGS_batch; ++i) {
62 CHECK(request.Get(kvs[i].first));
63 }
64 while (!brpc::IsAskedToQuit()) {
65 // We will receive response synchronously, safe to put variables
66 // on stack.
67 brpc::MemcacheResponse response;
68 brpc::Controller cntl;
69
70 // Because `done'(last parameter) is NULL, this function waits until
71 // the response comes back or error occurs(including timedout).
72 channel->CallMethod(NULL, &cntl, &request, &response, NULL);
73 const int64_t elp = cntl.latency_us();
74 if (!cntl.Failed()) {
75 g_latency_recorder << cntl.latency_us();
76 for (int i = 0; i < FLAGS_batch; ++i) {
77 uint32_t flags;
78 if (!response.PopGet(&value, &flags, NULL)) {
79 LOG(INFO) << "Fail to GET the key, " << response.LastError();
80 brpc::AskToQuit();
81 return NULL;
82 }
83 CHECK(flags == 0xdeadbeef + base_index + i)
84 << "flags=" << flags;
85 CHECK(kvs[i].second == value)
86 << "base=" << base_index << " i=" << i << " value=" << value;
87 }
88 } else {
89 g_error_count << 1;
90 CHECK(brpc::IsAskedToQuit() || !FLAGS_dont_fail)
91 << "error=" << cntl.ErrorText() << " latency=" << elp;
92 // We can't connect to the server, sleep a while. Notice that this
93 // is a specific sleeping to prevent this thread from spinning too
94 // fast. You should continue the business logic in a production
95 // server rather than sleeping.
96 bthread_usleep(50000);
97 }
98 }
99 return NULL;
100}
101
102int main(int argc, char* argv[]) {
103 // Parse gflags. We recommend you to use gflags as well.

Callers

nothing calls this directly

Calls 13

string_printfFunction · 0.85
IsAskedToQuitFunction · 0.85
AskToQuitFunction · 0.85
bthread_usleepFunction · 0.85
fetch_addMethod · 0.80
latency_usMethod · 0.80
PopGetMethod · 0.80
ErrorTextMethod · 0.80
resizeMethod · 0.45
c_strMethod · 0.45
GetMethod · 0.45
CallMethodMethod · 0.45

Tested by

no test coverage detected