[sr-dev] git:master: ndb_redis: able to connect to redis server via unix domain socket.

Vicente Hernando vhernando at systemonenoc.com
Wed Jun 20 21:10:43 CEST 2012


Module: sip-router
Branch: master
Commit: b20f81a4efc43faed2656170f8b4108f897dffee
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=b20f81a4efc43faed2656170f8b4108f897dffee

Author: Vicente Hernando <vhernando at systemonenoc.com>
Committer: Vicente Hernando <vhernando at systemonenoc.com>
Date:   Wed Jun 20 14:59:47 2012 -0400

ndb_redis: able to connect to redis server via unix domain socket.

- added unix atribute in server modparam
- unix attribute has higher precedence over address and port

---

 modules/ndb_redis/doc/ndb_redis_admin.xml |   13 ++++--
 modules/ndb_redis/redis_client.c          |   66 ++++++++++++++++++++++-------
 2 files changed, 60 insertions(+), 19 deletions(-)

diff --git a/modules/ndb_redis/doc/ndb_redis_admin.xml b/modules/ndb_redis/doc/ndb_redis_admin.xml
index 11345c5..54d6788 100644
--- a/modules/ndb_redis/doc/ndb_redis_admin.xml
+++ b/modules/ndb_redis/doc/ndb_redis_admin.xml
@@ -64,10 +64,12 @@
 		<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, 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. db is the DB number to use (defaults to 0 if
-			not specified).
+			name, unix, addr, port and db. Name is a generic identifier to be used
+			with module functions. unix is the path to the unix domain socket provided
+			by redis server. addr and port are the IP address and the port to
+			connect to REDIS server. unix and (addr, port) are mutually exclusive.
+			If both appear in same server settings unix domain socket is configured.
+			db is the DB number to use (defaults to 0 if not specified).
 		</para>
 		<para>
 			You can set this parameter many times, in case you want to connect to
@@ -85,6 +87,9 @@
 ...
 modparam("ndb_redis", "server", "name=srvN;addr=127.0.0.1;port=6379;db=1")
 modparam("ndb_redis", "server", "name=srvX;addr=127.0.0.2;port=6379;db=4")
+
+# Unix domain socket
+modparam("ndb_redis", "server", "name=srvY;unix=/tmp/redis.sock;db=3")
 ...
 </programlisting>
 		</example>
diff --git a/modules/ndb_redis/redis_client.c b/modules/ndb_redis/redis_client.c
index 34d2314..ae97583 100644
--- a/modules/ndb_redis/redis_client.c
+++ b/modules/ndb_redis/redis_client.c
@@ -45,7 +45,7 @@ static redisc_reply_t *_redisc_rpl_list=NULL;
  */
 int redisc_init(void)
 {
-	char *addr;
+	char *addr, *unix_sock_path = NULL;
 	unsigned int port, db;
 	redisc_server_t *rsrv=NULL;
 	param_t *pit = NULL;
@@ -67,7 +67,10 @@ int redisc_init(void)
 		db = 0;
 		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, "unix", 4)==0) {
+				unix_sock_path = pit->body.s;
+				unix_sock_path[pit->body.len] = '\0';
+			} else if(pit->name.len==4 && strncmp(pit->name.s, "addr", 4)==0) {
 				addr = pit->body.s;
 				addr[pit->body.len] = '\0';
 			} else if(pit->name.len==4 && strncmp(pit->name.s, "port", 4)==0) {
@@ -78,7 +81,14 @@ int redisc_init(void)
 					db = 0;
 			}
 		}
-		rsrv->ctxRedis = redisConnectWithTimeout(addr, port, tv);
+
+		if(unix_sock_path != NULL) {
+			LM_DBG("Connecting to unix socket: %s\n", unix_sock_path);
+			rsrv->ctxRedis = redisConnectUnixWithTimeout(unix_sock_path, tv);
+		} else {
+			rsrv->ctxRedis = redisConnectWithTimeout(addr, port, tv);
+		}
+
 		if(!rsrv->ctxRedis)
 			goto err;
 		if (rsrv->ctxRedis->err)
@@ -93,12 +103,22 @@ int redisc_init(void)
 	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);
+	if (unix_sock_path != NULL) {
+		LM_ERR("error communicating with redis server [%.*s] (unix:%s db:%d): %s\n",
+			   rsrv->sname->len, rsrv->sname->s, unix_sock_path, db, rsrv->ctxRedis->errstr);
+	} else {
+		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);
+	if (unix_sock_path != NULL) {
+		LM_ERR("failed to connect to redis server [%.*s] (unix:%s db:%d)\n",
+			   rsrv->sname->len, rsrv->sname->s, unix_sock_path, db);
+	} else {
+		LM_ERR("failed to connect to redis server [%.*s] (%s:%d/%d)\n",
+			   rsrv->sname->len, rsrv->sname->s, addr, port, db);
+	}
 	return -1;
 }
 
@@ -221,7 +241,7 @@ redisc_server_t *redisc_get_server(str *name)
  */
 int redisc_reconnect_server(redisc_server_t *rsrv)
 {
-	char *addr;
+	char *addr, *unix_sock_path = NULL;
 	unsigned int port, db;
 	param_t *pit = NULL;
 	struct timeval tv;
@@ -233,7 +253,10 @@ int redisc_reconnect_server(redisc_server_t *rsrv)
 	db = 0;
 	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, "unix", 4)==0) {
+			unix_sock_path = pit->body.s;
+			unix_sock_path[pit->body.len] = '\0';
+		} else if(pit->name.len==4 && strncmp(pit->name.s, "addr", 4)==0) {
 			addr = pit->body.s;
 			addr[pit->body.len] = '\0';
 		} else if(pit->name.len==4 && strncmp(pit->name.s, "port", 4)==0) {
@@ -249,7 +272,11 @@ int redisc_reconnect_server(redisc_server_t *rsrv)
 		rsrv->ctxRedis = NULL;
 	}
 
-	rsrv->ctxRedis = redisConnectWithTimeout(addr, port, tv);
+	if(unix_sock_path != NULL) {
+		rsrv->ctxRedis = redisConnectUnixWithTimeout(unix_sock_path, tv);
+	} else {
+		rsrv->ctxRedis = redisConnectWithTimeout(addr, port, tv);
+	}
 	if(!rsrv->ctxRedis)
 		goto err;
 	if (rsrv->ctxRedis->err)
@@ -262,12 +289,21 @@ int redisc_reconnect_server(redisc_server_t *rsrv)
 	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;
+	if (unix_sock_path != NULL) {
+		LM_ERR("error communicating with redis server [%.*s] (unix:%s db:%d): %s\n",
+			   rsrv->sname->len, rsrv->sname->s, unix_sock_path, db, rsrv->ctxRedis->errstr);
+	} else {
+		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);
+	}
 err:
-	LM_ERR("failed to connect to redis server [%.*s] (%s:%d/%d)\n",
-		rsrv->sname->len, rsrv->sname->s, addr, port, db);
+	if (unix_sock_path != NULL) {
+		LM_ERR("failed to connect to redis server [%.*s] (unix:%s db:%d)\n",
+			   rsrv->sname->len, rsrv->sname->s, unix_sock_path, db);
+	} else {
+		LM_ERR("failed to connect to redis server [%.*s] (%s:%d/%d)\n",
+			   rsrv->sname->len, rsrv->sname->s, addr, port, db);
+	}
 	return -1;
 }
 




More information about the sr-dev mailing list