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