/
client.cc
236 lines (202 loc) · 7.6 KB
/
client.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "client.h"
#include <rapidjson/document.h>
#include <evpp/event_loop.h>
#include <evpp/tcp_client.h>
#include <evpp/tcp_conn.h>
#include <evpp/httpc/request.h>
#include <evpp/httpc/response.h>
#include <evpp/httpc/conn.h>
#include "command.h"
#include "option.h"
namespace evnsq {
static const std::string kNSQMagic = " V2";
static const std::string kOK = "OK";
Client::Client(evpp::EventLoop* l, Type t, const Option& ops)
: loop_(l), type_(t), option_(ops), closing_(false) {
DLOG_TRACE;
}
Client::~Client() {
DLOG_TRACE;
}
void Client::ConnectToNSQD(const std::string& addr) {
auto c = NSQConnPtr(new NSQConn(this, option_));
connecting_conns_[addr] = c;
c->SetMessageCallback(msg_fn_);
c->SetConnectionCallback(std::bind(&Client::OnConnection, this, std::placeholders::_1));
c->Connect(addr);
}
void Client::ConnectToNSQDs(const std::string& addrs/*host1:port1,host2:port2*/) {
std::vector<std::string> v;
evpp::StringSplit(addrs, ",", 0, v);
ConnectToNSQDs(v);
}
void Client::ConnectToNSQDs(const std::vector<std::string>& tcp_addrs/*host:port*/) {
for (auto it = tcp_addrs.begin(); it != tcp_addrs.end(); ++it) {
ConnectToNSQD(*it);
}
}
void Client::ConnectToLookupd(const std::string& lookupd_url/*http://127.0.0.1:4161/lookup?topic=test*/) {
auto f = [this, lookupd_url]() {
LOG_INFO << "query nsqlookupd " << lookupd_url;
std::shared_ptr<evpp::httpc::Request> r(new evpp::httpc::Request(this->loop_, lookupd_url, "", evpp::Duration(1.0)));
r->Execute(std::bind(&Client::HandleLoopkupdHTTPResponse, this, std::placeholders::_1, r));
};
// query nsqlookupd immediately right now
loop_->RunInLoop(f);
// query nsqlookupd periodic
auto timer = loop_->RunEvery(option_.query_nsqlookupd_interval, f);
lookupd_timers_.push_back(timer);
}
void Client::ConnectToLookupds(const std::string& lookupd_urls/*http://192.168.0.5:4161/lookup?topic=test,http://192.168.0.6:4161/lookup?topic=test*/) {
std::vector<std::string> v;
evpp::StringSplit(lookupd_urls, ",", 0, v);
for (auto it = v.begin(); it != v.end(); ++it) {
ConnectToLookupd(*it);
}
}
void Client::Close() {
DLOG_TRACE << "conns_.size=" << conns_.size() << " connecting_conns_.size=" << connecting_conns_.size();
closing_ = true;
auto f = [this]() {
ready_to_publish_fn_ = ReadyToPublishCallback();
for (auto it = this->conns_.begin(), ite = this->conns_.end(); it != ite; ++it) {
DLOG_TRACE << "Close connected NSQConn " << (*it).get() << (*it)->remote_addr();
(*it)->Close();
}
for (auto it = this->connecting_conns_.begin(), ite = this->connecting_conns_.end(); it != ite; ++it) {
DLOG_TRACE << "Close connecting NSQConn " << it->second.get() << it->second->remote_addr();
it->second->Close();
}
for (auto& timer : lookupd_timers_) {
timer->Cancel();
}
lookupd_timers_.clear();
};
// If we use loop_->RunInLoop(f), this may execute f in current loop
// and then it will callback Client::OnConnection to release NSQConn object.
// That will make the iterators in function f broken down.
// So we use loop_->QueueInLoop(f) to delay the execution time of f to next loop.
loop_->QueueInLoop(f);
}
bool Client::IsReady() const {
if (conns_.empty()) {
return false;
}
return conns_[0]->IsReady();
}
void Client::HandleLoopkupdHTTPResponse(
const std::shared_ptr<evpp::httpc::Response>& response,
const std::shared_ptr<evpp::httpc::Request>& request) {
DLOG_TRACE;
std::string body = response->body().ToString();
if (response->http_code() != 200) {
LOG_ERROR << "Request lookupd http://" << request->conn()->host() << ":"
<< request->conn()->port() << request->uri()
<< " failed, http-code=" << response->http_code()
<< " [" << body << "]";
return;
}
rapidjson::Document doc;
doc.Parse(body.c_str());
int status_code = doc["status_code"].GetInt();
if (status_code != 200) {
LOG_ERROR << "Request lookupd http://" << request->conn()->host()
<< ":" << request->conn()->port() << request->uri()
<< " failed: [" << body
<< "]. We will automatically retry later.";
return;
} else {
LOG_INFO << "lookupd response OK. http://"
<< request->conn()->host() << ":"
<< request->conn()->port() << request->uri()
<< " : " << body;
}
rapidjson::Value& producers = doc["data"]["producers"];
for (rapidjson::SizeType i = 0; i < producers.Size(); ++i) {
rapidjson::Value& producer = producers[i];
std::string broadcast_address = producer["broadcast_address"].GetString();
int tcp_port = producer["tcp_port"].GetInt();
std::string addr = broadcast_address + ":" + std::to_string(tcp_port);
if (!IsKnownNSQDAddress(addr)) {
ConnectToNSQD(addr);
}
}
}
void Client::OnConnection(const NSQConnPtr& conn) {
DLOG_TRACE << " NSQConn remote_addr=" << conn->remote_addr() << " status=" << conn->StatusToString();
if (conn->IsConnected() || conn->IsReady()) {
conns_.push_back(conn);
connecting_conns_.erase(conn->remote_addr());
switch (conn->status()) {
case NSQConn::kConnected:
if (type_ == kConsumer) {
conn->Subscribe(topic_, channel_);
} else {
assert(type_ == kProducer);
conn->set_status(NSQConn::kReady);
if (ready_to_publish_fn_) {
ready_to_publish_fn_(conn.get());
}
}
break;
case NSQConn::kReady:
assert(type_ == kConsumer);
break;
default:
break;
}
} else if (conn->IsConnecting()) {
MoveToConnectingList(conn);
} else {
// The application layer calls Close()
assert(conn->IsDisconnected());
// Delete this NSQConn
for (auto it = conns_.begin(), ite = conns_.end(); it != ite; ++it) {
if (*it == conn) {
conns_.erase(it);
break;
}
}
connecting_conns_.erase(conn->remote_addr());
if (connecting_conns_.empty() && conns_.empty()) {
if (close_fn_) {
close_fn_();
}
}
// Waiting for the status of NSQConn to be stable, and then execute the function f in next loop
auto f = [this, conn]() {
assert(conn->IsDisconnected());
if (!conn->IsDisconnected()) {
LOG_ERROR << "NSQConn status is not kDisconnected : " << int(conn->status());
}
};
loop_->QueueInLoop(f);
}
}
bool Client::IsKnownNSQDAddress(const std::string& addr) const {
if (connecting_conns_.find(addr) != connecting_conns_.end()) {
return true;
}
for (auto c : conns_) {
if (c->remote_addr() == addr) {
return true;
}
}
return false;
}
void Client::MoveToConnectingList(const NSQConnPtr& conn) {
NSQConnPtr& connecting_conn = connecting_conns_[conn->remote_addr()];
if (connecting_conn.get()) {
// This connection is already in the connecting list
// so do not need to remove it from conns_
return;
}
for (auto it = conns_.begin(), ite = conns_.end(); it != ite; ++it) {
if (*it == conn) {
connecting_conn = conn;
conns_.erase(it);
return;
}
}
}
}