-
Notifications
You must be signed in to change notification settings - Fork 2
/
redis_plus_profile.cpp
208 lines (185 loc) · 9.14 KB
/
redis_plus_profile.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
198
199
200
201
202
203
204
205
206
207
208
#include "mod_redis_plus.h"
switch_status_t redis_plus_profile_create(redis_plus_profile_t **new_profile, char *name, uint8_t ignore_connect_fail,
uint8_t ignore_error, int max_pipelined_requests) {
redis_plus_profile_t *profile = NULL;
switch_memory_pool_t *pool = NULL;
switch_core_new_memory_pool(&pool);
profile = (redis_plus_profile_t *) switch_core_alloc(pool, sizeof(redis_plus_profile_t));
profile->pool = pool;
profile->name = name ? switch_core_strdup(profile->pool, name) : (char *) "default";
profile->connection = NULL;
profile->ignore_connect_fail = ignore_connect_fail;
profile->ignore_error = ignore_error;
profile->pipeline_running = 0;
profile->max_pipelined_requests = max_pipelined_requests;
switch_thread_rwlock_create(&profile->pipeline_lock, pool);
switch_queue_create(&profile->active_requests, 2000, pool);
switch_core_hash_insert(mod_redis_plus_globals.profiles, name, (void *) profile);
*new_profile = profile;
return SWITCH_STATUS_SUCCESS;
}
switch_status_t redis_plus_profile_destroy(redis_plus_profile_t **old_profile) {
redis_plus_profile_t *profile = NULL;
if (!old_profile || !*old_profile) {
return SWITCH_STATUS_SUCCESS;
} else {
profile = *old_profile;
*old_profile = NULL;
}
redis_plus_pipeline_threads_stop(profile);
profile->connection->redis_cluster = NULL;
profile->connection->master_redis = NULL;
profile->connection->slave_redis = NULL;
//profile->connection->pipeline = NULL;
switch_core_hash_delete(mod_redis_plus_globals.profiles, profile->name);
switch_core_destroy_memory_pool(&(profile->pool));
return SWITCH_STATUS_SUCCESS;
}
switch_status_t
redis_plus_profile_connection_add(redis_plus_profile_t *profile, char *host, char *password, uint32_t port,
uint32_t timeout_ms, uint32_t max_connections, uint32_t redis_type,
uint32_t pool_size, char *master_name, uint32_t sentinel_timeout_ms) {
redis_plus_connection_t *new_conn = NULL;
char *input = NULL;
if (!zstr(host)) {
input = strdup(host);
} else {
return SWITCH_STATUS_GENERR;
}
new_conn = (redis_plus_connection_t *) switch_core_alloc(profile->pool, sizeof(redis_plus_connection_t));
new_conn->host = host ? switch_core_strdup(profile->pool, host) : (char *) "localhost";
new_conn->password = password ? switch_core_strdup(profile->pool, password) : NULL;
new_conn->port = port ? port : 6379;
new_conn->pool = profile->pool;
new_conn->redis_type = redis_type;
new_conn->pool_size = pool_size;
if (timeout_ms) {
new_conn->timeout_us = timeout_ms * 1000;
new_conn->timeout.tv_sec = timeout_ms / 1000;
new_conn->timeout.tv_usec = (timeout_ms % 1000) * 1000;
} else {
new_conn->timeout_us = 500 * 1000;
new_conn->timeout.tv_sec = 0;
new_conn->timeout.tv_usec = 500 * 1000;
}
ConnectionOptions opts;
if (redis_type != 2) {
opts.host = new_conn->host;
opts.port = new_conn->port;
}
opts.password = new_conn->password;
opts.connect_timeout = std::chrono::milliseconds(new_conn->timeout_us); // Required.
opts.socket_timeout = std::chrono::milliseconds(new_conn->timeout_us); // Required.
ConnectionPoolOptions pool_opts;
pool_opts.size = 3;
try {
if (redis_type == 0) {
auto redis = new Redis(opts, pool_opts);
new_conn->master_redis = std::unique_ptr<Redis>(redis);
} else if (redis_type == 1) {
auto redis_cluster = new RedisCluster(opts, pool_opts);
new_conn->redis_cluster = std::unique_ptr<RedisCluster>(redis_cluster);
} else if (redis_type == 2) {
// read hosts
char *sentinel_host = NULL;
bool flag = true;
SentinelOptions sentinel_opts;
while (flag) {
char *host = input, *port = NULL, *tmp = NULL;
if ((sentinel_host = strchr(input, ','))) {
*sentinel_host = '\0';
sentinel_host++;
} else {
flag = false;
}
if ((tmp = strchr(input, ':'))) {
*tmp = '\0';
port = ++tmp;
input = sentinel_host;
sentinel_opts.nodes.push_back(std::make_pair<std::string, int>(std::string(host), atoi(port)));
}
}
if (master_name == nullptr) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "redis_plus: sentinel type need master_name!");
goto done;
}
sentinel_opts.connect_timeout = std::chrono::milliseconds(sentinel_timeout_ms);
sentinel_opts.socket_timeout = std::chrono::milliseconds(sentinel_timeout_ms);
auto sentinel = std::make_shared<Sentinel>(sentinel_opts);
auto master_redis = new Redis(sentinel, std::string(master_name), Role::MASTER, opts, pool_opts);
auto slave_redis = new Redis(sentinel, std::string(master_name), Role::SLAVE, opts, pool_opts);
new_conn->master_redis = std::unique_ptr<Redis>(master_redis);
new_conn->slave_redis = std::unique_ptr<Redis>(slave_redis);
}
}catch(std::exception &e) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "redis_plus can't create connection: %s", e.what());
goto done;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "redis_plus: adding conn[%s,%d], pool size = %d\n",
new_conn->host, new_conn->port, pool_size);
profile->connection = new_conn;
redis_plus_pipeline_thread_start(profile);
done:
switch_safe_free(input);
return SWITCH_STATUS_SUCCESS;
}
switch_status_t
redis_plus_profile_execute(redis_plus_profile_t *profile, switch_core_session_t *session, char **response,
const char *data) {
redis_plus_connection_t *conn = profile->connection;
ReplyUPtr resp;
if (conn) {
std::vector <std::string> commands = get_commands((char*)data);
try {
if (conn->redis_type == 0) {
resp = conn->master_redis->command(commands.begin(), commands.end());
} else if (conn->redis_type == 1) {
resp = conn->redis_cluster->command(commands.begin(), commands.end());
} else if (conn->redis_type == 2) {
if (data == NULL || strlen(data) <= 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "redis_plus: error command\n");
*response = switch_mprintf("%lld", -1);
return SWITCH_STATUS_GENERR;
}
if (strstr(data, "GET") != nullptr || strstr(data, "EXISTS")
|| strstr(data, "get") != nullptr || strstr(data, "exists")) {
try {
resp = conn->slave_redis->command(commands.begin(), commands.end());
} catch (const std::exception &e) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "redis_plus slave error: %s", e.what());
resp = conn->master_redis->command(commands.begin(), commands.end());
}
} else {
resp = conn->master_redis->command(commands.begin(), commands.end());
}
}
if (reply::is_integer(*resp)) {
auto int_resp = reply::parse<long long>(*resp);
*response = switch_mprintf("%lld", int_resp);
} else if (reply::is_string(*resp)) {
auto option_resp = reply::parse<OptionalString>(*resp);
*response = strdup(option_resp.value().c_str());
#ifdef REDIS_PLUS_PLUS_RESP_VERSION_3
} else if (reply::is_double(*resp)) {
auto double_resp = reply::parse<double>(*resp);
*response = switch_mprintf("%f", double_resp);
} else if (reply::is_bool(*resp)) {
auto bool_resp = reply::parse<long long>(*resp);
*response = switch_mprintf("%lld", bool_resp);
#endif
} else {
auto option_resp = reply::parse<OptionalString>(*resp);
*response = strdup(option_resp.value().c_str());
}
return SWITCH_STATUS_SUCCESS;
} catch (const ReplyError &e) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "redis_plus can't get reply: %s", e.what());
} catch (const std::exception &e) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "redis_plus unkown error: %s", e.what());
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "redis_plus: can't find redis connection\n");
}
*response = switch_mprintf("%lld", -2);
return SWITCH_STATUS_GENERR;
}