Revision
17079
Author
rupa
Date
2010-03-23 11:17:15 -0500 (Tue, 23 Mar 2010)

Log Message

in progress limit changes

Modified Paths

Added Paths

Diff

Added: freeswitch/branches/rupa_limit/src/include/switch_limit.h (0 => 17079)


--- freeswitch/branches/rupa_limit/src/include/switch_limit.h	                        (rev 0)
+++ freeswitch/branches/rupa_limit/src/include/switch_limit.h	2010-03-23 16:17:15 UTC (rev 17079)
@@ -0,0 +1,126 @@
+/* 
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005-2010, Anthony Minessale II <anthm@freeswitch.org>
+ *
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ *
+ * The Initial Developer of the Original Code is
+ * Anthony Minessale II <anthm@freeswitch.org>
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * 
+ * Rupa Schomaker <rupa@rupa.com>
+ *
+ * switch_limit.h - Limit generic implementations
+ *
+ */
+
+ 
+ /*!
+  \defgroup limit1 LIMIT code
+  \ingroup core1
+  \{
+*/
+#ifndef _SWITCH_LIMIT_H
+#define _SWITCH_LIMIT_H
+
+SWITCH_BEGIN_EXTERN_C
+
+/*! 
+  \brief Initilize the LIMIT Core System
+  \param pool the memory pool to use for long term allocations
+  \note Generally called by the core_init
+*/
+SWITCH_DECLARE(void) switch_limit_init(switch_memory_pool_t *pool);
+
+/*!
+  \brief Increment resource.  
+  \param backend to use
+  \param realm
+  \param resource
+  \param max - 0 means no limit, just count
+  \param interval - 0 means no interval
+  \return true/false - true ok, false over limit
+*/
+SWITCH_DECLARE(switch_status_t) switch_limit_incr(const char *backend, switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval);
+
+/*!
+  \brief Release resource.  
+  \param backend to use
+  \param realm
+  \param resource
+  \return true/false - true ok, false over limit
+*/
+SWITCH_DECLARE(switch_status_t) switch_limit_release(const char *backend, switch_core_session_t *session, const char *realm, const char *resource);
+
+/*!
+  \brief get usage count for resource
+  \param backend to use
+  \param realm
+  \param resource
+*/
+SWITCH_DECLARE(int) switch_limit_usage(const char *backend, const char *realm, const char *resource);
+
+/*!
+  \brief reset all usage counters
+  \param backend to use
+*/
+SWITCH_DECLARE(switch_status_t) switch_limit_reset(const char *backend);
+
+/*!
+  \brief fire event for limit usage
+  \param backend to use
+  \param realm
+  \param resource
+  \param usage
+  \param rate
+  \param max
+  \param ratemax
+*/
+SWITCH_DECLARE(void) switch_limit_fire_event(const char *backend, const char *realm, const char *resource, uint32_t usage, uint32_t rate, uint32_t max, uint32_t ratemax);
+
+/*!
+  \brief retrieve arbitrary status information
+  \param backend to use
+  \note caller must free returned value
+*/
+SWITCH_DECLARE(char *) switch_limit_status(const char *backend);
+
+/*! 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_RESET(name) static switch_status_t name (void)
+#define SWITCH_LIMIT_STATUS(name) static char * name (void)
+
+#define LIMIT_IGNORE_TRANSFER_VARIABLE "limit_ignore_transfer"
+#define LIMIT_BACKEND_VARIABLE "limit_backend"
+#define LIMIT_EVENT_USAGE "limit::usage"
+#define LIMIT_DEF_XFER_EXTEN "limit_exceeded"
+
+SWITCH_END_EXTERN_C
+#endif
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
+ */

Modified: freeswitch/branches/rupa_limit/src/mod/applications/mod_hash/mod_hash.c (17078 => 17079)


--- freeswitch/branches/rupa_limit/src/mod/applications/mod_hash/mod_hash.c	2010-03-23 16:16:48 UTC (rev 17078)
+++ freeswitch/branches/rupa_limit/src/mod/applications/mod_hash/mod_hash.c	2010-03-23 16:17:15 UTC (rev 17079)
@@ -42,9 +42,9 @@
 /* CORE STUFF */
 static struct {
 	switch_memory_pool_t *pool;
-	switch_mutex_t *limit_hash_mutex;
+	switch_thread_rwlock_t *limit_hash_rwlock;
 	switch_hash_t *limit_hash;
-	switch_mutex_t *db_hash_mutex;
+	switch_thread_rwlock_t *db_hash_rwlock;
 	switch_hash_t *db_hash;
 } globals;
 
