complete cache separation
This commit is contained in:
parent
f03172ab10
commit
2c02025332
@ -18,7 +18,6 @@
|
||||
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
// #define CACHE_SEPARATED 1
|
||||
|
||||
#if HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
@ -38,14 +37,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
#ifndef CACHE_SEPARATED
|
||||
#if ENABLE_CACHE == 1
|
||||
#define DB_DBM_HSEARCH 1
|
||||
#include <db.h>
|
||||
#endif /* ENABLE_CACHE */
|
||||
#else
|
||||
#include "htcache.h"
|
||||
#endif /* CACHE_SEPARATED */
|
||||
|
||||
#include "containers_public.h"
|
||||
#include "smmapd.h"
|
||||
@ -67,14 +59,7 @@ struct verify_container_handle_s {
|
||||
int timeout_result;
|
||||
int timeout_dialog;
|
||||
|
||||
#ifndef CACHE_SEPARATED
|
||||
int cache_enabled;
|
||||
int cache_expiry;
|
||||
pthread_mutex_t *cache_mutex;
|
||||
char *cache_file;
|
||||
#else
|
||||
htcache_t *cache;
|
||||
#endif /* CACHE_SEPARATED */
|
||||
|
||||
char *sender_address;
|
||||
char *helo_arg;
|
||||
@ -139,9 +124,6 @@ typedef struct worker_thread_s worker_thread_t;
|
||||
|
||||
struct mydata_s {
|
||||
int result;
|
||||
#ifndef CACHE_SEPARATED
|
||||
time_t timestamp;
|
||||
#endif /* CACHE_SEPARATED */
|
||||
char output[1];
|
||||
};
|
||||
|
||||
@ -188,19 +170,6 @@ int verify_init(cfgl_t *cfg, void **handle) {
|
||||
vch->smtp_port = atoi(findcfglx(vch->cfg, "smtp_port", "25"));
|
||||
vch->max_checker_threads = atoi(findcfglx(vch->cfg, "max_checker_threads", "25"));
|
||||
|
||||
#ifndef CACHE_SEPARATED
|
||||
vch->cache_enabled = atoi(findcfglx(vch->cfg, "cache_enabled", "1"));
|
||||
vch->cache_expiry = atoi(findcfglx(vch->cfg, "cache_expiry", "86400"));
|
||||
vch->cache_file = findcfglx(vch->cfg, "cache_file", "verifier_cache");
|
||||
|
||||
if (1 == vch->cache_enabled) {
|
||||
vch->cache_mutex = (pthread_mutex_t*) htmalloc(sizeof(pthread_mutex_t));
|
||||
pthread_mutex_init(vch->cache_mutex, NULL);
|
||||
pthread_mutex_unlock(vch->cache_mutex);
|
||||
} else {
|
||||
vch->cache_mutex = NULL;
|
||||
}
|
||||
#else
|
||||
cache_enabled = atoi(findcfglx(vch->cfg, "cache_enabled", "1"));
|
||||
cache_expiry = atoi(findcfglx(vch->cfg, "cache_expiry", "86400"));
|
||||
cache_file = findcfglx(vch->cfg, "cache_file", "verifier_cache");
|
||||
@ -210,7 +179,6 @@ int verify_init(cfgl_t *cfg, void **handle) {
|
||||
} else {
|
||||
vch->cache = NULL;
|
||||
}
|
||||
#endif /* CACHE_SEPARATED */
|
||||
|
||||
*handle = vch;
|
||||
return 0;
|
||||
@ -221,17 +189,9 @@ int verify_init(cfgl_t *cfg, void **handle) {
|
||||
int verify_destroy(void *handle) {
|
||||
verify_container_handle_t *vch = (verify_container_handle_t*)handle;
|
||||
|
||||
#ifndef CACHE_SEPARATED
|
||||
if (1 == vch->cache_enabled) {
|
||||
pthread_mutex_destroy(vch->cache_mutex);
|
||||
free(vch->cache_mutex);
|
||||
}
|
||||
#else
|
||||
if (NULL != vch->cache) {
|
||||
cache_destroy(vch->cache);
|
||||
htcache_destroy(vch->cache);
|
||||
}
|
||||
#endif /* CACHE_SEPARATED */
|
||||
|
||||
|
||||
free(vch);
|
||||
return 0;
|
||||
@ -324,104 +284,39 @@ int verify_work_destroy(void *handle, void *work_handle) {
|
||||
|
||||
void cache_insert(verify_container_handle_t *vch, const char *address, int result, const char *output) {
|
||||
#if ENABLE_CACHE == 1
|
||||
#ifndef CACHE_SEPARATED
|
||||
DBM *cache;
|
||||
datum data, key;
|
||||
int ret;
|
||||
#endif /* CACHE_SEPARATED */
|
||||
|
||||
int mydata_size;
|
||||
mydata_t *mydata;
|
||||
|
||||
if (1 == vch->cache_enabled) {
|
||||
if (NULL != vch->cache) {
|
||||
syslog(LOG_DEBUG, "cache_insert: inserting %s -> %d, %s", address, result, output);
|
||||
mydata = (mydata_t *) htmalloc(data.dsize);
|
||||
mydata->result = result;
|
||||
mydata->timestamp = time(NULL);
|
||||
strcpy(mydata->output, output);
|
||||
mydata_size = (sizeof(mydata_t) + (sizeof(char) * (strlen(output) + 1)));
|
||||
mydata = (mydata_t *) htmalloc(mydata_size);
|
||||
mydata->result = result;
|
||||
strcpy(mydata->output, output);
|
||||
|
||||
#ifndef CACHE_SEPARATED
|
||||
data.dptr = (char*) mydata;
|
||||
key.dsize = strlen(address) + 1; /* one more for the terminating \0 */
|
||||
key.dptr = (char*) address;
|
||||
data.dsize = mydata_size;
|
||||
|
||||
pthread_mutex_lock(vch->cache_mutex);
|
||||
if (NULL != (cache = dbm_open(vch->cache_file, O_RDWR | O_CREAT, 0644))) {
|
||||
ret = dbm_store(cache, key, data, DBM_INSERT);
|
||||
if (ret != 0) {
|
||||
syslog(LOG_DEBUG, "cache_insert: couldn't insert record");
|
||||
} else {
|
||||
incStatCounter(STAT_CNT_VERIFIER_CACHE);
|
||||
syslog(LOG_DEBUG, "cache_insert: record inserted");
|
||||
}
|
||||
dbm_close(cache);
|
||||
}
|
||||
pthread_mutex_unlock(vch->cache_mutex);
|
||||
#else /* CACHE_SEPARATED */
|
||||
htcache_insert(vch->cache, address, (const char*) mydata, mydata_size);
|
||||
#endif /* CACHE_SEPARATED */
|
||||
|
||||
|
||||
free(mydata);
|
||||
}
|
||||
#else
|
||||
syslog(LOG_DEBUG, "cache_insert: cache disabled");
|
||||
#endif /* ENABLE_CACHE */
|
||||
}
|
||||
|
||||
int cache_lookup(verify_container_handle_t *vch, const char* address, int *result, char **output) {
|
||||
#if ENABLE_CACHE == 1
|
||||
DBM *cache;
|
||||
datum data, key;
|
||||
mydata_t *mydata;
|
||||
int ret, res = -1;
|
||||
int mydata_size;
|
||||
|
||||
if (1 == vch->cache_enabled) {
|
||||
syslog(LOG_DEBUG, "cache_lookup: looking up %s, expiry %d",
|
||||
address, vch->cache_expiry);
|
||||
|
||||
if (NULL != (cache = dbm_open(vch->cache_file, O_RDONLY, 0644))) {
|
||||
key.dsize = strlen(address) + 1; /* one more for the terminating \0 */
|
||||
key.dptr = (char*) address;
|
||||
data = dbm_fetch(cache, key);
|
||||
if (NULL == data.dptr) {
|
||||
syslog(LOG_DEBUG, "cache_lookup: nothing found");
|
||||
dbm_close(cache);
|
||||
} else {
|
||||
mydata = (mydata_t *) data.dptr;
|
||||
syslog(LOG_DEBUG, "cache_lookup: found: %s -> %d, %d, %s",
|
||||
address, mydata->result, mydata->timestamp, mydata->output);
|
||||
if ((mydata->timestamp + vch->cache_expiry) > time(NULL)) {
|
||||
syslog(LOG_DEBUG, "cache_lookup: not yet expired");
|
||||
*result = mydata->result;
|
||||
*output = (char*) htmalloc(sizeof(char) * (strlen(mydata->output) + 1));
|
||||
strcpy(*output, mydata->output);
|
||||
|
||||
/* Berkeley DB frees on its own! */
|
||||
/* free(data.dptr); */
|
||||
dbm_close(cache);
|
||||
res = 0;
|
||||
} else {
|
||||
syslog(LOG_DEBUG, "cache_lookup: expired, will be deleted from cache");
|
||||
/* free(data.dptr); --- Berkeley DB frees on its own */
|
||||
dbm_close(cache);
|
||||
|
||||
pthread_mutex_lock(vch->cache_mutex);
|
||||
if (NULL != (cache = dbm_open(vch->cache_file, O_RDWR, 0644))) {
|
||||
ret = dbm_delete(cache, key);
|
||||
if (ret != 0) {
|
||||
syslog(LOG_DEBUG, "cache_insert: couldn't delete record");
|
||||
} else {
|
||||
decStatCounter(STAT_CNT_VERIFIER_CACHE);
|
||||
syslog(LOG_DEBUG, "cache_insert: record deleted");
|
||||
}
|
||||
dbm_close(cache);
|
||||
}
|
||||
pthread_mutex_unlock(vch->cache_mutex);
|
||||
}
|
||||
}
|
||||
if (NULL != vch->cache) {
|
||||
syslog(LOG_DEBUG, "cache_lookup: Looking up %s", address);
|
||||
ret = htcache_lookup(vch->cache, address, (char**) &mydata, &mydata_size);
|
||||
if (0 == ret) {
|
||||
*result = mydata->result;
|
||||
*output = (char*) htmalloc(sizeof(char) * (strlen(mydata->output) + 1));
|
||||
strcpy(*output, mydata->output);
|
||||
res = 0;
|
||||
free(mydata);
|
||||
syslog(LOG_DEBUG, "cache_lookup: found %d %s", *result, *output);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user