<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][17090] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=17090">17090</a></dd>
<dt>Author</dt> <dd>rupa</dd>
<dt>Date</dt> <dd>2010-03-24 18:26:03 -0500 (Wed, 24 Mar 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>update branch to support <a href="http://jira.freeswitch.org/browse/MODAPP-412">MODAPP-412</a>, some misc cleanups</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchbranchesrupa_limitsrcincludeswitch_limith">freeswitch/branches/rupa_limit/src/include/switch_limit.h</a></li>
<li><a href="#freeswitchbranchesrupa_limitsrcincludeswitch_module_interfacesh">freeswitch/branches/rupa_limit/src/include/switch_module_interfaces.h</a></li>
<li><a href="#freeswitchbranchesrupa_limitsrcmodapplicationsmod_commandsmod_commandsc">freeswitch/branches/rupa_limit/src/mod/applications/mod_commands/mod_commands.c</a></li>
<li><a href="#freeswitchbranchesrupa_limitsrcmodapplicationsmod_dbmod_dbc">freeswitch/branches/rupa_limit/src/mod/applications/mod_db/mod_db.c</a></li>
<li><a href="#freeswitchbranchesrupa_limitsrcmodapplicationsmod_hashmod_hashc">freeswitch/branches/rupa_limit/src/mod/applications/mod_hash/mod_hash.c</a></li>
<li><a href="#freeswitchbranchesrupa_limitsrcswitch_limitc">freeswitch/branches/rupa_limit/src/switch_limit.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchbranchesrupa_limitsrcincludeswitch_limith"></a>
<div class="modfile"><h4>Modified: freeswitch/branches/rupa_limit/src/include/switch_limit.h (17089 => 17090)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/branches/rupa_limit/src/include/switch_limit.h        2010-03-24 23:12:56 UTC (rev 17089)
+++ freeswitch/branches/rupa_limit/src/include/switch_limit.h        2010-03-24 23:26:03 UTC (rev 17090)
</span><span class="lines">@@ -72,8 +72,9 @@
</span><span class="cx">   \param backend to use
</span><span class="cx">   \param realm
</span><span class="cx">   \param resource
</span><ins>+  \param rcount - output paramter, rate counter
</ins><span class="cx"> */
</span><del>-SWITCH_DECLARE(int) switch_limit_usage(const char *backend, const char *realm, const char *resource);
</del><ins>+SWITCH_DECLARE(int) switch_limit_usage(const char *backend, const char *realm, const char *resource, uint32_t *rcount);
</ins><span class="cx"> 
</span><span class="cx"> /*!
</span><span class="cx">   \brief reset all usage counters
</span><span class="lines">@@ -103,7 +104,7 @@
</span><span class="cx"> /*! callback to init a backend */
</span><span class="cx"> #define SWITCH_LIMIT_INCR(name) static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval)
</span><span class="cx"> #define SWITCH_LIMIT_RELEASE(name) static switch_status_t name (switch_core_session_t *session, const char *realm, const char *resource)
</span><del>-#define SWITCH_LIMIT_USAGE(name) static int name (const char *realm, const char *resource)
</del><ins>+#define SWITCH_LIMIT_USAGE(name) static int name (const char *realm, const char *resource, uint32_t *rcount)
</ins><span class="cx"> #define SWITCH_LIMIT_RESET(name) static switch_status_t name (void)
</span><span class="cx"> #define SWITCH_LIMIT_STATUS(name) static char * name (void)
</span><span class="cx"> 
</span></span></pre></div>
<a id="freeswitchbranchesrupa_limitsrcincludeswitch_module_interfacesh"></a>
<div class="modfile"><h4>Modified: freeswitch/branches/rupa_limit/src/include/switch_module_interfaces.h (17089 => 17090)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/branches/rupa_limit/src/include/switch_module_interfaces.h        2010-03-24 23:12:56 UTC (rev 17089)
+++ freeswitch/branches/rupa_limit/src/include/switch_module_interfaces.h        2010-03-24 23:26:03 UTC (rev 17090)
</span><span class="lines">@@ -521,7 +521,7 @@
</span><span class="cx">         /*! release */
</span><span class="cx">         switch_status_t (*release) (switch_core_session_t *session, const char *realm, const char *resource);
</span><span class="cx">         /*! usage for resource */
</span><del>-        int (*usage) (const char *realm, const char *resource);
</del><ins>+        int (*usage) (const char *realm, const char *resource, uint32_t *rcount);
</ins><span class="cx">         /*! reset counters */
</span><span class="cx">         switch_status_t (*reset) (void);
</span><span class="cx">         /*! freform status */
</span></span></pre></div>
<a id="freeswitchbranchesrupa_limitsrcmodapplicationsmod_commandsmod_commandsc"></a>
<div class="modfile"><h4>Modified: freeswitch/branches/rupa_limit/src/mod/applications/mod_commands/mod_commands.c (17089 => 17090)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/branches/rupa_limit/src/mod/applications/mod_commands/mod_commands.c        2010-03-24 23:12:56 UTC (rev 17089)
+++ freeswitch/branches/rupa_limit/src/mod/applications/mod_commands/mod_commands.c        2010-03-24 23:26:03 UTC (rev 17090)
</span><span class="lines">@@ -4067,13 +4067,15 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /* LIMIT Stuff */
</span><del>-#define LIMIT_USAGE_USAGE &quot;&lt;backend&gt; &lt;realm&gt; &lt;id&gt;&quot;
</del><ins>+#define LIMIT_USAGE_USAGE &quot;&lt;backend&gt; &lt;realm&gt; &lt;id&gt; [rate]&quot;
</ins><span class="cx"> SWITCH_STANDARD_API(limit_usage_function)
</span><span class="cx"> {
</span><span class="cx">         int argc = 0;
</span><span class="cx">         char *argv[5] = { 0 };
</span><span class="cx">         char *mydata = NULL;
</span><span class="cx">         uint32_t count = 0;
</span><ins>+        uint32_t rcount = 0;
+        switch_bool_t dorate = SWITCH_FALSE;
</ins><span class="cx"> 
</span><span class="cx">         if (!zstr(cmd)) {
</span><span class="cx">                 mydata = strdup(cmd);
</span><span class="lines">@@ -4085,11 +4087,21 @@
</span><span class="cx">                 stream-&gt;write_function(stream, &quot;USAGE: limit_usage %s\n&quot;, LIMIT_USAGE_USAGE);
</span><span class="cx">                 goto end;
</span><span class="cx">         }
</span><ins>+
+        if (argc &gt; 3) {
+                if (!strcasecmp(&quot;rate&quot;, argv[3])) {
+                        dorate = SWITCH_TRUE;
+                }
+        }
</ins><span class="cx">         
</span><del>-        count = switch_limit_usage(argv[0], argv[1], argv[2]);
</del><ins>+        count = switch_limit_usage(argv[0], argv[1], argv[2], &amp;rcount);
+        
+        if (dorate == SWITCH_TRUE) {
+                stream-&gt;write_function(stream, &quot;%d/%d&quot;, count, rcount);
+        } else {
+                stream-&gt;write_function(stream, &quot;%d&quot;, count);
+        }
</ins><span class="cx"> 
</span><del>-        stream-&gt;write_function(stream, &quot;%d&quot;, count);
-
</del><span class="cx"> end:
</span><span class="cx">         switch_safe_free(mydata);
</span><span class="cx"> 
</span></span></pre></div>
<a id="freeswitchbranchesrupa_limitsrcmodapplicationsmod_dbmod_dbc"></a>
<div class="modfile"><h4>Modified: freeswitch/branches/rupa_limit/src/mod/applications/mod_db/mod_db.c (17089 => 17090)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/branches/rupa_limit/src/mod/applications/mod_db/mod_db.c        2010-03-24 23:12:56 UTC (rev 17089)
+++ freeswitch/branches/rupa_limit/src/mod/applications/mod_db/mod_db.c        2010-03-24 23:26:03 UTC (rev 17090)
</span><span class="lines">@@ -609,6 +609,12 @@
</span><span class="cx">         switch_mutex_init(&amp;globals.db_hash_mutex, SWITCH_MUTEX_NESTED, globals.pool);
</span><span class="cx">         switch_core_hash_init(&amp;globals.db_hash, pool);
</span><span class="cx"> 
</span><ins>+        status = switch_event_reserve_subclass(LIMIT_EVENT_USAGE);
+        if (status != SWITCH_STATUS_SUCCESS &amp;&amp; status != SWITCH_STATUS_INUSE) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Couldn't register event subclass \&quot;%s\&quot; (%d)\n&quot;, LIMIT_EVENT_USAGE, status);
+                return SWITCH_STATUS_FALSE;
+        }
+
</ins><span class="cx">         /* connect my internal structure to the blank pointer passed to me */
</span><span class="cx">         *module_interface = switch_loadable_module_create_module_interface(pool, modname);
</span><span class="cx"> 
</span></span></pre></div>
<a id="freeswitchbranchesrupa_limitsrcmodapplicationsmod_hashmod_hashc"></a>
<div class="modfile"><h4>Modified: freeswitch/branches/rupa_limit/src/mod/applications/mod_hash/mod_hash.c (17089 => 17090)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/branches/rupa_limit/src/mod/applications/mod_hash/mod_hash.c        2010-03-24 23:12:56 UTC (rev 17089)
+++ freeswitch/branches/rupa_limit/src/mod/applications/mod_hash/mod_hash.c        2010-03-24 23:26:03 UTC (rev 17090)
</span><span class="lines">@@ -35,6 +35,8 @@
</span><span class="cx"> 
</span><span class="cx"> #include &lt;switch.h&gt;
</span><span class="cx"> 
</span><ins>+#define LIMIT_HASH_CLEANUP_INTERVAL 900
+
</ins><span class="cx"> SWITCH_MODULE_LOAD_FUNCTION(mod_hash_load);
</span><span class="cx"> SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_hash_shutdown);
</span><span class="cx"> SWITCH_MODULE_DEFINITION(mod_hash, mod_hash_load, mod_hash_shutdown, NULL);
</span><span class="lines">@@ -52,6 +54,7 @@
</span><span class="cx">         uint32_t total_usage;
</span><span class="cx">         uint32_t rate_usage;
</span><span class="cx">         time_t last_check;
</span><ins>+        uint32_t interval;
</ins><span class="cx"> } limit_hash_item_t;
</span><span class="cx"> 
</span><span class="cx"> struct callback {
</span><span class="lines">@@ -114,6 +117,7 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (interval &gt; 0) {
</span><ins>+                item-&gt;interval = interval;
</ins><span class="cx">                 if (item-&gt;last_check &lt;= (now - interval)) {
</span><span class="cx">                         item-&gt;rate_usage = 1;
</span><span class="cx">                         item-&gt;last_check = now;
</span><span class="lines">@@ -170,6 +174,37 @@
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+/* !\brief Determines whether a given entry is ready to be removed. */
+SWITCH_HASH_DELETE_FUNC(limit_hash_cleanup_delete_callback) {
+        limit_hash_item_t *item = (limit_hash_item_t *) val;
+        time_t now = switch_epoch_time_now(NULL);
+
+        /* reset to 0 if window has passed so we can clean it up */
+        if (item-&gt;rate_usage &gt; 0 &amp;&amp; (item-&gt;last_check &lt;= (now - item-&gt;interval))) {
+                item-&gt;rate_usage = 0;
+        }
+
+        if (item-&gt;total_usage == 0 &amp;&amp; item-&gt;rate_usage == 0) {
+                /* Noone is using this item anymore */
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Freeing limit item: %s\n&quot;, (const char *) key);
+                
+                free(item);
+                return SWITCH_TRUE;
+        }
+        
+        return SWITCH_FALSE;
+}
+
+/* !\brief Periodically checks for unused limit entries and frees them */
+SWITCH_STANDARD_SCHED_FUNC(limit_hash_cleanup_callback)
+{
+        switch_thread_rwlock_wrlock(globals.limit_hash_rwlock);
+        switch_core_hash_delete_multi(globals.limit_hash, limit_hash_cleanup_delete_callback, NULL);
+        switch_thread_rwlock_unlock(globals.limit_hash_rwlock);
+        
+        task-&gt;runtime = switch_epoch_time_now(NULL) + LIMIT_HASH_CLEANUP_INTERVAL;
+}
+
</ins><span class="cx"> /* !\brief Releases usage of a limit_hash-controlled ressource  */
</span><span class="cx"> SWITCH_LIMIT_RELEASE(limit_release_hash)
</span><span class="cx"> {
</span><span class="lines">@@ -242,6 +277,7 @@
</span><span class="cx"> 
</span><span class="cx">         if ((item = switch_core_hash_find(globals.limit_hash, hash_key))) {
</span><span class="cx">                 count = item-&gt;total_usage;
</span><ins>+                *rcount = item-&gt;rate_usage;
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">          switch_safe_free(hash_key);
</span><span class="lines">@@ -257,22 +293,20 @@
</span><span class="cx"> 
</span><span class="cx"> SWITCH_LIMIT_STATUS(limit_status_hash)
</span><span class="cx"> {
</span><del>-/*
</del><span class="cx">         switch_hash_index_t *hi = NULL;
</span><span class="cx">         int count = 0;
</span><span class="cx">         char *ret = NULL;
</span><span class="cx">         
</span><span class="cx">         switch_thread_rwlock_rdlock(globals.limit_hash_rwlock);
</span><span class="cx">         
</span><del>-        while ((hi = switch_hash_first(NULL, globals.limit_hash))) {
</del><ins>+        for (hi = switch_hash_first(NULL, globals.limit_hash); hi; switch_hash_next(hi)) {
</ins><span class="cx">                 count++;
</span><span class="cx">         }
</span><ins>+        
</ins><span class="cx">         switch_thread_rwlock_unlock(globals.limit_hash_rwlock);
</span><span class="cx">         
</span><span class="cx">         ret = switch_mprintf(&quot;There are %d elements being tracked.&quot;, count);
</span><span class="cx">         return ret;
</span><del>-*/
-return &quot;-ERR Hash status does not work due to locking issues.&quot;;
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /* APP/API STUFF */
</span><span class="lines">@@ -407,12 +441,14 @@
</span><span class="cx">         switch_application_interface_t *app_interface;
</span><span class="cx">         switch_api_interface_t *commands_api_interface;
</span><span class="cx">         switch_limit_interface_t *limit_interface;
</span><ins>+        switch_status_t status;
</ins><span class="cx"> 
</span><span class="cx">         memset(&amp;globals, 0, sizeof(&amp;globals));
</span><span class="cx">         globals.pool = pool;
</span><span class="cx"> 
</span><del>-        if (switch_event_reserve_subclass(LIMIT_EVENT_USAGE) != SWITCH_STATUS_SUCCESS) {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Couldnt register event subclass \&quot;%s\&quot;&quot;, LIMIT_EVENT_USAGE);
</del><ins>+        status = switch_event_reserve_subclass(LIMIT_EVENT_USAGE);
+        if (status != SWITCH_STATUS_SUCCESS &amp;&amp; status != SWITCH_STATUS_INUSE) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Couldn't register event subclass \&quot;%s\&quot; (%d)\n&quot;, LIMIT_EVENT_USAGE, status);
</ins><span class="cx">                 return SWITCH_STATUS_FALSE;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -427,6 +463,9 @@
</span><span class="cx">         /* register limit interfaces */
</span><span class="cx">         SWITCH_ADD_LIMIT(limit_interface, &quot;hash&quot;, limit_incr_hash, limit_release_hash, limit_usage_hash, limit_reset_hash, limit_status_hash);
</span><span class="cx"> 
</span><ins>+        switch_scheduler_add_task(switch_epoch_time_now(NULL) + LIMIT_HASH_CLEANUP_INTERVAL, limit_hash_cleanup_callback, &quot;limit_hash_cleanup&quot;, &quot;mod_hash&quot;, 0, NULL,
+                                                  SSHF_NONE);
+        
</ins><span class="cx">         SWITCH_ADD_APP(app_interface, &quot;hash&quot;, &quot;Insert into the hashtable&quot;, HASH_DESC, hash_function, HASH_USAGE, SAF_SUPPORT_NOMEDIA)
</span><span class="cx">         SWITCH_ADD_API(commands_api_interface, &quot;hash&quot;, &quot;hash get/set&quot;, hash_api_function, &quot;[insert|delete|select]/&lt;realm&gt;/&lt;key&gt;/&lt;value&gt;&quot;);
</span><span class="cx">         switch_console_set_complete(&quot;add hash insert&quot;);
</span><span class="lines">@@ -441,6 +480,7 @@
</span><span class="cx"> 
</span><span class="cx"> SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_hash_shutdown)
</span><span class="cx"> {
</span><ins>+        switch_scheduler_del_task_group(&quot;mod_hash&quot;);
</ins><span class="cx"> 
</span><span class="cx">         switch_thread_rwlock_destroy(globals.db_hash_rwlock);
</span><span class="cx">         switch_thread_rwlock_destroy(globals.limit_hash_rwlock);
</span></span></pre></div>
<a id="freeswitchbranchesrupa_limitsrcswitch_limitc"></a>
<div class="modfile"><h4>Modified: freeswitch/branches/rupa_limit/src/switch_limit.c (17089 => 17090)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/branches/rupa_limit/src/switch_limit.c        2010-03-24 23:12:56 UTC (rev 17089)
+++ freeswitch/branches/rupa_limit/src/switch_limit.c        2010-03-24 23:26:03 UTC (rev 17090)
</span><span class="lines">@@ -159,7 +159,7 @@
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-SWITCH_DECLARE(int) switch_limit_usage(const char *backend, const char *realm, const char *resource) {
</del><ins>+SWITCH_DECLARE(int) switch_limit_usage(const char *backend, const char *realm, const char *resource, uint32_t *rcount) {
</ins><span class="cx">         switch_limit_interface_t *limit = NULL;
</span><span class="cx">         int usage = 0;
</span><span class="cx">         
</span><span class="lines">@@ -169,7 +169,7 @@
</span><span class="cx">                 goto end;
</span><span class="cx">         }
</span><span class="cx">         
</span><del>-        usage = limit-&gt;usage(realm, resource);
</del><ins>+        usage = limit-&gt;usage(realm, resource, rcount);
</ins><span class="cx">         
</span><span class="cx"> end:
</span><span class="cx">         release_backend(limit);
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>