FS-9054 [mod_hiredis] add ignore-connect-fail profile param so that calls do not get killed if limit fails due to lost connection

This commit is contained in:
Chris Rienzo 2016-04-12 09:44:17 -04:00
parent beada9b0cb
commit a0f347fca9
5 changed files with 25 additions and 16 deletions

View File

@ -16,7 +16,7 @@
</connection>
</connections>
<params>
<param name="debug" value="1"/>
<param name="ignore-connect-fail" value="true"/>
</params>
</profile>
</profiles>

View File

@ -93,7 +93,7 @@ static hiredis_context_t *hiredis_connection_get_context(hiredis_connection_t *c
return NULL;
}
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 ignore_connect_fail)
{
hiredis_profile_t *profile = NULL;
switch_memory_pool_t *pool = NULL;
@ -105,6 +105,7 @@ switch_status_t hiredis_profile_create(hiredis_profile_t **new_profile, char *na
profile->pool = pool;
profile->name = name ? switch_core_strdup(profile->pool, name) : "default";
profile->conn_head = NULL;
profile->ignore_connect_fail = ignore_connect_fail;
switch_core_hash_insert(mod_hiredis_globals.profiles, name, (void *) profile);
@ -260,7 +261,7 @@ switch_status_t hiredis_profile_execute_sync(hiredis_profile_t *profile, const c
return SWITCH_STATUS_GENERR;
}
}
return SWITCH_STATUS_GENERR;
return SWITCH_STATUS_SOCKERR;
}

View File

@ -45,20 +45,20 @@ switch_status_t mod_hiredis_do_config()
if ( (profiles = switch_xml_child(cfg, "profiles")) != NULL) {
for (profile = switch_xml_child(profiles, "profile"); profile; profile = profile->next) {
hiredis_profile_t *new_profile = NULL;
int debug = 0;
uint8_t ignore_connect_fail = 0;
char *name = (char *) switch_xml_attr_soft(profile, "name");
// Load params
if ( (params = switch_xml_child(profile, "params")) != NULL) {
for (param = switch_xml_child(params, "param"); param; param = param->next) {
char *var = (char *) switch_xml_attr_soft(param, "name");
if ( ! strncmp(var, "debug", 5) ) {
debug = atoi(switch_xml_attr_soft(param, "value"));
if ( !strncmp(var, "ignore-connect-fail", 19) ) {
ignore_connect_fail = switch_true(switch_xml_attr_soft(param, "value"));
}
}
}
if ( hiredis_profile_create(&new_profile, name, debug) == SWITCH_STATUS_SUCCESS) {
if ( hiredis_profile_create(&new_profile, name, ignore_connect_fail) == SWITCH_STATUS_SUCCESS ) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Created profile[%s]\n", name);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create profile[%s]\n", name);
@ -77,7 +77,7 @@ switch_status_t mod_hiredis_do_config()
} else if ( !strncmp(var, "port", 4) ) {
port = atoi(switch_xml_attr_soft(param, "value"));
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "hiredis: adding conn[%u == %s]\n", port, switch_xml_attr_soft(param, "value"));
} else if ( !strncmp(var, "timeout_ms", 10) ) {
} else if ( !strncmp(var, "timeout-ms", 10) || !strncmp(var, "timeout_ms", 10) ) {
timeout_ms = atoi(switch_xml_attr_soft(param, "value"));
} else if ( !strncmp(var, "password", 8) ) {
password = (char *) switch_xml_attr_soft(param, "value");

View File

@ -1,6 +1,6 @@
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2012, Anthony Minessale II <anthm@freeswitch.org>
* Copyright (C) 2005-2016, Anthony Minessale II <anthm@freeswitch.org>
*
* Version: MPL 1.1
*
@ -23,6 +23,7 @@
*
* Contributor(s):
* William King <william.king@quentustech.com>
* Chris Rienzo <chris.rienzo@citrix.com>
*
* mod_hiredis.c -- Redis DB access module
*
@ -149,9 +150,13 @@ SWITCH_LIMIT_INCR(hiredis_limit_incr)
hashkey = switch_mprintf("incr %s", limit_key);
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_channel_set_variable(channel, "hiredis_raw_response", response);
if ( (status = hiredis_profile_execute_sync(profile, hashkey, &response)) != SWITCH_STATUS_SUCCESS ) {
if ( status == SWITCH_STATUS_SOCKERR && profile->ignore_connect_fail ) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "hiredis: ignoring profile[%s] connection error executing [%s]\n", realm, hashkey);
switch_goto_status(SWITCH_STATUS_SUCCESS, done);
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: profile[%s] error executing [%s] because [%s]\n", realm, hashkey, response ? response : "");
switch_channel_set_variable(channel, "hiredis_raw_response", response ? response : "");
switch_goto_status(SWITCH_STATUS_GENERR, done);
}
@ -212,7 +217,11 @@ SWITCH_LIMIT_RELEASE(hiredis_limit_release)
hashkey = switch_mprintf("decr %s", limit_pvt->limit_key);
if ( hiredis_profile_execute_sync(profile, hashkey, &response) != SWITCH_STATUS_SUCCESS) {
if ( ( status = hiredis_profile_execute_sync(profile, hashkey, &response) ) != SWITCH_STATUS_SUCCESS ) {
if ( status == SWITCH_STATUS_SOCKERR && profile->ignore_connect_fail ) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "hiredis: ignoring profile[%s] connection error executing [%s]\n", realm, hashkey);
switch_goto_status(SWITCH_STATUS_SUCCESS, done);
}
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_goto_status(SWITCH_STATUS_GENERR, done);

View File

@ -8,7 +8,6 @@
typedef struct mod_hiredis_global_s {
switch_memory_pool_t *pool;
switch_hash_t *profiles;
uint8_t debug;
} mod_hiredis_global_t;
extern mod_hiredis_global_t mod_hiredis_globals;
@ -33,7 +32,7 @@ typedef struct hiredis_connection_s {
typedef struct hiredis_profile_s {
switch_memory_pool_t *pool;
char *name;
int debug;
uint8_t ignore_connect_fail;
hiredis_connection_t *conn_head;
} hiredis_profile_t;
@ -48,7 +47,7 @@ typedef struct hiredis_limit_pvt_s {
} hiredis_limit_pvt_t;
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 ignore_connect_fail);
switch_status_t hiredis_profile_destroy(hiredis_profile_t **old_profile);
switch_status_t hiredis_profile_connection_add(hiredis_profile_t *profile, char *host, char *password, uint32_t port, uint32_t timeout_ms, uint32_t max_connections);