提交 39f6d107 authored 作者: William King's avatar William King

FS-8377 Adding expanded support for limit_* functionality for mod_hiredis

上级 80111e7d
...@@ -122,10 +122,12 @@ SWITCH_LIMIT_INCR(hiredis_limit_incr) ...@@ -122,10 +122,12 @@ SWITCH_LIMIT_INCR(hiredis_limit_incr)
{ {
switch_channel_t *channel = switch_core_session_get_channel(session); switch_channel_t *channel = switch_core_session_get_channel(session);
hiredis_profile_t *profile = NULL; hiredis_profile_t *profile = NULL;
char *hashkey = NULL, *response = NULL; char *hashkey = NULL, *response = NULL, *limit_key = NULL;
int64_t count = 0; /* Redis defines the incr action as to be performed on a 64 bit signed integer */ 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); time_t now = switch_epoch_time_now(NULL);
switch_status_t status = SWITCH_STATUS_SUCCESS; switch_status_t status = SWITCH_STATUS_SUCCESS;
hiredis_limit_pvt_t *limit_pvt = NULL;
switch_memory_pool_t *session_pool = switch_core_session_get_pool(session);
if ( zstr(realm) ) { if ( zstr(realm) ) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: realm must be defined\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: realm must be defined\n");
...@@ -140,11 +142,13 @@ SWITCH_LIMIT_INCR(hiredis_limit_incr) ...@@ -140,11 +142,13 @@ SWITCH_LIMIT_INCR(hiredis_limit_incr)
} }
if ( interval ) { if ( interval ) {
hashkey = switch_mprintf("incr %s_%d", resource, now % interval); limit_key = switch_mprintf("%s_%d", resource, now % interval);
} else { } else {
hashkey = switch_mprintf("incr %s", resource); limit_key = switch_mprintf("%s", resource);
} }
hashkey = switch_mprintf("incr %s", limit_key);
if ( hiredis_profile_execute_sync(profile, hashkey, &response) != SWITCH_STATUS_SUCCESS) { if ( hiredis_profile_execute_sync(profile, hashkey, &response) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: profile[%s] error executing [%s] because [%s]\n", realm, hashkey, response); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: profile[%s] error executing [%s] because [%s]\n", realm, hashkey, response);
switch_channel_set_variable(channel, "hiredis_raw_response", response); switch_channel_set_variable(channel, "hiredis_raw_response", response);
...@@ -153,6 +157,14 @@ SWITCH_LIMIT_INCR(hiredis_limit_incr) ...@@ -153,6 +157,14 @@ SWITCH_LIMIT_INCR(hiredis_limit_incr)
switch_channel_set_variable(channel, "hiredis_raw_response", response); switch_channel_set_variable(channel, "hiredis_raw_response", response);
limit_pvt = switch_core_alloc(session_pool, sizeof(hiredis_limit_pvt_t));
limit_pvt->next = switch_channel_get_private(channel, "hiredis_limit_pvt");
limit_pvt->realm = switch_core_strdup(session_pool, realm);
limit_pvt->resource = switch_core_strdup(session_pool, resource);
limit_pvt->limit_key = switch_core_strdup(session_pool, limit_key);
limit_pvt->inc = 1;
switch_channel_set_private(channel, "hiredis_limit_pvt", limit_pvt);
count = atoll(response); count = atoll(response);
if ( !count || count > max ) { if ( !count || count > max ) {
...@@ -160,6 +172,7 @@ SWITCH_LIMIT_INCR(hiredis_limit_incr) ...@@ -160,6 +172,7 @@ SWITCH_LIMIT_INCR(hiredis_limit_incr)
} }
done: done:
switch_safe_free(limit_key);
switch_safe_free(response); switch_safe_free(response);
switch_safe_free(hashkey); switch_safe_free(hashkey);
return status; return status;
...@@ -174,6 +187,7 @@ SWITCH_LIMIT_RELEASE(hiredis_limit_release) ...@@ -174,6 +187,7 @@ SWITCH_LIMIT_RELEASE(hiredis_limit_release)
hiredis_profile_t *profile = switch_core_hash_find(mod_hiredis_globals.profiles, realm); hiredis_profile_t *profile = switch_core_hash_find(mod_hiredis_globals.profiles, realm);
char *hashkey = NULL, *response = NULL; char *hashkey = NULL, *response = NULL;
switch_status_t status = SWITCH_STATUS_SUCCESS; switch_status_t status = SWITCH_STATUS_SUCCESS;
hiredis_limit_pvt_t *limit_pvt = switch_channel_get_private(channel, "hiredis_limit_pvt");
if ( !zstr(realm) ) { if ( !zstr(realm) ) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: realm must be defined\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: realm must be defined\n");
...@@ -185,12 +199,26 @@ SWITCH_LIMIT_RELEASE(hiredis_limit_release) ...@@ -185,12 +199,26 @@ SWITCH_LIMIT_RELEASE(hiredis_limit_release)
switch_goto_status(SWITCH_STATUS_GENERR, done); switch_goto_status(SWITCH_STATUS_GENERR, done);
} }
/* If realm and resource are NULL, then clear all of the limits */
if ( !realm && !resource ) { if ( !realm && !resource ) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis does not yet support release on NULL realm[%s] and resource[%s]\n", realm, resource); hiredis_limit_pvt_t *tmp = limit_pvt;
switch_goto_status(SWITCH_STATUS_GENERR, done);
while (tmp) {
hashkey = switch_mprintf("decr %s", tmp->limit_key);
limit_pvt = tmp->next;
if ( hiredis_profile_execute_sync(profile, hashkey, &response) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: profile[%s] error executing [%s] because [%s]\n",
tmp->realm, hashkey, response);
} }
hashkey = switch_mprintf("decr %s", resource); tmp = limit_pvt;
switch_safe_free(response);
switch_safe_free(hashkey);
}
} else {
hashkey = switch_mprintf("decr %s", limit_pvt->limit_key);
if ( hiredis_profile_execute_sync(profile, hashkey, &response) != SWITCH_STATUS_SUCCESS) { if ( hiredis_profile_execute_sync(profile, hashkey, &response) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: profile[%s] error executing [%s] because [%s]\n", realm, hashkey, response); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: profile[%s] error executing [%s] because [%s]\n", realm, hashkey, response);
...@@ -199,6 +227,7 @@ SWITCH_LIMIT_RELEASE(hiredis_limit_release) ...@@ -199,6 +227,7 @@ SWITCH_LIMIT_RELEASE(hiredis_limit_release)
} }
switch_channel_set_variable(channel, "hiredis_raw_response", response); switch_channel_set_variable(channel, "hiredis_raw_response", response);
}
done: done:
switch_safe_free(response); switch_safe_free(response);
......
...@@ -32,6 +32,14 @@ typedef struct hiredis_profile_s { ...@@ -32,6 +32,14 @@ typedef struct hiredis_profile_s {
hiredis_connection_t *conn_head; hiredis_connection_t *conn_head;
} hiredis_profile_t; } hiredis_profile_t;
typedef struct hiredis_limit_pvt_s {
char *realm;
char *resource;
char *limit_key;
int inc;
struct hiredis_limit_pvt_s *next;
} hiredis_limit_pvt_t;
switch_status_t mod_hiredis_do_config(); switch_status_t mod_hiredis_do_config();
switch_status_t hiredis_profile_create(hiredis_profile_t **new_profile, char *name, uint8_t port); switch_status_t hiredis_profile_create(hiredis_profile_t **new_profile, char *name, uint8_t port);
switch_status_t hiredis_profile_destroy(hiredis_profile_t **old_profile); switch_status_t hiredis_profile_destroy(hiredis_profile_t **old_profile);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论