add new configuration 'ignore-error' which will not fail limit if an error like auth failure occurs
#include <mod_hiredis.h>
+/* auth if password is set */
+static switch_status_t hiredis_context_auth(hiredis_context_t *context)
+{
+ switch_status_t status = SWITCH_STATUS_SUCCESS;
+ if ( !zstr(context->connection->password) ) {
+ redisReply *response = redisCommand(context->context, "AUTH %s", context->connection->password);
+ if ( !response || response->type == REDIS_REPLY_ERROR ) {
+ status = SWITCH_STATUS_FALSE;
+ }
+ if ( response ) {
+ freeReplyObject(response);
+ }
+ }
+ return status;
+}
+
/* reconnect to redis server */
static switch_status_t hiredis_context_reconnect(hiredis_context_t *context)
{
redisFree(context->context);
context->context = redisConnectWithTimeout(context->connection->host, context->connection->port, context->connection->timeout);
- if ( context->context && !context->context->err ) {
+ if ( context->context && !context->context->err && hiredis_context_auth(context) == SWITCH_STATUS_SUCCESS ) {
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_FALSE;
if ( !context->context ) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "hiredis: attempting[%s, %d]\n", conn->host, conn->port);
context->context = redisConnectWithTimeout(conn->host, conn->port, conn->timeout);
- if ( context->context && !context->context->err ) {
+ if ( context->context && !context->context->err && hiredis_context_auth(context) == SWITCH_STATUS_SUCCESS ) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "hiredis: connection success[%s, %d]\n", conn->host, conn->port);
return context;
} else {
return NULL;
}
-switch_status_t hiredis_profile_create(hiredis_profile_t **new_profile, char *name, uint8_t ignore_connect_fail)
+switch_status_t hiredis_profile_create(hiredis_profile_t **new_profile, char *name, uint8_t ignore_connect_fail, uint8_t ignore_error)
{
hiredis_profile_t *profile = NULL;
switch_memory_pool_t *pool = NULL;
profile->name = name ? switch_core_strdup(profile->pool, name) : "default";
profile->conn_head = NULL;
profile->ignore_connect_fail = ignore_connect_fail;
+ profile->ignore_error = ignore_error;
switch_core_hash_insert(mod_hiredis_globals.profiles, name, (void *) profile);
case REDIS_REPLY_INTEGER:
*resp = switch_mprintf("%lld", response->integer);
break;
- default:
- switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "hiredis: response error[%s][%d]\n", response->str, response->type);
+ case REDIS_REPLY_ERROR:
+ switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "hiredis: error response[%s][%d]\n", response->str, response->type);
freeReplyObject(response);
*resp = NULL;
return SWITCH_STATUS_GENERR;
+ default:
+ switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "hiredis: unsupported response[%s][%d]\n", response->str, response->type);
+ freeReplyObject(response);
+ *resp = NULL;
+ return SWITCH_STATUS_IGNORE;
}
freeReplyObject(response);
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
-* Copyright (C) 2005-2015, Anthony Minessale II <anthm@freeswitch.org>
+* Copyright (C) 2005-2016, Anthony Minessale II <anthm@freeswitch.org>
*
* Version: MPL 1.1
*
for (profile = switch_xml_child(profiles, "profile"); profile; profile = profile->next) {
hiredis_profile_t *new_profile = NULL;
uint8_t ignore_connect_fail = 0;
+ uint8_t ignore_error = 0;
char *name = (char *) switch_xml_attr_soft(profile, "name");
// Load params
char *var = (char *) switch_xml_attr_soft(param, "name");
if ( !strncmp(var, "ignore-connect-fail", 19) ) {
ignore_connect_fail = switch_true(switch_xml_attr_soft(param, "value"));
+ } else if ( !strncmp(var, "ignore-error", 12) ) {
+ ignore_error = switch_true(switch_xml_attr_soft(param, "value"));
}
}
}
- if ( hiredis_profile_create(&new_profile, name, ignore_connect_fail) == SWITCH_STATUS_SUCCESS ) {
+ if ( hiredis_profile_create(&new_profile, name, ignore_connect_fail, ignore_error) == 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);
hashkey = switch_mprintf("incr %s", limit_key);
if ( (status = hiredis_profile_execute_sync(profile, hashkey, &response, session)) != SWITCH_STATUS_SUCCESS ) {
- if ( status == SWITCH_STATUS_SOCKERR && profile->ignore_connect_fail ) {
+ 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 executing [%s]\n", realm, hashkey);
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 executing [%s]\n", realm, hashkey);
+ switch_goto_status(SWITCH_STATUS_SUCCESS, done);
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), 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 : "");
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 executing [%s]\n", realm, hashkey);
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 executing [%s]\n", realm, hashkey);
+ switch_goto_status(SWITCH_STATUS_SUCCESS, done);
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), 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_memory_pool_t *pool;
char *name;
uint8_t ignore_connect_fail;
+ uint8_t ignore_error;
hiredis_connection_t *conn_head;
} hiredis_profile_t;
} hiredis_limit_pvt_t;
switch_status_t mod_hiredis_do_config(void);
-switch_status_t hiredis_profile_create(hiredis_profile_t **new_profile, char *name, uint8_t ignore_connect_fail);
+switch_status_t hiredis_profile_create(hiredis_profile_t **new_profile, char *name, uint8_t ignore_connect_fail, uint8_t ignore_error);
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);