FS-4871 --resolve

This commit is contained in:
Raymond Chandler 2013-05-07 15:07:04 -04:00 committed by Anthony Minessale
parent 22076b7b6f
commit 1e6d7ea113
1 changed files with 89 additions and 35 deletions

View File

@ -26,6 +26,7 @@
* Anthony Minessale II <anthm@freeswitch.org>
* Simon Capper <skyjunky@sbcglobal.net>
* Marc Olivier Chouinard <mochouinard@moctel.com>
* Raymond Chandler <intralanman@freeswitch.org>
*
* switch_xml.c -- XML PARSER
*
@ -150,6 +151,7 @@ static switch_xml_open_root_function_t XML_OPEN_ROOT_FUNCTION = (switch_xml_open
static void *XML_OPEN_ROOT_FUNCTION_USER_DATA = NULL;
static switch_hash_t *CACHE_HASH = NULL;
static switch_hash_t *CACHE_EXPIRES_HASH = NULL;
struct xml_section_t {
const char *name;
@ -1897,6 +1899,9 @@ SWITCH_DECLARE(uint32_t) switch_xml_clear_user_cache(const char *key, const char
if ((lookup = switch_core_hash_find(CACHE_HASH, mega_key))) {
switch_core_hash_delete(CACHE_HASH, mega_key);
if ((lookup = switch_core_hash_find(CACHE_EXPIRES_HASH, mega_key))) {
switch_core_hash_delete(CACHE_EXPIRES_HASH, mega_key);
}
switch_xml_free(lookup);
r++;
}
@ -1909,6 +1914,12 @@ SWITCH_DECLARE(uint32_t) switch_xml_clear_user_cache(const char *key, const char
switch_core_hash_delete(CACHE_HASH, var);
r++;
}
while ((hi = switch_hash_first(NULL, CACHE_EXPIRES_HASH))) {
switch_hash_this(hi, &var, NULL, &val);
switch_safe_free(val);
switch_core_hash_delete(CACHE_EXPIRES_HASH, var);
}
}
switch_mutex_unlock(CACHE_MUTEX);
@ -1920,33 +1931,61 @@ SWITCH_DECLARE(uint32_t) switch_xml_clear_user_cache(const char *key, const char
static switch_status_t switch_xml_locate_user_cache(const char *key, const char *user_name, const char *domain_name, switch_xml_t *user)
{
char mega_key[1024];
switch_xml_t lookup;
switch_status_t status = SWITCH_STATUS_FALSE;
switch_xml_t lookup;
switch_snprintf(mega_key, sizeof(mega_key), "%s%s%s", key, user_name, domain_name);
switch_mutex_lock(CACHE_MUTEX);
if ((lookup = switch_core_hash_find(CACHE_HASH, mega_key))) {
char *expires_lookup = NULL;
if ((expires_lookup = switch_core_hash_find(CACHE_EXPIRES_HASH, mega_key))) {
switch_time_t time_expires = 0;
switch_time_t time_now = 0;
time_now = switch_micro_time_now();
time_expires = atol(expires_lookup);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Cache Info\nTime Now:\t%ld\nExpires:\t%ld\n", time_now, time_expires);
if (time_expires < time_now) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Cache expired for %s@%s, doing fresh lookup\n", user_name, domain_name);
} else {
*user = switch_xml_dup(lookup);
status = SWITCH_STATUS_SUCCESS;
}
} else {
*user = switch_xml_dup(lookup);
status = SWITCH_STATUS_SUCCESS;
}
}
switch_mutex_unlock(CACHE_MUTEX);
return status;
}
static void switch_xml_user_cache(const char *key, const char *user_name, const char *domain_name, switch_xml_t user)
static void switch_xml_user_cache(const char *key, const char *user_name, const char *domain_name, switch_xml_t user, switch_time_t expires)
{
char mega_key[1024];
switch_xml_t lookup;
char *expires_lookup;
switch_snprintf(mega_key, sizeof(mega_key), "%s%s%s", key, user_name, domain_name);
switch_mutex_lock(CACHE_MUTEX);
if ((lookup = switch_core_hash_find(CACHE_HASH, mega_key))) {
switch_core_hash_delete(CACHE_HASH, mega_key);
switch_xml_free(lookup);
}
if ((expires_lookup = switch_core_hash_find(CACHE_EXPIRES_HASH, mega_key))) {
switch_core_hash_delete(CACHE_EXPIRES_HASH, mega_key);
switch_safe_free(expires_lookup);
}
if (expires) {
char *expires_val = malloc(1024);
if (sprintf(expires_val, "%ld", expires)) {
switch_core_hash_insert(CACHE_EXPIRES_HASH, mega_key, expires_val);
}
}
switch_core_hash_insert(CACHE_HASH, mega_key, switch_xml_dup(user));
switch_mutex_unlock(CACHE_MUTEX);
}
@ -1960,10 +1999,24 @@ SWITCH_DECLARE(switch_status_t) switch_xml_locate_user_merged(const char *key, c
if ((status = switch_xml_locate_user_cache(key, user_name, domain_name, &x_user)) == SWITCH_STATUS_SUCCESS) {
*user = x_user;
} else if ((status = switch_xml_locate_user(key, user_name, domain_name, ip, &xml, &domain, &x_user, &group, params)) == SWITCH_STATUS_SUCCESS) {
const char *cacheable = NULL;
x_user_dup = switch_xml_dup(x_user);
switch_xml_merge_user(x_user_dup, domain, group);
if (switch_true(switch_xml_attr(x_user_dup, "cacheable"))) {
switch_xml_user_cache(key, user_name, domain_name, x_user_dup);
cacheable = switch_xml_attr(x_user_dup, "cacheable");
if (switch_true(cacheable)) {
switch_time_t expires = 0;
switch_time_t time_now = 0;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "caching lookup for user %s@%s\n", user_name, domain_name);
if (switch_is_number(cacheable)) {
int cache_ms = atol(cacheable);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "caching lookup for %d milliseconds\n", cache_ms);
time_now = switch_micro_time_now();
expires = time_now + (cache_ms * 1000);
}
switch_xml_user_cache(key, user_name, domain_name, x_user_dup, expires);
}
*user = x_user_dup;
switch_xml_free(xml);
@ -2225,6 +2278,7 @@ SWITCH_DECLARE(switch_status_t) switch_xml_init(switch_memory_pool_t *pool, cons
switch_mutex_init(&FILE_LOCK, SWITCH_MUTEX_NESTED, XML_MEMORY_POOL);
switch_mutex_init(&XML_GEN_LOCK, SWITCH_MUTEX_NESTED, XML_MEMORY_POOL);
switch_core_hash_init(&CACHE_HASH, XML_MEMORY_POOL);
switch_core_hash_init(&CACHE_EXPIRES_HASH, XML_MEMORY_POOL);
switch_thread_rwlock_create(&B_RWLOCK, XML_MEMORY_POOL);