#define LIMIT_EVENT_USAGE "limit::usage"
#define LIMIT_IGNORE_TRANSFER_VARIABLE "limit_ignore_transfer"
+#define LIMIT_HASH_CLEANUP_INTERVAL 900
SWITCH_MODULE_LOAD_FUNCTION(mod_limit_load);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_limit_shutdown);
uint32_t total_usage;
uint32_t rate_usage;
time_t last_check;
+ uint32_t interval;
} limit_hash_item_t;
typedef struct {
switch_hash_index_t *hi;
switch_mutex_lock(globals.limit_hash_mutex);
- /* Loop through the channel's hashtable which contains mapping to all the limit_hash_item_t referenced by that channel */
+ /* 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->hash is being completely removed
+ */
while ((hi = switch_hash_first(NULL, pvt->hash))) {
void *val = NULL;
const void *key;
item->total_usage--;
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Usage for %s is now %d\n", (const char *) key, item->total_usage);
- if (item->total_usage == 0) {
+ if (item->total_usage == 0 && item->rate_usage == 0) {
/* Noone is using this item anymore */
switch_core_hash_delete(globals.limit_hash, (const char *) key);
free(item);
}
if (interval > 0) {
+ /* interval is always the last used interval setting? */
+ item->interval = interval;
if (item->last_check <= (now - interval)) {
item->rate_usage = 1;
item->last_check = now;
return status;
}
+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_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->runtime = switch_epoch_time_now(NULL) + LIMIT_HASH_CLEANUP_INTERVAL;
+}
+
/* !\brief Releases usage of a limit_hash-controlled ressource */
static void limit_hash_release(switch_core_session_t *session, const char *realm, const char *id)
{
switch_core_hash_delete(pvt->hash, hashkey);
- if (item->total_usage == 0) {
+ if (item->total_usage == 0 && item->rate_usage == 0) {
/* Noone is using this item anymore */
switch_core_hash_delete(globals.limit_hash, (const char *) hashkey);
free(item);
}
-#define LIMIT_HASH_USAGE_USAGE "<realm> <id>"
+#define LIMIT_HASH_USAGE_USAGE "<realm> <id> [rate]"
SWITCH_STANDARD_API(limit_hash_usage_function)
{
int argc = 0;
- char *argv[3] = { 0 };
+ char *argv[4] = { 0 };
char *mydata = NULL;
char *hash_key = NULL;
limit_hash_item_t *item = NULL;
- uint32_t count = 0;
+ uint32_t count = 0, rcount = 0;
+ switch_bool_t dorate = SWITCH_FALSE;
switch_mutex_lock(globals.limit_hash_mutex);
goto end;
}
+ if (argc > 2) {
+ if (!strcasecmp(argv[2], "rate")) {
+ dorate = SWITCH_TRUE;
+ }
+ }
+
hash_key = switch_mprintf("%s_%s", argv[0], argv[1]);
if ((item = switch_core_hash_find(globals.limit_hash, hash_key))) {
count = item->total_usage;
+ rcount = item->rate_usage;
}
- stream->write_function(stream, "%d", count);
+ if (dorate == SWITCH_TRUE) {
+ stream->write_function(stream, "%d/%d", count, rcount);
+ } else {
+ stream->write_function(stream, "%d", count);
+ }
end:
switch_safe_free(mydata);
/* connect my internal structure to the blank pointer passed to me */
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
-
+ switch_scheduler_add_task(switch_epoch_time_now(NULL) + LIMIT_HASH_CLEANUP_INTERVAL, limit_hash_cleanup_callback, "limit_hash_cleanup", "mod_limit", 0, NULL,
+ SSHF_NONE);
SWITCH_ADD_APP(app_interface, "limit", "Limit", LIMIT_DESC, limit_function, LIMIT_USAGE, SAF_SUPPORT_NOMEDIA);
SWITCH_ADD_APP(app_interface, "limit_execute", "Limit", LIMITEXECUTE_USAGE, limit_execute_function, LIMITEXECUTE_USAGE, SAF_SUPPORT_NOMEDIA);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_limit_shutdown)
{
-
+ switch_scheduler_del_task_group("mod_limit");
switch_event_free_subclass(LIMIT_EVENT_USAGE);
switch_xml_config_cleanup(config_settings);
return SWITCH_STATUS_SUCCESS;
}
+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(&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, &key, NULL, &val);
+ if (callback(key, val, pData)) {
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "delete", (const char *) key);
+ }
+ }
+
+ /* now delete them */
+ for (header = event->headers; header; header = header->next) {
+ if (switch_core_hash_delete(hash, header->value) == SWITCH_STATUS_SUCCESS) {
+ status = SWITCH_STATUS_SUCCESS;
+ }
+ }
+
+ switch_event_destroy(&event);
+
+ return status;
+}
+
+
SWITCH_DECLARE(void *) switch_core_hash_find(switch_hash_t *hash, const char *key)
{
return sqlite3HashFind(&hash->table, key, (int) strlen(key) + 1);