]> source.dussan.org Git - rspamd.git/commitdiff
Start new era of regexp cache
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 3 Dec 2015 17:10:34 +0000 (17:10 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 3 Dec 2015 17:10:34 +0000 (17:10 +0000)
src/libserver/cfg_file.h
src/libserver/cfg_utils.c
src/libserver/re_cache.c
src/libserver/re_cache.h
src/libserver/task.c
src/libserver/task.h

index 05946a79e02c287c6c9f9daaa78ea430af087eba..36e6371d8de90647c436ea27a9f67d267c3ba91d 100644 (file)
@@ -13,6 +13,7 @@
 #include "cfg_rcl.h"
 #include "ucl.h"
 #include "regexp.h"
+#include "libserver/re_cache.h"
 #include "ref.h"
 
 #define DEFAULT_BIND_PORT 11333
@@ -327,6 +328,8 @@ struct rspamd_config {
 
        struct rspamd_external_libs_ctx *libs_ctx;              /**< context for external libraries                                             */
 
+       struct rspamd_re_cache *re_cache;                               /**< static regexp cache                                                                */
+
        ref_entry_t ref;                                                                /**< reference counter                                                                  */
 };
 
index 042658f8a3b8b7b13d1856810e98fcef26c72bb1..2d185a59d384317e7e9c049b256f88a163ab69f2 100644 (file)
@@ -198,6 +198,7 @@ rspamd_config_new (void)
        cfg->lua_state = rspamd_lua_init (cfg);
        cfg->cache = rspamd_symbols_cache_new (cfg);
        cfg->ups_ctx = rspamd_upstreams_library_init ();
+       cfg->re_cache = rspamd_regexp_cache_new ();
 
        REF_INIT_RETAIN (cfg, rspamd_config_free);
 
@@ -229,6 +230,7 @@ rspamd_config_free (struct rspamd_config *cfg)
        g_list_free (cfg->metrics_list);
        rspamd_symbols_cache_destroy (cfg->cache);
        REF_RELEASE (cfg->libs_ctx);
+       rspamd_re_cache_unref (cfg->re_cache);
        rspamd_upstreams_library_unref (cfg->ups_ctx);
        rspamd_mempool_delete (cfg->cfg_pool);
        lua_close (cfg->lua_state);
index 18106a7c66c08af7067d992c6f7841fa3a6cd902..5ec8dd0aab05fad6da11aa2e1607bbda85507d39 100644 (file)
@@ -28,6 +28,8 @@
 #include "cryptobox.h"
 #include "ref.h"
 #include "libserver/url.h"
+#include "libserver/task.h"
+#include "libutil/util.h"
 
 struct rspamd_re_class {
        guint64 id;
@@ -69,6 +71,36 @@ rspamd_re_cache_class_id (enum rspamd_re_type type,
        return XXH64_digest (&st);
 }
 
+static void
+rspamd_re_cache_destroy (struct rspamd_re_cache *cache)
+{
+       GHashTableIter it;
+       gpointer k, v;
+       struct rspamd_re_class *re_class;
+       rspamd_regexp_t *re;
+       guint i;
+
+       g_assert (cache != NULL);
+       g_hash_table_iter_init (&it, cache->re_classes);
+
+       while (g_hash_table_iter_next (&it, &k, &v)) {
+               re_class = v;
+               g_hash_table_iter_steal (&it);
+
+               for (i = 0; i < re_class->all_re->len; i++) {
+                       re = g_ptr_array_index (re_class->all_re, i);
+
+                       rspamd_regexp_set_cache_id (re, RSPAMD_INVALID_ID);
+                       rspamd_regexp_unref (re);
+               }
+
+               g_slice_free1 (sizeof (*re_class), re_class);
+       }
+
+       g_hash_table_unref (cache->re_classes);
+       g_slice_free1 (sizeof (*cache), cache);
+}
+
 struct rspamd_re_cache *
 rspamd_re_cache_new (void)
 {
@@ -412,31 +444,32 @@ rspamd_re_cache_runtime_destroy (struct rspamd_re_runtime *rt)
 }
 
 void
-rspamd_re_cache_destroy (struct rspamd_re_cache *cache)
+rspamd_re_cache_unref (struct rspamd_re_cache *cache)
 {
-       GHashTableIter it;
-       gpointer k, v;
-       struct rspamd_re_class *re_class;
-       rspamd_regexp_t *re;
-       guint i;
+       if (cache) {
+               REF_RELEASE (cache);
+       }
+}
 
-       g_assert (cache != NULL);
-       g_hash_table_iter_init (&it, cache->re_classes);
+struct rspamd_re_cache *
+rspamd_re_cache_ref (struct rspamd_re_cache *cache)
+{
+       if (cache) {
+               REF_RETAIN (cache);
+       }
 
-       while (g_hash_table_iter_next (&it, &k, &v)) {
-               re_class = v;
-               g_hash_table_iter_steal (&it);
+       return cache;
+}
 
-               for (i = 0; i < re_class->all_re->len; i++) {
-                       re = g_ptr_array_index (re_class->all_re, i);
+guint
+rspamd_re_cache_set_limit (struct rspamd_re_cache *cache, guint limit)
+{
+       guint old;
 
-                       rspamd_regexp_set_cache_id (re, RSPAMD_INVALID_ID);
-                       rspamd_regexp_unref (re);
-               }
+       g_assert (cache != NULL);
 
-               g_slice_free1 (sizeof (*re_class), re_class);
-       }
+       old = cache->max_re_data;
+       cache->max_re_data = limit;
 
-       g_hash_table_unref (cache->re_classes);
-       g_slice_free1 (sizeof (*cache), cache);
+       return old;
 }
index ade0a1164daa8bd1b1e0d40f3d0c3e6c308b8ecc..ae23fc89a030a77612d3f1b509af8686f74590cc 100644 (file)
 #define RSPAMD_RE_CACHE_H
 
 #include "config.h"
-#include "libserver/task.h"
 #include "libutil/regexp.h"
 
 struct rspamd_re_cache;
 struct rspamd_re_runtime;
+struct rspamd_task;
 
 enum rspamd_re_type {
        RSPAMD_RE_HEADER,
@@ -91,8 +91,17 @@ gint rspamd_re_cache_process (struct rspamd_task *task,
 void rspamd_re_cache_runtime_destroy (struct rspamd_re_runtime *rt);
 
 /**
- * Destroy re cache
+ * Unref re cache
  */
-void rspamd_re_cache_destroy (struct rspamd_re_cache *cache);
+void rspamd_re_cache_unref (struct rspamd_re_cache *cache);
+/**
+ * Retain reference to re cache
+ */
+struct rspamd_re_cache *rspamd_re_cache_ref (struct rspamd_re_cache *cache);
+
+/**
+ * Set limit for all regular expressions in the cache, returns previous limit
+ */
+guint rspamd_re_cache_set_limit (struct rspamd_re_cache *cache, guint limit);
 
 #endif
index eea9057eeab0099110cf3a02463307401c80568e..2f4d7ad892d09a4ecbc2a925163cc8bee8930b89 100644 (file)
@@ -67,10 +67,7 @@ rspamd_task_new (struct rspamd_worker *worker, struct rspamd_config *cfg)
        rspamd_mempool_add_destructor (new_task->task_pool,
                (rspamd_mempool_destruct_t) g_hash_table_unref,
                new_task->results);
-       new_task->re_cache = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
-       rspamd_mempool_add_destructor (new_task->task_pool,
-               (rspamd_mempool_destruct_t) g_hash_table_unref,
-               new_task->re_cache);
+       new_task->re_rt = rspamd_re_cache_runtime_new (cfg->re_cache);
        new_task->raw_headers = g_hash_table_new (rspamd_strcase_hash,
                        rspamd_strcase_equal);
        new_task->request_headers = g_hash_table_new_full (rspamd_ftok_icase_hash,
@@ -226,6 +223,7 @@ rspamd_task_free (struct rspamd_task *task)
                        event_del (&task->timeout_ev);
                }
 
+               rspamd_re_cache_runtime_destroy (task->re_rt);
                REF_RELEASE (task->cfg);
 
                rspamd_mempool_delete (task->task_pool);
index 49357e00b5e31625033582e5555149fa080f580c..21d885b2520f3f39e03da19a9749efb817a11e00 100644 (file)
@@ -29,6 +29,7 @@
 #include "util.h"
 #include "mem_pool.h"
 #include "dns.h"
+#include "re_cache.h"
 
 #include <gmime/gmime.h>
 
@@ -152,7 +153,7 @@ struct rspamd_task {
        InternetAddressList *from_envelope;
 
        GList *messages;                                                                /**< list of messages that would be reported            */
-       GHashTable *re_cache;                                                   /**< cache for matched or not matched regexps           */
+       struct rspamd_re_runtime *re_rt;                                /**< regexp runtime                                                                     */
        struct rspamd_config *cfg;                                              /**< pointer to config object                                           */
        GError *err;
        rspamd_mempool_t *task_pool;                                    /**< memory pool for task                                                       */