[sr-dev] git:master: ndb_redis: redis_free function.

Vicente Hernando vhernando at systemonenoc.com
Tue Jun 19 12:21:16 CEST 2012


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

Author: Vicente Hernando <vhernando at systemonenoc.com>
Committer: Vicente Hernando <vhernando at systemonenoc.com>
Date:   Tue Jun 19 05:56:24 2012 -0400

ndb_redis: redis_free function.

- Free an unused ndb_redis reply, including also its inner rplRedis
structure.

---

 modules/ndb_redis/doc/ndb_redis.xml       |    9 ++++++
 modules/ndb_redis/doc/ndb_redis_admin.xml |   27 +++++++++++++++++
 modules/ndb_redis/ndb_redis_mod.c         |   24 +++++++++++++++-
 modules/ndb_redis/redis_client.c          |   44 +++++++++++++++++++++++++++++
 4 files changed, 103 insertions(+), 1 deletions(-)

diff --git a/modules/ndb_redis/doc/ndb_redis.xml b/modules/ndb_redis/doc/ndb_redis.xml
index f9caa15..cd1d752 100644
--- a/modules/ndb_redis/doc/ndb_redis.xml
+++ b/modules/ndb_redis/doc/ndb_redis.xml
@@ -23,11 +23,20 @@
 		<surname>Mierla</surname>
 		<email>miconda at gmail.com</email>
 	    </editor>
+	    <author>
+		<firstname>Vicente</firstname>
+		<surname>Hernando</surname>
+		<email>vhernando at systemonenoc.com</email>
+	    </author>
 	</authorgroup>
 	<copyright>
 	    <year>2011</year>
 	    <holder>asipto.com</holder>
 	</copyright>
+	<copyright>
+	    <year>2012</year>
+	    <holder>www.systemonenoc.com</holder>
+	</copyright>
     </bookinfo>
     <toc></toc>
     
diff --git a/modules/ndb_redis/doc/ndb_redis_admin.xml b/modules/ndb_redis/doc/ndb_redis_admin.xml
index 1b4aa18..11345c5 100644
--- a/modules/ndb_redis/doc/ndb_redis_admin.xml
+++ b/modules/ndb_redis/doc/ndb_redis_admin.xml
@@ -158,6 +158,33 @@ if(redis_cmd("srvN", "HMGET foo_key field1 field3", "r")) {
 </programlisting>
 	    </example>
 	</section>
+	<section>
+	<title>
+		<function moreinfo="none">redis_free(replyid)</function>
+	</title>
+	<para>
+		Free a previous reply from memory.
+		After this function call, accessing to a freed replyid returns null value.
+	</para>
+	<para>
+		It is not necessary to free a reply to use it again in a new redis_cmd
+		function. When ndb_redis module closes, all pending replies are freed
+		automatically, so you only need to use this function if you perform a
+		lot of redis command requests with different replyid.
+	</para>
+	<example>
+		<title><function>redis_free</function> usage</title>
+		<programlisting format="linespecific">
+...
+After a redis command call:
+	redis_cmd("srvN", "INCR cnt", "r");
+
+when reply not used anymore:
+	redis_free("r");
+...
+		</programlisting>
+	</example>
+	</section>
 	</section>
 </chapter>
 
diff --git a/modules/ndb_redis/ndb_redis_mod.c b/modules/ndb_redis/ndb_redis_mod.c
index d91626c..c1f2b13 100644
--- a/modules/ndb_redis/ndb_redis_mod.c
+++ b/modules/ndb_redis/ndb_redis_mod.c
@@ -52,6 +52,8 @@ static int w_redis_cmd6(struct sip_msg* msg, char* ssrv, char* scmd,
 		char *sargv1, char *sargv2, char *sargv3, char* sres);
 static int fixup_redis_cmd6(void** param, int param_no);
 
+static int w_redis_free_reply(struct sip_msg* msg, char* res);
+
 static int  mod_init(void);
 static void mod_destroy(void);
 static int  child_init(int rank);
@@ -76,6 +78,8 @@ static cmd_export_t cmds[]={
 		0, ANY_ROUTE},
 	{"redis_cmd", (cmd_function)w_redis_cmd6, 6, fixup_redis_cmd6,
 		0, ANY_ROUTE},
+	{"redis_free", (cmd_function)w_redis_free_reply, 1, fixup_spve_null,
+		0, ANY_ROUTE},
 	{0, 0, 0, 0, 0, 0}
 };
 
@@ -291,12 +295,30 @@ static int fixup_redis_cmd6(void** param, int param_no)
 /**
  *
  */
+static int w_redis_free_reply(struct sip_msg* msg, char* res)
+{
+	str name;
+
+	if(fixup_get_svalue(msg, (gparam_t*)res, &name)!=0)
+	{
+		LM_ERR("no redis reply name\n");
+		return -1;
+	}
+
+	if(redisc_free_reply(&name)<0)
+		return -1;
+
+	return 1;
+}
+
+/**
+ *
+ */
 int redis_srv_param(modparam_t type, void *val)
 {
 	return redisc_add_server((char*)val);
 }
 
-
 /**
  *
  */
diff --git a/modules/ndb_redis/redis_client.c b/modules/ndb_redis/redis_client.c
index 9066fda..34d2314 100644
--- a/modules/ndb_redis/redis_client.c
+++ b/modules/ndb_redis/redis_client.c
@@ -364,3 +364,47 @@ redisc_reply_t *redisc_get_reply(str *name)
 	_redisc_rpl_list = rpl;
 	return rpl;
 }
+
+
+/**
+ *
+ */
+int redisc_free_reply(str *name)
+{
+	redisc_reply_t *rpl, *prev_rpl, *next_rpl;
+	unsigned int hid;
+
+	hid = get_hash1_raw(name->s, name->len);
+
+	prev_rpl = NULL;
+	rpl = _redisc_rpl_list;
+	while(rpl) {
+
+		if(rpl->hname==hid && rpl->rname.len==name->len
+		   && strncmp(rpl->rname.s, name->s, name->len)==0) {
+			next_rpl = rpl->next;
+			if(rpl->rplRedis)
+				freeReplyObject(rpl->rplRedis);
+
+			if(rpl->rname.s != NULL)
+				pkg_free(rpl->rname.s);
+
+			pkg_free(rpl);
+
+			if(prev_rpl==NULL) {
+				/* We delete first element in the list. */
+				_redisc_rpl_list = next_rpl;
+			} else {
+				prev_rpl->next = next_rpl;
+			}
+
+			return 0;
+		}
+
+		prev_rpl = rpl;
+		rpl = rpl->next;
+	}
+
+	/* reply entry not found. */
+	return -1;
+}




More information about the sr-dev mailing list