Browse Source

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.
Andreas Granig 14 years ago
parent
commit
2cbd47c4f9

+ 3 - 3
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
    Specify the details to connect to REDIS server. It takes a list of
    attribute=value separated by semicolon, the attributes can be name,
    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
    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.
    Default value is NULL.
 
 
    Example 1.1. Set server parameter
    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
 4. Exported Functions

+ 3 - 3
modules/ndb_redis/doc/ndb_redis_admin.xml

@@ -64,9 +64,9 @@
 		<para>
 		<para>
 			Specify the details to connect to REDIS server. It takes a list of
 			Specify the details to connect to REDIS server. It takes a list of
 			attribute=value separated by semicolon, the attributes can be
 			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
 			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>
 		<para>
 		<para>
 		<emphasis>
 		<emphasis>
@@ -77,7 +77,7 @@
 			<title>Set <varname>server</varname> parameter</title>
 			<title>Set <varname>server</varname> parameter</title>
 		<programlisting format="linespecific">
 		<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>
 </programlisting>
 		</example>
 		</example>

+ 31 - 10
modules/ndb_redis/redis_client.c

@@ -34,6 +34,8 @@
 
 
 #include "redis_client.h"
 #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_server_t *_redisc_srv_list=NULL;
 
 
 static redisc_reply_t *_redisc_rpl_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)
 int redisc_init(void)
 {
 {
 	char *addr;
 	char *addr;
-	unsigned int port;
+	unsigned int port, db;
 	redisc_server_t *rsrv=NULL;
 	redisc_server_t *rsrv=NULL;
 	param_t *pit = NULL;
 	param_t *pit = NULL;
+	struct timeval tv;
+
+	tv.tv_sec = 1;
+	tv.tv_usec = 0;
 
 
 	if(_redisc_srv_list==NULL)
 	if(_redisc_srv_list==NULL)
 	{
 	{
@@ -58,6 +64,7 @@ int redisc_init(void)
 	{
 	{
 		addr = "127.0.0.1";
 		addr = "127.0.0.1";
 		port = 6379;
 		port = 6379;
+		db = 0;
 		for (pit = rsrv->attrs; pit; pit=pit->next)
 		for (pit = rsrv->attrs; pit; pit=pit->next)
 		{
 		{
 			if(pit->name.len==4 && strncmp(pit->name.s, "addr", 4)==0) {
 			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) {
 			} else if(pit->name.len==4 && strncmp(pit->name.s, "port", 4)==0) {
 				if(!str2int(&pit->body, &port))
 				if(!str2int(&pit->body, &port))
 					port = 6379;
 					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;
 	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;
 }
 }
 
 
 /**
 /**