提交 e4258a83 authored 作者: Rupa Schomaker's avatar Rupa Schomaker

add limit_execute (like limit_hash_execute)

allow -#s to act as counter only
add INFO level logs to limit similar to limit_hash


git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@14870 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 7d3ded08
...@@ -790,6 +790,67 @@ SWITCH_STANDARD_APP(group_function) ...@@ -790,6 +790,67 @@ SWITCH_STANDARD_APP(group_function)
} }
} }
/* \brief Enforces limit restrictions
* \param session current session
* \param realm limit realm
* \param id limit id
* \param max maximum count
* \return SWITCH_TRUE if the access is allowed, SWITCH_FALSE if it isnt
*/
static switch_bool_t do_limit(switch_core_session_t *session, const char *realm, const char *id, int max)
{
switch_channel_t *channel = switch_core_session_get_channel(session);
int got = 0;
char *sql = NULL;
char buf[80] = "";
callback_t cbt = { 0 };
switch_status_t status = SWITCH_TRUE;
switch_mutex_lock(globals.mutex);
switch_channel_set_variable(channel, "limit_realm", realm);
switch_channel_set_variable(channel, "limit_id", id);
switch_channel_set_variable(channel, "limit_max", switch_core_session_sprintf(session, "%d", max));
cbt.buf = buf;
cbt.len = sizeof(buf);
sql = switch_mprintf("select count(hostname) from limit_data where realm='%q' and id like '%q'", realm, id);
limit_execute_sql_callback(NULL, sql, sql2str_callback, &cbt);
switch_safe_free(sql);
got = atoi(buf);
if (max < 0) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Usage for %s_%s is now %d\n", realm, id, got+1);
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Usage for %s_%s is now %d/%d\n", realm, id, got+1, max);
}
if (max >= 0 && got + 1 > max) {
status = SWITCH_FALSE;
goto done;
}
switch_core_event_hook_add_state_change(session, db_state_handler);
sql =
switch_mprintf("insert into limit_data (hostname, realm, id, uuid) values('%q','%q','%q','%q');", globals.hostname, realm, id,
switch_core_session_get_uuid(session));
limit_execute_sql(sql, NULL);
switch_safe_free(sql);
{
const char *susage = switch_core_session_sprintf(session, "%d", ++got);
switch_channel_set_variable(channel, "limit_usage", susage);
switch_channel_set_variable(channel, switch_core_session_sprintf(session, "limit_usage_%s_%s", realm, id), susage);
}
limit_fire_event(realm, id, got, 0, max, 0);
done:
switch_mutex_unlock(globals.mutex);
return status;
}
#define LIMIT_USAGE "<realm> <id> [<max> [number [dialplan [context]]]]" #define LIMIT_USAGE "<realm> <id> [<max> [number [dialplan [context]]]]"
#define LIMIT_DESC "limit access to a resource and transfer to an extension if the limit is exceeded" #define LIMIT_DESC "limit access to a resource and transfer to an extension if the limit is exceeded"
static char *limit_def_xfer_exten = "limit_exceeded"; static char *limit_def_xfer_exten = "limit_exceeded";
...@@ -799,13 +860,10 @@ SWITCH_STANDARD_APP(limit_function) ...@@ -799,13 +860,10 @@ SWITCH_STANDARD_APP(limit_function)
int argc = 0; int argc = 0;
char *argv[6] = { 0 }; char *argv[6] = { 0 };
char *mydata = NULL; char *mydata = NULL;
char *sql = NULL;
char *realm = NULL; char *realm = NULL;
char *id = NULL; char *id = NULL;
char *xfer_exten = NULL; char *xfer_exten = NULL;
int max = 0, got = 0; int max = 0;
char buf[80] = "";
callback_t cbt = { 0 };
switch_channel_t *channel = switch_core_session_get_channel(session); switch_channel_t *channel = switch_core_session_get_channel(session);
if (!switch_strlen_zero(data)) { if (!switch_strlen_zero(data)) {
...@@ -818,19 +876,18 @@ SWITCH_STANDARD_APP(limit_function) ...@@ -818,19 +876,18 @@ SWITCH_STANDARD_APP(limit_function)
return; return;
} }
switch_mutex_lock(globals.mutex);
realm = argv[0]; realm = argv[0];
id = argv[1]; id = argv[1];
if (argc > 2) { /* Accept '-' as unlimited (act as counter)*/
if (argv[2][0] == '-') {
max = -1;
} else {
max = atoi(argv[2]); max = atoi(argv[2]);
if (max < 0) { if (max < 0) {
max = 0; max = 0;
} }
}
else {
max = -1;
} }
if (argc >= 4) { if (argc >= 4) {
...@@ -839,45 +896,88 @@ SWITCH_STANDARD_APP(limit_function) ...@@ -839,45 +896,88 @@ SWITCH_STANDARD_APP(limit_function)
xfer_exten = limit_def_xfer_exten; xfer_exten = limit_def_xfer_exten;
} }
if (!do_limit(session, realm, id, max)) {
/* Limit exceeded */
if (*xfer_exten == '!') {
switch_channel_hangup(channel, switch_channel_str2cause(xfer_exten+1));
} else {
switch_ivr_session_transfer(session, xfer_exten, argv[4], argv[5]);
}
}
switch_channel_set_variable(channel, "limit_realm", realm); }
switch_channel_set_variable(channel, "limit_id", id);
switch_channel_set_variable(channel, "limit_max", argv[2]); /* !\brief Releases usage of a limit_-controlled ressource */
static void limit_release(switch_core_session_t *session, const char *realm, const char *id)
{
char *sql = NULL;
char buf[80] = "";
callback_t cbt = { 0 };
switch_mutex_lock(globals.mutex);
cbt.buf = buf; cbt.buf = buf;
cbt.len = sizeof(buf); cbt.len = sizeof(buf);
sql = switch_mprintf("select count(hostname) from limit_data where realm='%q' and id like '%q'", realm, id); sql = switch_mprintf("delete from limit_data where uuid='%q' and realm='%q' and id like '%q'",
switch_core_session_get_uuid(session), realm, id);
limit_execute_sql_callback(NULL, sql, sql2str_callback, &cbt); limit_execute_sql_callback(NULL, sql, sql2str_callback, &cbt);
switch_safe_free(sql); switch_safe_free(sql);
got = atoi(buf);
switch_mutex_unlock(globals.mutex);
}
if (max >= 0 && got + 1 > max) { #define LIMITEXECUTE_USAGE "<realm> <id> [<max>] [application] [application arguments]"
switch_ivr_session_transfer(session, xfer_exten, argv[4], argv[5]); #define LIMITEXECUTE_DESC "limit access to a resource. the specified application will only be executed if the resource is available"
goto done; SWITCH_STANDARD_APP(limit_execute_function)
{
int argc = 0;
char *argv[5] = { 0 };
char *mydata = NULL;
char *realm = NULL;
char *id = NULL;
char *app = NULL;
char *app_arg = NULL;
int max = -1;
/* Parse application data */
if (!switch_strlen_zero(data)) {
mydata = switch_core_session_strdup(session, data);
argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
}
if (argc < 2) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "USAGE: limit_execute %s\n", LIMITEXECUTE_USAGE);
return;
} }
realm = argv[0];
id = argv[1];
/* Accept '-' as unlimited (act as counter)*/
if (argv[2][0] == '-') {
max = -1;
} else {
max = atoi(argv[2]);
switch_core_event_hook_add_state_change(session, db_state_handler); if (max < 0) {
switch_core_event_hook_add_state_change(session, db_state_handler); max = 0;
}
}
sql = app = argv[3];
switch_mprintf("insert into limit_data (hostname, realm, id, uuid) values('%q','%q','%q','%q');", globals.hostname, realm, id, app_arg = argv[4];
switch_core_session_get_uuid(session));
limit_execute_sql(sql, NULL);
switch_safe_free(sql);
{
const char *susage = switch_core_session_sprintf(session, "%d", ++got);
switch_channel_set_variable(channel, "limit_usage", susage); if (switch_strlen_zero(app)) {
switch_channel_set_variable(channel, switch_core_session_sprintf(session, "limit_usage_%s_%s", realm, id), susage); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Missing application\n");
return;
} }
limit_fire_event(realm, id, got, 0, max, 0);
done: if (do_limit(session, realm, id, max)) {
switch_mutex_unlock(globals.mutex); switch_core_session_execute_application(session, app, app_arg);
limit_release(session, realm, id);
}
} }
#define LIMIT_USAGE_USAGE "<realm> <id>" #define LIMIT_USAGE_USAGE "<realm> <id>"
SWITCH_STANDARD_API(limit_usage_function) SWITCH_STANDARD_API(limit_usage_function)
{ {
...@@ -1591,6 +1691,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_limit_load) ...@@ -1591,6 +1691,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_limit_load)
SWITCH_ADD_APP(app_interface, "limit", "Limit", LIMIT_DESC, limit_function, LIMIT_USAGE, SAF_SUPPORT_NOMEDIA); 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_ADD_APP(app_interface, "limit_hash", "Limit (hash)", LIMITHASH_DESC, limit_hash_function, LIMITHASH_USAGE, SAF_SUPPORT_NOMEDIA); SWITCH_ADD_APP(app_interface, "limit_hash", "Limit (hash)", LIMITHASH_DESC, limit_hash_function, LIMITHASH_USAGE, SAF_SUPPORT_NOMEDIA);
SWITCH_ADD_APP(app_interface, "limit_hash_execute", "Limit (hash)", LIMITHASHEXECUTE_USAGE, limit_hash_execute_function, LIMITHASHEXECUTE_USAGE, SAF_SUPPORT_NOMEDIA); SWITCH_ADD_APP(app_interface, "limit_hash_execute", "Limit (hash)", LIMITHASHEXECUTE_USAGE, limit_hash_execute_function, LIMITHASHEXECUTE_USAGE, SAF_SUPPORT_NOMEDIA);
SWITCH_ADD_APP(app_interface, "limit_memcache", "Limit (memcache)", LIMITMEMCACHE_DESC, limit_memcache_function, LIMITMEMCACHE_USAGE, SAF_SUPPORT_NOMEDIA); SWITCH_ADD_APP(app_interface, "limit_memcache", "Limit (memcache)", LIMITMEMCACHE_DESC, limit_memcache_function, LIMITMEMCACHE_USAGE, SAF_SUPPORT_NOMEDIA);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论