forked from OpenAtomFoundation/pika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pika_proxy_conn.cc
172 lines (144 loc) · 4.11 KB
/
pika_proxy_conn.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
// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#include "include/pika_proxy_conn.h"
#include "pink/include/redis_cli.h"
#include "include/pika_proxy.h"
extern PikaProxy* g_pika_proxy;
PikaProxyConn::PikaProxyConn(int fd, std::string ip_port,
pink::Thread* thread,
pink::PinkEpoll* pink_epoll,
std::shared_ptr<ProxyCli> proxy_cli)
: RedisConn(fd, ip_port, thread, pink_epoll,
pink::HandleType::kSynchronous, PIKA_MAX_CONN_RBUF_HB),
proxy_cli_(proxy_cli) {
}
int PikaProxyConn::DealMessage(
const pink::RedisCmdArgsType& argv, std::string* response) {
std::string res;
for (auto& arg : argv) {
res += arg;
}
g_pika_proxy->MayScheduleWritebackToCliConn(
std::dynamic_pointer_cast<PikaProxyConn>(shared_from_this()),
proxy_cli_, res);
return 0;
}
ParallelConn::ParallelConn(const std::string& addr, ConnConfig& config,
std::shared_ptr<pink::BackendThread> client)
: addr_(addr), config_(config), client_(client) {
refCount_ = 1;
}
Status ParallelConn::Connect() {
int num = parallelConn_.size() + tmpConns_.size();
if (num > config_.parallel_) {
return Status::OK();
}
for (int i = 0; i < num; i++) {
std::string ip;
int port, fd;
if (!slash::ParseIpPortString(addr_, ip, port)) {
LOG(INFO) << "parser addr " << addr_ << " error";
return Status::InvalidArgument("paser addr error, addr: ", addr_);
}
Status s = client_->Connect(ip, port, &fd);
if (!s.ok()) {
LOG(INFO) << "connect addr: " << addr_ << "error: " << s.ToString();
return s;
}
LOG(INFO) << "connect addr: " << addr_ << " fd: " << std::to_string(fd);
tmpConns_.insert(fd);
}
return Status::OK();
}
std::shared_ptr<pink::PinkConn> ParallelConn::GetConn(int fd) {
return client_->GetConn(fd);
}
void ParallelConn::VerifyAuth(int fd) {
}
void ParallelConn::SelectConn(int fd) {
}
void ParallelConn::KeepAlive() {
}
void ParallelConn::KeepAliveConn(int fd) {
}
Status ParallelConn::PrepareConn() {
for(auto item : tmpConns_) {
auto conn = std::dynamic_pointer_cast<PikaProxyConn>(GetConn(item));
if (conn->IsAuthed()) {
SelectConn(item);
} else {
VerifyAuth(item);
SelectConn(item);
}
}
return Status::OK();
}
Status ParallelConn::Start() {
Status s = Connect();
if (!s.ok()) {
return s;
}
s = PrepareConn();
if (!s.ok()) {
return s;
}
return Status::OK();
}
void ParallelConn::Close() {
for (auto item : parallelConn_) {
client_->Close(item.second);
}
parallelConn_.clear();
for (auto item : tmpConns_) {
client_->Close(item);
}
tmpConns_.clear();
}
void ParallelConn::Retain() {
int expect = 0;
if (refCount_.compare_exchange_strong(expect, -1)) {
LOG(INFO) << "retain parallel conn ref count error";
return;
}
refCount_++;
}
bool ParallelConn::Release() {
int expect = 0;
if (refCount_.compare_exchange_strong(expect, -1)) {
LOG(INFO) << "release parallel conn ref count error";
return true;
}
refCount_--;
if (refCount_.compare_exchange_strong(expect, -1)) {
return true;
}
return false;
}
void ConnectionPool::Release(std::string addr) {
if (pool_.find(addr) == pool_.end()) {
return;
}
auto parallel = pool_.find(addr)->second;
if (parallel->Release()) {
parallel->Close();
delete parallel;
pool_.erase(addr);
LOG(INFO) << "release parallel conn :" << parallel->Addr() << " table :"
<< std::to_string(parallel->GetTable());
}
}
void ConnectionPool::AddParallel(const std::string& addr) {
auto conns = new ParallelConn(addr, config_, client_);
pool_.insert(make_pair(addr, conns));
conns->Start();
}
void ConnectionPool::Retain(std::string addr) {
auto iter = pool_.find(addr);
if (iter != pool_.end()) {
iter->second->Retain();
return;
}
AddParallel(addr);
}