@@ -87,7 +87,7 @@
 
 	hashkey = switch_core_session_sprintf(session, "%s_%s", realm, resource);
 
-	switch_mutex_lock(globals.limit_hash_mutex);
+	switch_thread_rwlock_wrlock(globals.limit_hash_rwlock);
 	/* Check if that realm+resource has ever been checked */
 	if (!(item = (limit_hash_item_t *) switch_core_hash_find(globals.limit_hash, hashkey))) {
 		/* No, create an empty structure and add it, then continue like as if it existed */
@@ -163,7 +163,7 @@
 	}
 
   end:
-	switch_mutex_unlock(globals.limit_hash_mutex);
+	switch_thread_rwlock_unlock(globals.limit_hash_rwlock);
 	return status;
 }
 
@@ -180,7 +180,7 @@
 		return SWITCH_STATUS_SUCCESS;
 	}
 
-	switch_mutex_lock(globals.limit_hash_mutex);
+	switch_thread_rwlock_wrlock(globals.limit_hash_rwlock);
 
 	/* clear for uuid */
 	if (realm == NULL && resource == NULL) {
@@ -222,7 +222,7 @@
 		}
 	}
 
-	switch_mutex_unlock(globals.limit_hash_mutex);
+	switch_thread_rwlock_unlock(globals.limit_hash_rwlock);
 	
 	return SWITCH_STATUS_SUCCESS;
 }
@@ -233,7 +233,7 @@
 	limit_hash_item_t *item = NULL;
 	int count = 0;
 
-	switch_mutex_lock(globals.limit_hash_mutex);
+	switch_thread_rwlock_rdlock(globals.limit_hash_rwlock);
 
 	hash_key = switch_mprintf("%s_%s", realm, resource);
 
@@ -242,7 +242,7 @@
 	}
 
  	switch_safe_free(hash_key);
-	switch_mutex_unlock(globals.limit_hash_mutex);
+	switch_thread_rwlock_unlock(globals.limit_hash_rwlock);
 
 	return count;
 }
@@ -254,19 +254,22 @@
 
 SWITCH_LIMIT_STATUS(limit_status_hash)
 {
+/*
 	switch_hash_index_t *hi = NULL;
 	int count = 0;
 	char *ret = NULL;
 	
-	switch_mutex_lock(globals.limit_hash_mutex);
+	switch_thread_rwlock_rdlock(globals.limit_hash_rwlock);
 	
 	while ((hi = switch_hash_first(NULL, globals.limit_hash))) {
 		count++;
 	}
-	switch_mutex_unlock(globals.limit_hash_mutex);
+	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 */
@@ -284,7 +287,7 @@
 	char *hash_key = NULL;
 	char *value = NULL;
 
-	switch_mutex_lock(globals.db_hash_mutex);
+	switch_thread_rwlock_wrlock(globals.db_hash_rwlock);
 
 	if (!zstr(data)) {
 		mydata = strdup(data);
@@ -324,7 +327,7 @@
 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "USAGE: hash %s\n", HASH_USAGE);
 
   done:
-	switch_mutex_unlock(globals.db_hash_mutex);
+	switch_thread_rwlock_unlock(globals.db_hash_rwlock);
 	switch_safe_free(mydata);
 	switch_safe_free(hash_key);
 }
@@ -338,8 +341,6 @@
 	char *value = NULL;
 	char *hash_key = NULL;
 
