-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.cpp
197 lines (164 loc) · 4.77 KB
/
client.cpp
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
extern "C" {
#ifdef _WIN32
#define WIN32_INTEROP_APIS_H
#define NO_QFORKIMPL
#include <Win32_Interop/win32fixes.h>
#pragma comment(lib, "hiredis.lib")
#pragma comment(lib, "Win32_Interop.lib")
#endif
}
#include <hiredis/hiredis.h>
#include <sstream>
#include <chrono>
using namespace std::chrono_literals;
#include "client.h"
namespace async_redis
{
client::client(size_t _piped_cache, uint32_t pipeline_timeout) :
cache_size(_piped_cache), pipe_timeout(pipeline_timeout), ctx(nullptr)
{
stopped = true;
}
client::~client()
{
Disconnect();
}
bool client::Connect(const std::string & host, int port, uint32_t timeout_ms)
{
if (IsConnected()) {
Disconnect();
}
if (timeout_ms > 0) {
struct timeval timeout;
timeout.tv_sec = (timeout_ms / 1000);
timeout.tv_usec = ((timeout_ms - (timeout.tv_sec * 1000)) * 1000);
ctx = redisConnectWithTimeout(host.c_str(), port, timeout);
} else {
ctx = redisConnect(host.c_str(), port);
}
if (ctx == nullptr || ctx->err) {
if (ctx) {
redisFree(ctx);
ctx = nullptr;
}
return false;
}
stopped = false;
worker = std::thread([this] {
std::vector<command_request> reqs;
while (!stopped) {
if (!ctx || ctx->err) {
stopped = true;
return;
}
auto send = [&] {
size_t queue_size = m_commands.size_approx();
if (!queue_size) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return;
}
queue_size = cache_size < queue_size ? queue_size : cache_size;
reqs.clear();
reqs.resize(queue_size);
size_t dequeued = 0;
if (pipe_timeout) {
dequeued = m_commands.wait_dequeue_bulk_timed(reqs.begin(), reqs.size(), std::chrono::milliseconds(pipe_timeout));
} else {
do {
dequeued += m_commands.wait_dequeue_bulk_timed(reqs.begin(), reqs.size() - dequeued, std::chrono::milliseconds(100));
} while (!stopped && dequeued < queue_size && !flush_pipeline);
}
if (!dequeued) {
return;
}
flush_pipeline = false;
std::vector<bool> appended;
for (size_t i = 0; i < dequeued; ++i) {
auto &req = reqs[i];
appended.emplace_back(
redisAppendFormattedCommand(ctx, req.command.c_str(), req.command.size()) == REDIS_OK
);
}
for (size_t i = 0; i < dequeued; ++i) {
auto &req = reqs[i];
redisReply *rawReply = nullptr;
bool success = appended[i];
if (success) {
success &= redisGetReply(ctx, (void **)&rawReply) == REDIS_OK;
}
if (req.callback) {
if (success) {
req.callback(new reply(rawReply));
} else {
req.callback(nullptr);
}
}
if (rawReply) {
freeReplyObject(rawReply);
}
}
};
if (cache_size == 0) {
if (!pipeline_sem.wait(100 * 1000)) {
continue;
}
}
{
send();
}
}
});
return true;
}
bool client::IsConnected() const
{
return ctx && ctx->err == 0 && !stopped;
}
void client::Disconnect()
{
stopped = true;
if (worker.joinable()) {
worker.join();
}
if (ctx) {
redisFree(ctx);
ctx = nullptr;
}
}
client & client::Append(const std::vector<std::string> &redis_cmd, const reply_callback & callback)
{
m_commands.enqueue({ formatCommand(redis_cmd), callback });
return *this;
}
client & client::Commit()
{
flush_pipeline = true;
if (IsConnected() && cache_size == 0) {
pipeline_sem.signal();
}
return *this;
}
int client::GetError() const
{
if (ctx) {
return ctx->err;
}
return -1;
}
const char *client::GetErrorString() const
{
if (ctx) {
return ctx->errstr;
}
return nullptr;
}
inline std::string client::formatCommand(const std::vector<std::string>& redis_cmd)
{
std::stringstream ss;
ss << "*" << redis_cmd.size() << "\r\n";
for (const auto &cmd_part : redis_cmd) {
ss << "$" << cmd_part.length() << "\r\n" << cmd_part << "\r\n";
}
return ss.str();
}
}