Module: sip-router
Branch: master
Commit: 2cbd47c4f9a16ef869efd7830e48a419b3cc1020
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=2cbd47c…
Author: Andreas Granig <agranig(a)sipwise.com>
Committer: Andreas Granig <agranig(a)sipwise.com>
Date: Wed Jul 20 15:54:21 2011 +0200
Add new db option and improve connect mechanism.
Add "db" option to "server" parameter to select the redis db number.
Set 1 sec timeout when connecting to redis server.
Perform "PING" to verify newly created redis connection.
---
modules/ndb_redis/README | 6 ++--
modules/ndb_redis/doc/ndb_redis_admin.xml | 6 ++--
modules/ndb_redis/redis_client.c | 41 ++++++++++++++++++++++-------
3 files changed, 37 insertions(+), 16 deletions(-)
diff --git a/modules/ndb_redis/README b/modules/ndb_redis/README
index 37409c4..8dc695d 100644
--- a/modules/ndb_redis/README
+++ b/modules/ndb_redis/README
@@ -87,15 +87,15 @@ Chapter 1. Admin Guide
Specify the details to connect to REDIS server. It takes a list of
attribute=value separated by semicolon, the attributes can be name,
- addr and port. Name is a genric identifier to be used with module
+ addr, port and db. Name is a generic identifier to be used with module
functions. addr and port are the IP address and the port to connect to
- REDIS server.
+ REDIS server. db is the DB number to use (defaults to 0 if not specified).
Default value is NULL.
Example 1.1. Set server parameter
...
-modparam("ndb_redis", "server",
"name=srvN;addr=127.0.0.1;port=6379")
+modparam("ndb_redis", "server",
"name=srvN;addr=127.0.0.1;port=6379;db=1")
...
4. Exported Functions
diff --git a/modules/ndb_redis/doc/ndb_redis_admin.xml
b/modules/ndb_redis/doc/ndb_redis_admin.xml
index a9609a2..4e2db66 100644
--- a/modules/ndb_redis/doc/ndb_redis_admin.xml
+++ b/modules/ndb_redis/doc/ndb_redis_admin.xml
@@ -64,9 +64,9 @@
<para>
Specify the details to connect to REDIS server. It takes a list of
attribute=value separated by semicolon, the attributes can be
- name, addr and port. Name is a genric identifier to be used with
+ name, addr, port and db. Name is a generic identifier to be used with
module functions. addr and port are the IP address and the port to
- connect to REDIS server.
+ connect to REDIS server. db is the DB number to use (defaults to 0 if not specified).
</para>
<para>
<emphasis>
@@ -77,7 +77,7 @@
<title>Set <varname>server</varname> parameter</title>
<programlisting format="linespecific">
...
-modparam("ndb_redis", "server",
"name=srvN;addr=127.0.0.1;port=6379")
+modparam("ndb_redis", "server",
"name=srvN;addr=127.0.0.1;port=6379;db=1")
...
</programlisting>
</example>
diff --git a/modules/ndb_redis/redis_client.c b/modules/ndb_redis/redis_client.c
index 37c67f7..6a91019 100644
--- a/modules/ndb_redis/redis_client.c
+++ b/modules/ndb_redis/redis_client.c
@@ -34,6 +34,8 @@
#include "redis_client.h"
+#define redisCommandNR(a...) (int)({ void *__tmp; __tmp = redisCommand(a); if (__tmp)
freeReplyObject(__tmp); __tmp ? 0 : -1;})
+
static redisc_server_t *_redisc_srv_list=NULL;
static redisc_reply_t *_redisc_rpl_list=NULL;
@@ -44,9 +46,13 @@ static redisc_reply_t *_redisc_rpl_list=NULL;
int redisc_init(void)
{
char *addr;
- unsigned int port;
+ unsigned int port, db;
redisc_server_t *rsrv=NULL;
param_t *pit = NULL;
+ struct timeval tv;
+
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
if(_redisc_srv_list==NULL)
{
@@ -58,6 +64,7 @@ int redisc_init(void)
{
addr = "127.0.0.1";
port = 6379;
+ db = 0;
for (pit = rsrv->attrs; pit; pit=pit->next)
{
if(pit->name.len==4 && strncmp(pit->name.s, "addr", 4)==0) {
@@ -66,19 +73,33 @@ int redisc_init(void)
} else if(pit->name.len==4 && strncmp(pit->name.s, "port",
4)==0) {
if(!str2int(&pit->body, &port))
port = 6379;
+ } else if(pit->name.len==2 && strncmp(pit->name.s, "db",
2)==0) {
+ if(!str2int(&pit->body, &db))
+ db = 0;
}
}
- rsrv->ctxRedis = redisConnect(addr, port);
- if (rsrv->ctxRedis->err) {
- LM_ERR("failed to connect to redis server [%.*s]: %s\n",
- rsrv->sname->len, rsrv->sname->s, rsrv->ctxRedis->errstr);
- return -1;
- } else {
- LM_DBG("connected to redis server [%.*s] (%s:%d)\n",
- rsrv->sname->len, rsrv->sname->s, addr, port);
- }
+ rsrv->ctxRedis = redisConnectWithTimeout(addr, port, tv);
+ if(!rsrv->ctxRedis)
+ goto err;
+ if (rsrv->ctxRedis->err)
+ goto err2;
+ if (redisCommandNR(rsrv->ctxRedis, "PING"))
+ goto err2;
+ if (redisCommandNR(rsrv->ctxRedis, "SELECT %i", db))
+ goto err2;
+
}
+
return 0;
+
+err2:
+ LM_ERR("error communicating with redis server [%.*s] (%s:%d/%d): %s\n",
+ rsrv->sname->len, rsrv->sname->s, addr, port, db,
rsrv->ctxRedis->errstr);
+ return -1;
+err:
+ LM_ERR("failed to connect to redis server [%.*s] (%s:%d/%d)\n",
+ rsrv->sname->len, rsrv->sname->s, addr, port, db);
+ return -1;
}
/**