-	switch_mutex_lock(globals.db_hash_mutex);
-
 	if (!zstr(cmd)) {
 		mydata = strdup(cmd);
 		switch_assert(mydata);
@@ -356,6 +357,7 @@
 		if (argc < 4) {
 			goto usage;
 		}
+		switch_thread_rwlock_wrlock(globals.db_hash_rwlock);
 		if ((value = switch_core_hash_find(globals.db_hash, hash_key))) {
 			switch_safe_free(value);
 			switch_core_hash_delete(globals.db_hash, hash_key);
@@ -365,6 +367,7 @@
 		switch_core_hash_insert(globals.db_hash, hash_key, value);
 		stream->write_function(stream, "+OK\n");
 	} else if (!strcasecmp(argv[0], "delete")) {
+		switch_thread_rwlock_wrlock(globals.db_hash_rwlock);
 		if ((value = switch_core_hash_find(globals.db_hash, hash_key))) {
 			switch_safe_free(value);
 			switch_core_hash_delete(globals.db_hash, hash_key);
@@ -373,6 +376,7 @@
 			stream->write_function(stream, "-ERR Not found\n");
 		}
 	} else if (!strcasecmp(argv[0], "select")) {
+		switch_thread_rwlock_rdlock(globals.db_hash_rwlock);
 		if ((value = switch_core_hash_find(globals.db_hash, hash_key))) {
 			stream->write_function(stream, "%s", value);
 		}
@@ -386,7 +390,7 @@
 	stream->write_function(stream, "-ERR Usage: hash %s\n", HASH_API_USAGE);
 
   done:
-	switch_mutex_unlock(globals.db_hash_mutex);
+	switch_thread_rwlock_unlock(globals.db_hash_rwlock);
 	switch_safe_free(mydata);
 	switch_safe_free(hash_key);
 
@@ -409,8 +413,8 @@
 		return SWITCH_STATUS_FALSE;
 	}
 
-	switch_mutex_init(&globals.limit_hash_mutex, SWITCH_MUTEX_NESTED, globals.pool);
-	switch_mutex_init(&globals.db_hash_mutex, SWITCH_MUTEX_NESTED, globals.pool);
+	switch_thread_rwlock_create(&globals.limit_hash_rwlock, globals.pool);
+	switch_thread_rwlock_create(&globals.db_hash_rwlock, globals.pool);
 	switch_core_hash_init(&globals.limit_hash, pool);
 	switch_core_hash_init(&globals.db_hash, pool);
 
@@ -435,8 +439,8 @@
 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_hash_shutdown)
 {
 
-	switch_mutex_destroy(globals.db_hash_mutex);
-	switch_mutex_destroy(globals.limit_hash_mutex);
+	switch_thread_rwlock_destroy(globals.db_hash_rwlock);
+	switch_thread_rwlock_destroy(globals.limit_hash_rwlock);
 
 	switch_core_hash_destroy(&globals.limit_hash);
 	switch_core_hash_destroy(&globals.db_hash);

Added: freeswitch/branches/rupa_limit/src/switch_limit.c (0 => 17079)


--- freeswitch/branches/rupa_limit/src/switch_limit.c	                        (rev 0)
+++ freeswitch/branches/rupa_limit/src/switch_limit.c	2010-03-23 16:17:15 UTC (rev 17079)
@@ -0,0 +1,219 @@
+/*
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005-2009, Anthony Minessale II <anthm@freeswitch.org>
+ *
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ *
+ * The Initial Developer of the Original Code is
+ * Anthony Minessale II <anthm@freeswitch.org>
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Rupa Schomaker <rupa@rupa.com>
+ *
+ * switch_limit.c Limit support
+ *
+ */
+
+#include <switch.h>
+
+static switch_limit_interface_t *get_backend(const char *backend) {
+	switch_limit_interface_t *limit = NULL;
+	
+	if (!backend) {
+		return NULL;
+	}
+	
+	if (!(limit = switch_loadable_module_get_limit_interface(backend))) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Unable to locate limit backend: %s\n", backend);
+	}
+	
+	return limit;
+}
+
+static void release_backend(switch_limit_interface_t *limit) {
+	if (limit) {
+		UNPROTECT_INTERFACE(limit);
+	}
+}
+
+SWITCH_DECLARE(void) switch_limit_init(switch_memory_pool_t *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);
+	}
+}
+
+
+SWITCH_DECLARE(void) switch_limit_fire_event(const char *backend, const char *realm, const char *key, uint32_t usage, uint32_t rate, uint32_t max, uint32_t ratemax)
+{
+	switch_event_t *event;
+
+	if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, LIMIT_EVENT_USAGE) == SWITCH_STATUS_SUCCESS) {
+		switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "backend", backend);
+		switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "realm", realm);
+		switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "key", key);
+		switch_event_add_header(event, SWITCH_STACK_BOTTOM, "usage", "%d", usage);
+		switch_event_add_header(event, SWITCH_STACK_BOTTOM, "rate", "%d", rate);
+		switch_event_add_header(event, SWITCH_STACK_BOTTOM, "max", "%d", max);
+		switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ratemax", "%d", ratemax);
+		switch_event_fire(&event);
+	}
+}
+
+static switch_status_t limit_state_handler(switch_core_session_t *session)
+{
+	switch_channel_t *channel = switch_core_session_get_channel(session);
+	switch_channel_state_t state = switch_channel_get_state(channel);
+	const char *vval = switch_channel_get_variable(channel, LIMIT_IGNORE_TRANSFER_VARIABLE);
+	const char *backendlist = switch_channel_get_variable(channel, LIMIT_BACKEND_VARIABLE);
+	int argc = 0;
+	char *argv[6] = { 0 };
+	char *mydata = NULL;
+	
+	if (zstr(backendlist)) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unset limit backendlist!\n");
+		return SWITCH_STATUS_SUCCESS;
+	}
+
+	mydata = strdup(backendlist);
+	
+	if (state >= CS_HANGUP || (state == CS_ROUTING && !switch_true(vval))) {
+		int x;
+		argc = switch_separate_string(mydata, ',', argv, (sizeof(argv) / sizeof(argv[0])));
+		for (x = 0; x < argc; x++) {
+			switch_limit_release(argv[x], session, NULL, NULL);
+		}
+		switch_core_event_hook_remove_state_change(session, limit_state_handler);
+		/* Remove limit_realm variable so we register another hook if limit is called again */
+		switch_channel_set_variable(channel, "limit_realm", NULL);
+	}
+	return SWITCH_STATUS_SUCCESS;
+}
+
+
+SWITCH_DECLARE(switch_status_t) switch_limit_incr(const char *backend, switch_core_session_t *session, const char *realm, const char *resource, const int max, const int interval) {
+	switch_limit_interface_t *limit = NULL;
+	switch_channel_t *channel = NULL;
+	int status = SWITCH_STATUS_SUCCESS;
+	
+	assert(session);
+
+	channel = switch_core_session_get_channel(session);
+
+	/* locate impl, call appropriate func */
+	if (!(limit = get_backend(backend))) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
+		switch_goto_status(SWITCH_STATUS_GENERR, end);
+	}
+	
+	if ((status = limit->incr(session, realm, resource, max, interval)) == SWITCH_STATUS_SUCCESS) {
+		/* race condition? what if another leg is doing the same thing? */
+		const char *existing = switch_channel_get_variable(channel, LIMIT_BACKEND_VARIABLE);
+		if (existing) {
+			if (!strstr(existing, backend)) {
+				switch_channel_set_variable_printf(channel, LIMIT_BACKEND_VARIABLE, "%s,%s", existing, backend);
+			}
+		} else {
+			switch_channel_set_variable(channel, LIMIT_BACKEND_VARIABLE, backend);
+			switch_core_event_hook_add_state_change(session, limit_state_handler);
+		}
+	}
+	
+	release_backend(limit);
+	
+end:
+	return status;
+}
+
+SWITCH_DECLARE(switch_status_t) switch_limit_release(const char *backend, switch_core_session_t *session, const char *realm, const char *resource) {
+	switch_limit_interface_t *limit = NULL;
+	int status = SWITCH_STATUS_SUCCESS;
+	
+	/* locate impl, call appropriate func */
+	if (!(limit = get_backend(backend))) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
+		switch_goto_status(SWITCH_STATUS_GENERR, end);
+	}
+	
+	status = limit->release(session, realm, resource);
+	
+end:
+	release_backend(limit);
+	return status;
+}
+
+SWITCH_DECLARE(int) switch_limit_usage(const char *backend, const char *realm, const char *resource) {
+	switch_limit_interface_t *limit = NULL;
+	int usage = 0;
+	
+	/* locate impl, call appropriate func */
+	if (!(limit = get_backend(backend))) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
+		goto end;
+	}
+	
+	usage = limit->usage(realm, resource);
+	
+end:
+	release_backend(limit);
+	return usage;
+}
+
+SWITCH_DECLARE(switch_status_t) switch_limit_reset(const char *backend) {
+	switch_limit_interface_t *limit = NULL;
+	int status = SWITCH_STATUS_SUCCESS;
+	
+	/* locate impl, call appropriate func */
+	if (!(limit = get_backend(backend))) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
+		switch_goto_status(SWITCH_STATUS_GENERR, end);
+	}
+	
+	status = limit->reset();
+	
+end:
+	release_backend(limit);
+	return status;
+}
+
+SWITCH_DECLARE(char *) switch_limit_status(const char *backend) {
+	switch_limit_interface_t *limit = NULL;
+	char *status = NULL;
+	
+	/* locate impl, call appropriate func */
+	if (!(limit = get_backend(backend))) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Limit subsystem %s not found!\n", backend);
+		switch_goto_status("-ERR", end);
+	}
+	
+	status = limit->status();
+	
+end:
+	release_backend(limit);
+	return status;
+}
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
+ */
Property changes on: freeswitch/branches/rupa_limit/src/switch_limit.c
___________________________________________________________________
Name: svn:executable
   + *