<!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][17085] </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=17085">17085</a></dd>
<dt>Author</dt> <dd>rupa</dd>
<dt>Date</dt> <dd>2010-03-24 11:11:24 -0500 (Wed, 24 Mar 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre><a href="http://jira.freeswitch.org/browse/MODAPP-412">MODAPP-412</a> - improve rate limit support
  add switch_core_hash_delete_multi</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcincludeswitch_coreh">freeswitch/trunk/src/include/switch_core.h</a></li>
<li><a href="#freeswitchtrunksrcincludeswitch_typesh">freeswitch/trunk/src/include/switch_types.h</a></li>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_limitmod_limitc">freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c</a></li>
<li><a href="#freeswitchtrunksrcswitch_core_hashc">freeswitch/trunk/src/switch_core_hash.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcincludeswitch_coreh"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/include/switch_core.h (17084 => 17085)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/include/switch_core.h        2010-03-23 20:53:26 UTC (rev 17084)
+++ freeswitch/trunk/src/include/switch_core.h        2010-03-24 16:11:24 UTC (rev 17085)
</span><span class="lines">@@ -1202,6 +1202,14 @@
</span><span class="cx"> SWITCH_DECLARE(switch_status_t) switch_core_hash_delete_locked(_In_ switch_hash_t *hash, _In_z_ const char *key, _In_ switch_mutex_t *mutex);
</span><span class="cx"> 
</span><span class="cx"> /*! 
</span><ins>+  \brief Delete data from a hash based on callback function
+  \param hash the hash to delete from
+  \param callback the function to call which returns SWITCH_TRUE to delete, SWITCH_FALSE to preserve
+  \return SWITCH_STATUS_SUCCESS if any data is deleted
+*/
+SWITCH_DECLARE(switch_status_t) switch_core_hash_delete_multi(_In_ switch_hash_t *hash, _In_ switch_hash_delete_callback_t callback, _In_opt_ void *pData);
+
+/*! 
</ins><span class="cx">   \brief Retrieve data from a given hash
</span><span class="cx">   \param hash the hash to retrieve from
</span><span class="cx">   \param key the key to retrieve
</span></span></pre></div>
<a id="freeswitchtrunksrcincludeswitch_typesh"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/include/switch_types.h (17084 => 17085)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/include/switch_types.h        2010-03-23 20:53:26 UTC (rev 17084)
+++ freeswitch/trunk/src/include/switch_types.h        2010-03-24 16:11:24 UTC (rev 17085)
</span><span class="lines">@@ -1560,6 +1560,8 @@
</span><span class="cx"> typedef switch_caller_extension_t *(*switch_dialplan_hunt_function_t) (switch_core_session_t *, void *, switch_caller_profile_t *);
</span><span class="cx"> #define SWITCH_STANDARD_DIALPLAN(name) static switch_caller_extension_t *name (switch_core_session_t *session, void *arg, switch_caller_profile_t *caller_profile)
</span><span class="cx"> 
</span><ins>+typedef switch_bool_t (*switch_hash_delete_callback_t) (_In_ const void *key, _In_ const void *val, _In_opt_ void *pData);
+#define SWITCH_HASH_DELETE_FUNC(name) static switch_bool_t name (const void *key, const void *val, void *pData)
</ins><span class="cx"> 
</span><span class="cx"> typedef struct switch_scheduler_task switch_scheduler_task_t;
</span><span class="cx"> 
</span></span></pre></div>
<a id="freeswitchtrunksrcmodapplicationsmod_limitmod_limitc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c (17084 => 17085)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c        2010-03-23 20:53:26 UTC (rev 17084)
+++ freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c        2010-03-24 16:11:24 UTC (rev 17085)
</span><span class="lines">@@ -37,6 +37,7 @@
</span><span class="cx"> 
</span><span class="cx"> #define LIMIT_EVENT_USAGE &quot;limit::usage&quot;
</span><span class="cx"> #define LIMIT_IGNORE_TRANSFER_VARIABLE &quot;limit_ignore_transfer&quot;
</span><ins>+#define LIMIT_HASH_CLEANUP_INTERVAL 900
</ins><span class="cx"> 
</span><span class="cx"> SWITCH_MODULE_LOAD_FUNCTION(mod_limit_load);
</span><span class="cx"> SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_limit_shutdown);
</span><span class="lines">@@ -60,6 +61,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"> typedef struct {
</span><span class="lines">@@ -280,7 +282,9 @@
</span><span class="cx">                 switch_hash_index_t *hi;
</span><span class="cx">                 switch_mutex_lock(globals.limit_hash_mutex);
</span><span class="cx"> 
</span><del>-                /* Loop through the channel's hashtable which contains mapping to all the limit_hash_item_t referenced by that channel */
</del><ins>+                /* Loop through the channel's hashtable which contains mapping to all the limit_hash_item_t referenced by that channel
+                   while() idiom used -- 'cause pvt-&gt;hash is being completely removed
+                 */
</ins><span class="cx">                 while ((hi = switch_hash_first(NULL, pvt-&gt;hash))) {
</span><span class="cx">                         void *val = NULL;
</span><span class="cx">                         const void *key;
</span><span class="lines">@@ -293,7 +297,7 @@
</span><span class="cx">                         item-&gt;total_usage--;
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, &quot;Usage for %s is now %d\n&quot;, (const char *) key, item-&gt;total_usage);
</span><span class="cx"> 
</span><del>-                        if (item-&gt;total_usage == 0) {
</del><ins>+                        if (item-&gt;total_usage == 0 &amp;&amp; item-&gt;rate_usage == 0) {
</ins><span class="cx">                                 /* Noone is using this item anymore */
</span><span class="cx">                                 switch_core_hash_delete(globals.limit_hash, (const char *) key);
</span><span class="cx">                                 free(item);
</span><span class="lines">@@ -956,6 +960,8 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (interval &gt; 0) {
</span><ins>+                /* interval is always the last used interval setting? */
+                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">@@ -1012,6 +1018,36 @@
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+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_mutex_lock(globals.limit_hash_mutex);
+        switch_core_hash_delete_multi(globals.limit_hash, limit_hash_cleanup_delete_callback, NULL);
+        switch_mutex_unlock(globals.limit_hash_mutex);
+        
+        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"> static void limit_hash_release(switch_core_session_t *session, const char *realm, const char *id)
</span><span class="cx"> {
</span><span class="lines">@@ -1034,7 +1070,7 @@
</span><span class="cx"> 
</span><span class="cx">                 switch_core_hash_delete(pvt-&gt;hash, hashkey);
</span><span class="cx"> 
</span><del>-                if (item-&gt;total_usage == 0) {
</del><ins>+                if (item-&gt;total_usage == 0 &amp;&amp; item-&gt;rate_usage == 0) {
</ins><span class="cx">                         /* Noone is using this item anymore */
</span><span class="cx">                         switch_core_hash_delete(globals.limit_hash, (const char *) hashkey);
</span><span class="cx">                         free(item);
</span><span class="lines">@@ -1172,15 +1208,16 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-#define LIMIT_HASH_USAGE_USAGE &quot;&lt;realm&gt; &lt;id&gt;&quot;
</del><ins>+#define LIMIT_HASH_USAGE_USAGE &quot;&lt;realm&gt; &lt;id&gt; [rate]&quot;
</ins><span class="cx"> SWITCH_STANDARD_API(limit_hash_usage_function)
</span><span class="cx"> {
</span><span class="cx">         int argc = 0;
</span><del>-        char *argv[3] = { 0 };
</del><ins>+        char *argv[4] = { 0 };
</ins><span class="cx">         char *mydata = NULL;
</span><span class="cx">         char *hash_key = NULL;
</span><span class="cx">         limit_hash_item_t *item = NULL;
</span><del>-        uint32_t count = 0;
</del><ins>+        uint32_t count = 0, rcount = 0;
+        switch_bool_t dorate = SWITCH_FALSE;
</ins><span class="cx"> 
</span><span class="cx">         switch_mutex_lock(globals.limit_hash_mutex);
</span><span class="cx"> 
</span><span class="lines">@@ -1195,13 +1232,24 @@
</span><span class="cx">                 goto end;
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        if (argc &gt; 2) {
+                if (!strcasecmp(argv[2], &quot;rate&quot;)) {
+                        dorate = SWITCH_TRUE;
+                }
+        }
+
</ins><span class="cx">         hash_key = switch_mprintf(&quot;%s_%s&quot;, argv[0], argv[1]);
</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><del>-        stream-&gt;write_function(stream, &quot;%d&quot;, count);
</del><ins>+        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><span class="cx">   end:
</span><span class="cx">         switch_safe_free(mydata);
</span><span class="lines">@@ -1240,8 +1288,9 @@
</span><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><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_limit&quot;, 0, NULL,
+                                                          SSHF_NONE);
</ins><span class="cx"> 
</span><del>-
</del><span class="cx">         SWITCH_ADD_APP(app_interface, &quot;limit&quot;, &quot;Limit&quot;, LIMIT_DESC, limit_function, LIMIT_USAGE, SAF_SUPPORT_NOMEDIA);
</span><span class="cx">         SWITCH_ADD_APP(app_interface, &quot;limit_execute&quot;, &quot;Limit&quot;, LIMITEXECUTE_USAGE, limit_execute_function, LIMITEXECUTE_USAGE, SAF_SUPPORT_NOMEDIA);
</span><span class="cx">         SWITCH_ADD_APP(app_interface, &quot;limit_hash&quot;, &quot;Limit (hash)&quot;, LIMITHASH_DESC, limit_hash_function, LIMITHASH_USAGE, SAF_SUPPORT_NOMEDIA);
</span><span class="lines">@@ -1274,7 +1323,7 @@
</span><span class="cx"> 
</span><span class="cx"> SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_limit_shutdown)
</span><span class="cx"> {
</span><del>-
</del><ins>+        switch_scheduler_del_task_group(&quot;mod_limit&quot;);
</ins><span class="cx">         switch_event_free_subclass(LIMIT_EVENT_USAGE);
</span><span class="cx"> 
</span><span class="cx">         switch_xml_config_cleanup(config_settings);
</span></span></pre></div>
<a id="freeswitchtrunksrcswitch_core_hashc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_core_hash.c (17084 => 17085)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_core_hash.c        2010-03-23 20:53:26 UTC (rev 17084)
+++ freeswitch/trunk/src/switch_core_hash.c        2010-03-24 16:11:24 UTC (rev 17085)
</span><span class="lines">@@ -117,6 +117,42 @@
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+SWITCH_DECLARE(switch_status_t) switch_core_hash_delete_multi(switch_hash_t *hash, switch_hash_delete_callback_t callback, void *pData) {
+
+        switch_hash_index_t *hi = NULL;
+        switch_event_t *event = NULL;
+        switch_event_header_t *header = NULL;
+        switch_status_t status = SWITCH_STATUS_GENERR;
+        
+        switch_event_create_subclass(&amp;event, SWITCH_EVENT_CLONE, NULL);
+        switch_assert(event);
+        
+        /* iterate through the hash, call callback, if callback returns true, put the key on the list (event)
+           When done, iterate through the list deleting hash entries
+         */
+        
+        for (hi = switch_hash_first(NULL, hash); hi; hi = switch_hash_next(hi)) {
+                const void *key;
+                void *val;
+                switch_hash_this(hi, &amp;key, NULL, &amp;val);
+                if (callback(key, val, pData)) {
+                        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, &quot;delete&quot;, (const char *) key);
+                }
+        }
+        
+        /* now delete them */
+        for (header = event-&gt;headers; header; header = header-&gt;next) {
+                if (switch_core_hash_delete(hash, header-&gt;value) == SWITCH_STATUS_SUCCESS) {
+                        status = SWITCH_STATUS_SUCCESS;
+                }
+        }
+
+        switch_event_destroy(&amp;event);
+        
+        return status;
+}
+
+
</ins><span class="cx"> SWITCH_DECLARE(void *) switch_core_hash_find(switch_hash_t *hash, const char *key)
</span><span class="cx"> {
</span><span class="cx">         return sqlite3HashFind(&amp;hash-&gt;table, key, (int) strlen(key) + 1);
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>