]> source.dussan.org Git - rspamd.git/commitdiff
Allow tuning for maximum compile time for hyperscan
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 10 Dec 2015 09:14:58 +0000 (09:14 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 10 Dec 2015 09:14:58 +0000 (09:14 +0000)
src/hs_helper.c
src/libserver/re_cache.c
src/libserver/re_cache.h

index 4d89bedf80ae947cba919470b1e73010962d1f95..54517302c22b5dc9116321adcdf98cf5cc3c60cb 100644 (file)
@@ -50,11 +50,14 @@ worker_t hs_helper_worker = {
                SOCK_STREAM                 /* TCP socket */
 };
 
+const static gdouble default_max_time = 1.0;
+
 /*
  * Worker's context
  */
 struct hs_helper_ctx {
        gchar *hs_dir;
+       gdouble max_time;
        struct rspamd_config *cfg;
        struct event_base *ev_base;
 };
@@ -70,10 +73,15 @@ init_hs_helper (struct rspamd_config *cfg)
 
        ctx->cfg = cfg;
        ctx->hs_dir = RSPAMD_DBDIR "/";
+       ctx->max_time = default_max_time;
 
        rspamd_rcl_register_worker_option (cfg, type, "cache_dir",
                        rspamd_rcl_parse_struct_string, ctx,
                        G_STRUCT_OFFSET (struct hs_helper_ctx, hs_dir), 0);
+       rspamd_rcl_register_worker_option (cfg, type, "max_time",
+                       rspamd_rcl_parse_struct_time, ctx,
+                       G_STRUCT_OFFSET (struct hs_helper_ctx, max_time),
+                       RSPAMD_CL_FLAG_TIME_FLOAT);
 
        return ctx;
 }
@@ -138,7 +146,7 @@ rspamd_rs_compile (struct hs_helper_ctx *ctx, struct rspamd_worker *worker)
        }
 
        if ((ncompiled = rspamd_re_cache_compile_hyperscan (ctx->cfg->re_cache,
-                       ctx->hs_dir,
+                       ctx->hs_dir, ctx->max_time,
                        &err)) == -1) {
                msg_err ("failed to compile re cache: %e", err);
                g_error_free (err);
index 5b6eb67f6221aefc7f57df863270ba420be19fe3..c2ca68788200e970d4dd4e28ec76a88405acec6b 100644 (file)
@@ -882,17 +882,18 @@ rspamd_re_cache_type_from_string (const char *str)
 #ifdef WITH_HYPERSCAN
 static gboolean
 rspamd_re_cache_is_finite (struct rspamd_re_cache *cache,
-               rspamd_regexp_t *re, gint flags)
+               rspamd_regexp_t *re, gint flags, gdouble max_time)
 {
        pid_t cld;
        gint status;
        struct timespec ts;
        hs_compile_error_t *hs_errors;
        hs_database_t *test_db;
-       const double wait_time = 0.1;
+       gdouble wait_time;
        const gint max_tries = 10;
        gint tries = 0, rc;
 
+       wait_time = max_time / max_tries;
        /* We need to restore SIGCHLD processing */
        signal (SIGCHLD, SIG_DFL);
        cld = fork ();
@@ -951,7 +952,7 @@ rspamd_re_cache_is_finite (struct rspamd_re_cache *cache,
 
 gint
 rspamd_re_cache_compile_hyperscan (struct rspamd_re_cache *cache,
-               const char *cache_dir,
+               const char *cache_dir, gdouble max_time,
                GError **err)
 {
        g_assert (cache != NULL);
@@ -1039,14 +1040,14 @@ rspamd_re_cache_compile_hyperscan (struct rspamd_re_cache *cache,
                                        &cache->plt,
                                        &test_db,
                                        &hs_errors) != HS_SUCCESS) {
-                               msg_info_re_cache ("cannot compile %s to hyperscan, try prefilter match",
+                               msg_debug_re_cache ("cannot compile %s to hyperscan, try prefilter match",
                                                rspamd_regexp_get_pattern (re));
                                hs_free_compile_error (hs_errors);
 
                                /* The approximation operation might take a significant
                                 * amount of time, so we need to check if it's finite
                                 */
-                               if (rspamd_re_cache_is_finite (cache, re, hs_flags[i])) {
+                               if (rspamd_re_cache_is_finite (cache, re, hs_flags[i], max_time)) {
                                        hs_flags[i] |= HS_FLAG_PREFILTER;
                                        hs_ids[i] = rspamd_regexp_get_cache_id (re);
                                        hs_pats[i] = rspamd_regexp_get_pattern (re);
index 1f0ab7c9b5c7302c5b733935005501937e3cb1a7..429d16e0a3bc6b7f19e358db49b2b72760f6414e 100644 (file)
@@ -129,7 +129,7 @@ enum rspamd_re_type rspamd_re_cache_type_from_string (const char *str);
  * Compile expressions to the hyperscan tree and store in the `cache_dir`
  */
 gint rspamd_re_cache_compile_hyperscan (struct rspamd_re_cache *cache,
-               const char *cache_dir,
+               const char *cache_dir, gdouble max_time,
                GError **err);