]> source.dussan.org Git - rspamd.git/commitdiff
Enable per-users learn cache
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 11 Jan 2016 15:16:09 +0000 (15:16 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 11 Jan 2016 15:16:09 +0000 (15:16 +0000)
src/libstat/backends/redis_backend.c
src/libstat/backends/sqlite3_backend.c
src/libstat/learn_cache/redis_cache.c
src/libstat/learn_cache/sqlite3_cache.c

index f339668caa08f49834e136ff4c506de0dce56d2f..5d6ba7d98907cfb223c8c228a2a2647b42b934de 100644 (file)
@@ -130,32 +130,40 @@ rspamd_redis_expand_object (const gchar *pattern,
        stcf = ctx->stcf;
        L = task->cfg->lua_state;
 
-       if (ctx->cbref_user == -1) {
-               rcpt = rspamd_task_get_principal_recipient (task);
-       }
-       else {
-               /* Execute lua function to get userdata */
-               lua_pushcfunction (L, &rspamd_lua_traceback);
-               err_idx = lua_gettop (L);
-
-               lua_rawgeti (L, LUA_REGISTRYINDEX, ctx->cbref_user);
-               ptask = lua_newuserdata (L, sizeof (struct rspamd_task *));
-               *ptask = task;
-               rspamd_lua_setclass (L, "rspamd{task}", -1);
-
-               if (lua_pcall (L, 1, 1, err_idx) != 0) {
-                       tb = lua_touserdata (L, -1);
-                       msg_err_task ("call to user extraction script failed: %v", tb);
-                       g_string_free (tb, TRUE);
+       if (ctx->enable_users) {
+               if (ctx->cbref_user == -1) {
+                       rcpt = rspamd_task_get_principal_recipient (task);
                }
                else {
-                       rcpt = rspamd_mempool_strdup (task->task_pool, lua_tostring (L, -1));
+                       /* Execute lua function to get userdata */
+                       lua_pushcfunction (L, &rspamd_lua_traceback);
+                       err_idx = lua_gettop (L);
+
+                       lua_rawgeti (L, LUA_REGISTRYINDEX, ctx->cbref_user);
+                       ptask = lua_newuserdata (L, sizeof (struct rspamd_task *));
+                       *ptask = task;
+                       rspamd_lua_setclass (L, "rspamd{task}", -1);
+
+                       if (lua_pcall (L, 1, 1, err_idx) != 0) {
+                               tb = lua_touserdata (L, -1);
+                               msg_err_task ("call to user extraction script failed: %v", tb);
+                               g_string_free (tb, TRUE);
+                       }
+                       else {
+                               rcpt = rspamd_mempool_strdup (task->task_pool, lua_tostring (L, -1));
+                       }
+
+                       /* Result + error function */
+                       lua_pop (L, 2);
                }
 
-               /* Result + error function */
-               lua_pop (L, 2);
+               if (rcpt) {
+                       rspamd_mempool_set_variable (task->task_pool, "stat_user", rcpt, NULL);
+               }
        }
 
+
+
        /* Length calculation */
        while (*p) {
                switch (state) {
index 7000226e27ca6bdd58f51b11d8462f9e44378daa..ee9e532148ac2f533c193fb3bfb1ad4f15553e9f 100644 (file)
@@ -335,6 +335,8 @@ rspamd_sqlite3_get_user (struct rspamd_stat_sqlite3_db *db,
 
 
        if (user != NULL) {
+               rspamd_mempool_set_variable (task->task_pool, "stat_user", user, NULL);
+
                rc = rspamd_sqlite3_run_prstmt (task->task_pool, db->sqlite, db->prstmt,
                                RSPAMD_STAT_BACKEND_GET_USER, user, &id);
 
index c1dd89fc43006678e9f8c74e0624e48d45a00269..42474803919c7a86f23f12dd28caea3067558efb 100644 (file)
@@ -160,9 +160,16 @@ rspamd_stat_cache_redis_generate_id (struct rspamd_task *task)
        guint i;
        guchar out[rspamd_cryptobox_HASHBYTES];
        gchar *b32out;
+       gchar *user = NULL;
 
        rspamd_cryptobox_hash_init (&st, NULL, 0);
 
+       user = rspamd_mempool_get_variable (task->task_pool, "stat_user");
+       /* Use dedicated hash space for per users cache */
+       if (user != NULL) {
+               rspamd_cryptobox_hash_update (&st, user, strlen (user));
+       }
+
        for (i = 0; i < task->tokens->len; i ++) {
                tok = g_ptr_array_index (task->tokens, i);
                rspamd_cryptobox_hash_update (&st, tok->data, tok->datalen);
index 7b46afefc8c508e08337bbfae23e4e90def3c14a..77a2cafefbddce59c1195ddf18086a5446f50769 100644 (file)
@@ -187,6 +187,7 @@ rspamd_stat_cache_sqlite3_check (struct rspamd_task *task,
        rspamd_cryptobox_hash_state_t st;
        rspamd_ftok_t *word;
        guchar *out;
+       gchar *user = NULL;
        guint i, j;
        gint rc;
        gint64 flag;
@@ -196,6 +197,12 @@ rspamd_stat_cache_sqlite3_check (struct rspamd_task *task,
 
                rspamd_cryptobox_hash_init (&st, NULL, 0);
 
+               user = rspamd_mempool_get_variable (task->task_pool, "stat_user");
+               /* Use dedicated hash space for per users cache */
+               if (user != NULL) {
+                       rspamd_cryptobox_hash_update (&st, user, strlen (user));
+               }
+
                for (i = 0; i < task->text_parts->len; i ++) {
                        part = g_ptr_array_index (task->text_parts, i);