提交 bff9aab8 authored 作者: William King's avatar William King

Merge pull request #450 in FS/freeswitch from mod_hiredis to master

* commit 'd582e08d':
  Startiing the deprecation mod_redis in favor of mod_hiredis.
  FS-8075
......@@ -23,6 +23,7 @@ applications/mod_fifo
#applications/mod_fsk
applications/mod_fsv
applications/mod_hash
#applications/mod_hiredis
applications/mod_httapi
#applications/mod_http_cache
#applications/mod_ladspa
......
<configuration name="hiredis.conf" description="mod_hiredis">
<profiles>
<profile name="default">
<connections>
<connection name="primary">
<param name="hostname" value="172.18.101.101"/>
<param name="password" value="redis"/>
<param name="port" value="6379"/>
<param name="timeout_ms" value="500"/>
</connection>
<connection name="secondary">
<param name="hostname" value="localhost"/>
<param name="password" value="redis"/>
<param name="port" value="6380"/>
<param name="timeout_ms" value="500"/>
</connection>
</connections>
<params>
<param name="debug" value="1"/>
</params>
</profile>
</profiles>
</configuration>
......@@ -1390,6 +1390,10 @@ PKG_CHECK_MODULES([SMPP34], [libsmpp34 >= 1.10],[
AM_CONDITIONAL([HAVE_SMPP34],[true])],[
AC_MSG_RESULT([no]); AM_CONDITIONAL([HAVE_SMPP34],[false])])
PKG_CHECK_MODULES([HIREDIS], [hiredis >= 0.11.0],[
AM_CONDITIONAL([HAVE_HIREDIS],[true])],[
AC_MSG_RESULT([no]); AM_CONDITIONAL([HAVE_HIREDIS],[false])])
AC_ARG_ENABLE(core-libedit-support,
[AS_HELP_STRING([--disable-core-libedit-support], [Compile without libedit Support])])
......@@ -1688,6 +1692,7 @@ AC_CONFIG_FILES([Makefile
src/mod/applications/mod_fsk/Makefile
src/mod/applications/mod_fsv/Makefile
src/mod/applications/mod_hash/Makefile
src/mod/applications/mod_hiredis/Makefile
src/mod/applications/mod_httapi/Makefile
src/mod/applications/mod_http_cache/Makefile
src/mod/applications/mod_ladspa/Makefile
......
include $(top_srcdir)/build/modmake.rulesam
MODNAME=mod_hiredis
if HAVE_HIREDIS
mod_LTLIBRARIES = mod_hiredis.la
mod_hiredis_la_SOURCES = mod_hiredis.c hiredis_utils.c hiredis_profile.c
mod_hiredis_la_CFLAGS = $(AM_CFLAGS) $(HIREDIS_CFLAGS)
mod_hiredis_la_LIBADD = $(switch_builddir)/libfreeswitch.la
mod_hiredis_la_LDFLAGS = -avoid-version -module -no-undefined -shared $(HIREDIS_LIBS) $(SWITCH_AM_LDFLAGS)
else
install: error
all: error
error:
$(error You must install libhiredis-dev to build this module)
endif
1. jsapi interface(mod_verto)
2. add lock for hiredis_profile for destroy vs running commands
3. Look into refactor/cleanup of xml processing
4. Add tab complete for profile names for APIs, and possibly for supported actions(and in theory look into key listing from redis)
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2015, Anthony Minessale II <anthm@freeswitch.org>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthm@freeswitch.org>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* William King <william.king@quentustech.com>
*
* mod_hiredis.c -- redis client built using the C client library hiredis
*
*/
#include <mod_hiredis.h>
switch_status_t hiredis_profile_create(hiredis_profile_t **new_profile, char *name, uint8_t port)
{
hiredis_profile_t *profile = NULL;
switch_memory_pool_t *pool = NULL;
switch_core_new_memory_pool(&pool);
profile = switch_core_alloc(pool, sizeof(hiredis_profile_t));
profile->pool = pool;
profile->name = name ? switch_core_strdup(profile->pool, name) : "default";
profile->conn = NULL;
profile->conn_head = NULL;
switch_core_hash_insert(mod_hiredis_globals.profiles, name, (void *) profile);
*new_profile = profile;
return SWITCH_STATUS_SUCCESS;
}
switch_status_t hiredis_profile_destroy(hiredis_profile_t **old_profile)
{
hiredis_profile_t *profile = NULL;
if ( !old_profile || !*old_profile ) {
return SWITCH_STATUS_SUCCESS;
} else {
profile = *old_profile;
*old_profile = NULL;
}
switch_core_hash_delete(mod_hiredis_globals.profiles, profile->name);
switch_core_destroy_memory_pool(&(profile->pool));
return SWITCH_STATUS_SUCCESS;
}
switch_status_t hiredis_profile_connection_add(hiredis_profile_t *profile, char *host, char *password, uint32_t port, uint32_t timeout_ms)
{
hiredis_connection_t *connection = NULL, *new_conn = NULL;
new_conn = switch_core_alloc(profile->pool, sizeof(hiredis_connection_t));
new_conn->host = host ? switch_core_strdup(profile->pool, host) : "localhost";
new_conn->password = password ? switch_core_strdup(profile->pool, password) : NULL;
new_conn->port = port ? port : 6379;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "hiredis: adding conn[%d]\n", new_conn->port);
if ( timeout_ms ) {
new_conn->timeout.tv_sec = 0;
new_conn->timeout.tv_usec = timeout_ms * 1000;
} else {
new_conn->timeout.tv_sec = 0;
new_conn->timeout.tv_usec = 500 * 1000;
}
if ( profile->conn_head != NULL ){
/* Adding 'another' connection */
connection = profile->conn_head;
while ( connection->next != NULL ){
connection = connection->next;
}
connection->next = new_conn;
} else {
profile->conn_head = new_conn;
}
return SWITCH_STATUS_SUCCESS;
}
switch_status_t hiredis_profile_reconnect(hiredis_profile_t *profile)
{
hiredis_connection_t *conn = profile->conn_head;
profile->conn = NULL;
/* TODO: Needs thorough expansion to handle all disconnection scenarios */
while ( conn ) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "hiredis: attempting[%s, %d]\n", conn->host, conn->port);
conn->context = redisConnectWithTimeout(conn->host, conn->port, conn->timeout);
if ( conn->context && conn->context->err) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: connection error[%s]\n", conn->context->errstr);
conn = conn->next;
continue;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "hiredis: connection success[%s]\n", conn->host);
/* successful redis connection */
profile->conn = conn;
return SWITCH_STATUS_SUCCESS;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: unable to reconnect\n");
return SWITCH_STATUS_GENERR;
}
switch_status_t hiredis_profile_execute_sync(hiredis_profile_t *profile, const char *data, char **resp)
{
char *str = NULL;
redisReply *response = NULL;
/* Check connection */
if ( !profile->conn && hiredis_profile_reconnect(profile) != SWITCH_STATUS_SUCCESS ) {
*resp = strdup("hiredis profile unable to establish connection");
return SWITCH_STATUS_GENERR;
}
response = redisCommand(profile->conn->context, data);
switch(response->type) {
case REDIS_REPLY_STATUS: /* fallthrough */
case REDIS_REPLY_STRING:
str = strdup(response->str);
break;
case REDIS_REPLY_INTEGER:
str = switch_mprintf("%lld", response->integer);
break;
default:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "hiredis: response error[%s][%d]\n", response->str, response->type);
freeReplyObject(response);
return SWITCH_STATUS_GENERR;
}
freeReplyObject(response);
*resp = str;
return SWITCH_STATUS_SUCCESS;
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2015, Anthony Minessale II <anthm@freeswitch.org>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthm@freeswitch.org>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* William King <william.king@quentustech.com>
*
* mod_hiredis.c -- redis client built using the C client library hiredis
*
*/
#include <mod_hiredis.h>
switch_status_t mod_hiredis_do_config()
{
char *conf = "hiredis.conf";
switch_xml_t xml, cfg, profiles, profile, connections, connection, params, param;
if (!(xml = switch_xml_open_cfg(conf, &cfg, NULL))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", conf);
goto err;
}
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;
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 ( hiredis_profile_create(&new_profile, name, debug) == 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);
}
/* Add connection to profile */
if ( (connections = switch_xml_child(profile, "connections")) != NULL) {
for (connection = switch_xml_child(connections, "connection"); connection; connection = connection->next) {
char *host = NULL, *password = NULL;
uint32_t port = 0, timeout_ms = 0;
for (param = switch_xml_child(connection, "param"); param; param = param->next) {
char *var = (char *) switch_xml_attr_soft(param, "name");
if ( !strncmp(var, "hostname", 8) ) {
host = (char *) switch_xml_attr_soft(param, "value");
} else if ( !strncmp(var, "port", 4) ) {
port = atoi(switch_xml_attr_soft(param, "value"));
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "hiredis: adding conn[%u == %s]\n", port, switch_xml_attr_soft(param, "value"));
} else if ( !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");
}
}
if ( hiredis_profile_connection_add(new_profile, host, password, port, timeout_ms) == 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);
}
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile->connections config is missing\n");
goto err;
}
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profiles config is missing\n");
goto err;
}
return SWITCH_STATUS_SUCCESS;
err:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Configuration failed\n");
return SWITCH_STATUS_GENERR;
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/
差异被折叠。
#ifndef MOD_HIREDIS_H
#define MOD_HIREDIS_H
#include <switch.h>
#include <hiredis/hiredis.h>
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;
typedef struct hiredis_connection_s {
char *host;
char *password;
uint32_t port;
redisContext *context;
struct timeval timeout;
struct hiredis_connection_s *next;
} hiredis_connection_t;
typedef struct hiredis_profile_s {
switch_memory_pool_t *pool;
char *name;
int debug;
hiredis_connection_t *conn;
hiredis_connection_t *conn_head;
} hiredis_profile_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_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);
switch_status_t hiredis_profile_execute_sync(hiredis_profile_t *profile, const char *data, char **response);
#endif /* MOD_HIREDIS_H */
......@@ -85,7 +85,9 @@ SWITCH_LIMIT_INCR(limit_incr_redis)
uint8_t increment = 1;
switch_status_t status = SWITCH_STATUS_SUCCESS;
REDIS redis;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "mod_redis is deprecated and will be removed in FS 1.8. Check out mod_hiredis.\n");
if (redis_factory(&redis) != SWITCH_STATUS_SUCCESS) {
if ( globals.ignore_connect_fail ) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ignore_connect_fail=true, so ignoring the fact that redis was not contactabl and continuing with the call\n" );
......@@ -315,6 +317,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_redis_load)
return SWITCH_STATUS_FALSE;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "mod_redis is deprecated and will be removed in FS 1.8. Check out mod_hiredis.\n");
/* If FreeSWITCH was restarted and we still have active calls, decrement them so our global count stays valid */
limit_reset_redis();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论