-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod_redis_plus.cpp
418 lines (352 loc) · 17.4 KB
/
mod_redis_plus.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include "mod_redis_plus.h"
mod_redis_plus_global_t mod_redis_plus_globals;
SWITCH_MODULE_LOAD_FUNCTION(mod_redis_plus_load);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_redis_plus_shutdown);
SWITCH_MODULE_DEFINITION(mod_redis_plus, mod_redis_plus_load, mod_redis_plus_shutdown, NULL);
/**
* Get exclusive access to limit_pvt, if it exists
*/
static redis_plus_limit_pvt_t *get_limit_pvt(switch_core_session_t *session)
{
switch_channel_t *channel = switch_core_session_get_channel(session);
redis_plus_limit_pvt_t *limit_pvt = (redis_plus_limit_pvt_t*)switch_channel_get_private(channel, "hiredis_limit_pvt");
if (limit_pvt) {
/* pvt already exists, return it */
switch_mutex_lock(limit_pvt->mutex);
return limit_pvt;
}
return NULL;
}
/**
* Add limit_pvt and get exclusive access to it
*/
static redis_plus_limit_pvt_t *add_limit_pvt(switch_core_session_t *session)
{
switch_channel_t *channel = switch_core_session_get_channel(session);
redis_plus_limit_pvt_t *limit_pvt = (redis_plus_limit_pvt_t*)switch_channel_get_private(channel, "hiredis_limit_pvt");
if (limit_pvt) {
/* pvt already exists, return it */
switch_mutex_lock(limit_pvt->mutex);
return limit_pvt;
}
/* not created yet, add it - NOTE a channel mutex would be better here if we had access to it */
switch_mutex_lock(mod_redis_plus_globals.limit_pvt_mutex);
limit_pvt = (redis_plus_limit_pvt_t*)switch_channel_get_private(channel, "hiredis_limit_pvt");
if (limit_pvt) {
/* was just added by another thread */
switch_mutex_unlock(mod_redis_plus_globals.limit_pvt_mutex);
switch_mutex_lock(limit_pvt->mutex);
return limit_pvt;
}
/* still not created yet, add it */
limit_pvt = (redis_plus_limit_pvt_t*)switch_core_session_alloc(session, sizeof(*limit_pvt));
switch_mutex_init(&limit_pvt->mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
limit_pvt->first = NULL;
switch_channel_set_private(channel, "hiredis_limit_pvt", (void*)limit_pvt);
switch_mutex_unlock(mod_redis_plus_globals.limit_pvt_mutex);
switch_mutex_lock(limit_pvt->mutex);
return limit_pvt;
}
/**
* Release exclusive acess to limit_pvt
*/
static void release_limit_pvt(redis_plus_limit_pvt_t *limit_pvt)
{
if (limit_pvt) {
switch_mutex_unlock(limit_pvt->mutex);
}
}
SWITCH_STANDARD_APP(raw_app)
{
switch_channel_t *channel = switch_core_session_get_channel(session);
char *response = NULL, *profile_name = NULL, *cmd = NULL;
redis_plus_profile_t *profile = NULL;
if ( !zstr(data) ) {
profile_name = strdup(data);
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "hiredis: invalid data! Use the format 'default set keyname value' \n");
goto done;
}
if ( (cmd = strchr(profile_name, ' '))) {
*cmd = '\0';
cmd++;
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "hiredis: invalid data! Use the format 'default set keyname value' \n");
goto done;
}
profile = (redis_plus_profile_t*)switch_core_hash_find(mod_redis_plus_globals.profiles, profile_name);
if ( !profile ) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "hiredis: Unable to locate profile[%s]\n", profile_name);
return;
}
if ( redis_plus_profile_execute(profile, session, &response, cmd) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "hiredis: profile[%s] error executing [%s] because [%s]\n", profile_name, cmd, response ? response : "");
}
switch_channel_set_variable(channel, "hiredis_raw_response", response ? response : "");
done:
switch_safe_free(profile_name);
switch_safe_free(response);
return;
}
SWITCH_STANDARD_API(raw_api)
{
redis_plus_profile_t *profile = NULL;
char *data = NULL, *input = NULL, *response = NULL;
switch_status_t status = SWITCH_STATUS_SUCCESS;
if ( !zstr(cmd) ) {
input = strdup(cmd);
} else {
switch_goto_status(SWITCH_STATUS_GENERR, done);
}
if ( (data = strchr(input, ' '))) {
*data = '\0';
data++;
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "hiredis: debug: profile[%s] for command [%s]\n", input, data);
profile = (redis_plus_profile_t*)switch_core_hash_find(mod_redis_plus_globals.profiles, input);
if ( !profile ) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "hiredis: Unable to locate profile[%s]\n", input);
switch_goto_status(SWITCH_STATUS_GENERR, done);
}
if ( redis_plus_profile_execute(profile, session, &response, data) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "hiredis: profile[%s] error executing [%s] reason:[%s]\n", input, data, response ? response : "");
switch_goto_status(SWITCH_STATUS_GENERR, done);
}
if (response) {
stream->write_function(stream, response);
}
done:
switch_safe_free(input);
switch_safe_free(response);
return status;
}
/*
SWITCH_LIMIT_INCR(name) static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource,
const int max, const int interval)
*/
SWITCH_LIMIT_INCR(redis_plus_limit_incr)
{
switch_channel_t *channel = switch_core_session_get_channel(session);
redis_plus_profile_t *profile = NULL;
char *response = NULL, *limit_key = NULL;
int64_t count = 0; /* Redis defines the incr action as to be performed on a 64 bit signed integer */
time_t now = switch_epoch_time_now(NULL);
switch_status_t status = SWITCH_STATUS_SUCCESS;
redis_plus_limit_pvt_t *limit_pvt = NULL;
redis_plus_limit_pvt_node_t *limit_pvt_node = NULL;
switch_memory_pool_t *session_pool = switch_core_session_get_pool(session);
if ( zstr(realm) ) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "hiredis: realm must be defined\n");
switch_goto_status(SWITCH_STATUS_GENERR, done);
}
if ( interval < 0 ) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "hiredis: interval must be >= 0\n");
switch_goto_status(SWITCH_STATUS_GENERR, done);
}
profile = (redis_plus_profile_t*)switch_core_hash_find(mod_redis_plus_globals.profiles, realm);
if ( !profile ) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "hiredis: Unable to locate profile[%s]\n", realm);
switch_goto_status(SWITCH_STATUS_GENERR, done);
}
if ( interval ) {
limit_key = switch_core_session_sprintf(session, "%s_%d", resource, now / interval);
} else {
limit_key = switch_core_session_sprintf(session, "%s", resource);
}
if ( (status = redis_plus_profile_execute_pipeline_printf(profile, session, &response, "incr %s", limit_key) ) != SWITCH_STATUS_SUCCESS ) {
if ( status == SWITCH_STATUS_SOCKERR && profile->ignore_connect_fail) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "hiredis: ignoring profile[%s] connection error incrementing [%s]\n", realm, limit_key);
switch_goto_status(SWITCH_STATUS_SUCCESS, done);
} else if ( profile->ignore_error ) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "hiredis: ignoring profile[%s] general error incrementing [%s]\n", realm, limit_key);
switch_goto_status(SWITCH_STATUS_SUCCESS, done);
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "hiredis: profile[%s] error incrementing [%s] because [%s]\n", realm, limit_key, response ? response : "");
switch_channel_set_variable(channel, "hiredis_raw_response", response ? response : "");
switch_goto_status(SWITCH_STATUS_GENERR, done);
}
/* set expiration for interval on first increment */
if ( interval && !strcmp("1", response ? response : "") ) {
redis_plus_profile_execute_pipeline_printf(profile, session, NULL, "expire %s %d", limit_key, interval);
}
switch_channel_set_variable(channel, "hiredis_raw_response", response ? response : "");
count = atoll(response ? response : "");
if ( switch_is_number(response ? response : "") && count <= 0 ) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "limit not positive after increment, resource = %s, val = %s\n", limit_key, response ? response : "");
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "resource = %s, response = %s\n", limit_key, response ? response : "");
}
if ( !switch_is_number(response ? response : "") && !profile->ignore_error ) {
/* got response error */
switch_goto_status(SWITCH_STATUS_GENERR, done);
} else if ( max > 0 && count > 0 && count > max ) {
switch_channel_set_variable(channel, "hiredis_limit_exceeded", "true");
if ( !interval ) { /* don't need to decrement intervals if limit exceeded since the interval keys are named w/ timestamp */
redis_plus_profile_execute_pipeline_printf(profile, session, NULL, "decr %s", limit_key);
}
switch_goto_status(SWITCH_STATUS_GENERR, done);
}
if ( !interval && count > 0 ) {
/* only non-interval limits need to be released on session destroy */
limit_pvt_node = (redis_plus_limit_pvt_node_t*)switch_core_alloc(session_pool, sizeof(*limit_pvt_node));
limit_pvt_node->realm = switch_core_strdup(session_pool, realm);
limit_pvt_node->resource = switch_core_strdup(session_pool, resource);
limit_pvt_node->limit_key = limit_key;
limit_pvt_node->inc = 1;
limit_pvt_node->interval = interval;
limit_pvt = add_limit_pvt(session);
limit_pvt_node->next = limit_pvt->first;
limit_pvt->first = limit_pvt_node;
release_limit_pvt(limit_pvt);
}
done:
switch_safe_free(response);
return status;
}
/*
SWITCH_LIMIT_RELEASE(name) static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource)
*/
SWITCH_LIMIT_RELEASE(redis_plus_limit_release)
{
switch_channel_t *channel = switch_core_session_get_channel(session);
redis_plus_profile_t *profile = NULL;
char *response = NULL;
switch_status_t status = SWITCH_STATUS_SUCCESS;
redis_plus_limit_pvt_t *limit_pvt = get_limit_pvt(session);
if (!limit_pvt) {
/* nothing to release */
return SWITCH_STATUS_SUCCESS;
}
/* If realm and resource are NULL, then clear all of the limits */
if ( zstr(realm) && zstr(resource) ) {
redis_plus_limit_pvt_node_t *cur = NULL;
for ( cur = limit_pvt->first; cur; cur = cur->next ) {
/* Rate limited resources are not auto-decremented, they will expire. */
if ( !cur->interval && cur->inc ) {
switch_status_t result;
cur->inc = 0; /* mark as released */
profile = (redis_plus_profile_t*)switch_core_hash_find(mod_redis_plus_globals.profiles, cur->realm);
result = redis_plus_profile_execute_pipeline_printf(profile, session, &response, "decr %s", cur->limit_key);
if ( result != SWITCH_STATUS_SUCCESS ) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "hiredis: profile[%s] error decrementing [%s] because [%s]\n",
cur->realm, cur->limit_key, response ? response : "");
}
switch_safe_free(response);
response = NULL;
}
}
} else if (!zstr(resource) ) {
/* clear single non-interval resource */
redis_plus_limit_pvt_node_t *cur = NULL;
for (cur = limit_pvt->first; cur; cur = cur->next ) {
if ( !cur->interval && cur->inc && !strcmp(cur->resource, resource) && (zstr(realm) || !strcmp(cur->realm, realm)) ) {
/* found the resource to clear */
cur->inc = 0; /* mark as released */
profile = (redis_plus_profile_t*)switch_core_hash_find(mod_redis_plus_globals.profiles, cur->realm);
if (profile) {
status = redis_plus_profile_execute_pipeline_printf(profile, session, &response, "decr %s", cur->limit_key);
if ( status != SWITCH_STATUS_SUCCESS ) {
if ( status == SWITCH_STATUS_SOCKERR && profile->ignore_connect_fail ) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "hiredis: ignoring profile[%s] connection error decrementing [%s]\n", cur->realm, cur->limit_key);
switch_goto_status(SWITCH_STATUS_SUCCESS, done);
} else if ( profile->ignore_error ) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "hiredis: ignoring profile[%s] general error decrementing [%s]\n", realm, cur->limit_key);
switch_goto_status(SWITCH_STATUS_SUCCESS, done);
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "hiredis: profile[%s] error decrementing [%s] because [%s]\n", realm, cur->limit_key, response ? response : "");
switch_channel_set_variable(channel, "hiredis_raw_response", response ? response : "");
switch_goto_status(SWITCH_STATUS_GENERR, done);
}
switch_channel_set_variable(channel, "hiredis_raw_response", response ? response : "");
}
break;
}
}
}
done:
release_limit_pvt(limit_pvt);
switch_safe_free(response);
return status;
}
/*
SWITCH_LIMIT_USAGE(name) static int name (const char *realm, const char *resource, uint32_t *rcount)
*/
SWITCH_LIMIT_USAGE(redis_plus_limit_usage)
{
redis_plus_profile_t *profile = (redis_plus_profile_t*)switch_core_hash_find(mod_redis_plus_globals.profiles, realm);
int64_t count = 0; /* Redis defines the incr action as to be performed on a 64 bit signed integer */
char *response = NULL;
if ( zstr(realm) ) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: realm must be defined\n");
goto err;
}
if ( !profile ) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: Unable to locate profile[%s]\n", realm);
goto err;
}
if ( redis_plus_profile_execute_pipeline_printf(profile, NULL, &response, "get %s", resource) != SWITCH_STATUS_SUCCESS ) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: profile[%s] error querying [%s] because [%s]\n", realm, resource, response ? response : "");
goto err;
}
count = atoll(response ? response : "");
switch_safe_free(response);
return count;
err:
switch_safe_free(response);
return -1;
}
/*
SWITCH_LIMIT_RESET(name) static switch_status_t name (void)
*/
SWITCH_LIMIT_RESET(redis_plus_limit_reset)
{
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: unable to globally reset hiredis limit resources. Use 'hiredis_raw set resource_name 0'\n");
return SWITCH_STATUS_NOTIMPL;
}
/*
SWITCH_LIMIT_INTERVAL_RESET(name) static switch_status_t name (const char *realm, const char *resource)
*/
SWITCH_LIMIT_INTERVAL_RESET(redis_plus_limit_interval_reset)
{
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: unable to reset hiredis interval limit resources.\n");
return SWITCH_STATUS_NOTIMPL;
}
/*
SWITCH_LIMIT_STATUS(name) static char * name (void)
*/
SWITCH_LIMIT_STATUS(redis_plus_limit_status)
{
return strdup("-ERR not supported");
}
SWITCH_MODULE_LOAD_FUNCTION(mod_redis_plus_load)
{
switch_application_interface_t *app_interface;
switch_api_interface_t *api_interface;
switch_limit_interface_t *limit_interface;
memset(&mod_redis_plus_globals, 0, sizeof(mod_redis_plus_globals));
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
mod_redis_plus_globals.pool = pool;
switch_mutex_init(&mod_redis_plus_globals.limit_pvt_mutex, SWITCH_MUTEX_NESTED, pool);
switch_core_hash_init(&(mod_redis_plus_globals.profiles));
if ( mod_redis_plus_do_config() != SWITCH_STATUS_SUCCESS ) {
return SWITCH_STATUS_GENERR;
}
SWITCH_ADD_LIMIT(limit_interface, "redis_plus", redis_plus_limit_incr, redis_plus_limit_release, redis_plus_limit_usage,
redis_plus_limit_reset, redis_plus_limit_status, redis_plus_limit_interval_reset);
SWITCH_ADD_APP(app_interface, "hiredis_raw", "hiredis_raw", "hiredis_raw", raw_app, "", SAF_SUPPORT_NOMEDIA | SAF_ROUTING_EXEC | SAF_ZOMBIE_EXEC);
SWITCH_ADD_API(api_interface, "hiredis_raw", "hiredis_raw", raw_api, "");
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_redis_plus_shutdown)
{
switch_hash_index_t *hi;
redis_plus_profile_t *profile = NULL;
/* loop through profiles, and destroy them */
while ((hi = switch_core_hash_first(mod_redis_plus_globals.profiles))) {
switch_core_hash_this(hi, NULL, NULL, (void **)&profile);
redis_plus_profile_destroy(&profile);
switch_safe_free(hi);
}
switch_core_hash_destroy(&(mod_redis_plus_globals.profiles));
return SWITCH_STATUS_SUCCESS;
}