]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Allow to skip some initialization phases to speed up rspamadm
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 24 Jun 2016 15:47:22 +0000 (16:47 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 24 Jun 2016 15:47:22 +0000 (16:47 +0100)
src/libserver/cfg_file.h
src/libserver/cfg_utils.c
src/lua/lua_util.c
src/rspamadm/configdump.c
src/rspamadm/configtest.c
src/rspamadm/rspamadm.c
src/rspamd.c

index 93470c0f303891dc53d76d2929f8d8183df2a890..eaaafc318748510507a4b6e22320567eeafa3a93 100644 (file)
@@ -453,12 +453,21 @@ const ucl_object_t * rspamd_config_get_module_opt (struct rspamd_config *cfg,
  */
 gchar rspamd_config_parse_flag (const gchar *str, guint len);
 
+enum rspamd_post_load_options {
+       RSPAMD_CONFIG_INIT_URL = 1 << 0,
+       RSPAMD_CONFIG_INIT_LIBS = 1 << 1,
+       RSPAMD_CONFIG_INIT_SYMCACHE = 1 << 2,
+       RSPAMD_CONFIG_INIT_VALIDATE = 1 << 3
+};
+
+#define RSPAMD_CONFIG_LOAD_ALL (RSPAMD_CONFIG_INIT_URL|RSPAMD_CONFIG_INIT_LIBS|RSPAMD_CONFIG_INIT_SYMCACHE|RSPAMD_CONFIG_INIT_VALIDATE)
+
 /**
  * Do post load actions for config
  * @param cfg config file
  */
 gboolean rspamd_config_post_load (struct rspamd_config *cfg,
-               gboolean validate_cache);
+               enum rspamd_post_load_options opts);
 
 /**
  * Calculate checksum for config file
index 8f8fb551c8aea3fdaad0f2af19811c7037c201ae..bbbb062bb29c7435d732d30534300b077a4fa60a 100644 (file)
@@ -611,7 +611,8 @@ rspamd_config_parse_log_format (struct rspamd_config *cfg)
  * Perform post load actions
  */
 gboolean
-rspamd_config_post_load (struct rspamd_config *cfg, gboolean validate_cache)
+rspamd_config_post_load (struct rspamd_config *cfg,
+               enum rspamd_post_load_options opts)
 {
 #ifdef HAVE_CLOCK_GETTIME
        struct timespec ts;
@@ -651,46 +652,48 @@ rspamd_config_post_load (struct rspamd_config *cfg, gboolean validate_cache)
                def_metric->actions[METRIC_ACTION_REJECT].score = DEFAULT_SCORE;
        }
 
-       if (cfg->tld_file == NULL) {
-               /* Try to guess tld file */
-               GString *fpath = g_string_new (NULL);
+       if (opts & RSPAMD_CONFIG_INIT_URL) {
+               if (cfg->tld_file == NULL) {
+                       /* Try to guess tld file */
+                       GString *fpath = g_string_new (NULL);
 
-               rspamd_printf_gstring (fpath, "%s%c%s", RSPAMD_PLUGINSDIR,
-                               G_DIR_SEPARATOR, "effective_tld_names.dat");
+                       rspamd_printf_gstring (fpath, "%s%c%s", RSPAMD_PLUGINSDIR,
+                                       G_DIR_SEPARATOR, "effective_tld_names.dat");
 
-               if (access (fpath->str, R_OK) != -1) {
-                       msg_debug_config ("url_tld option is not specified but %s is available,"
-                                       " therefore this file is assumed as TLD file for URL"
-                                       " extraction", fpath->str);
-                       cfg->tld_file = rspamd_mempool_strdup (cfg->cfg_pool, fpath->str);
+                       if (access (fpath->str, R_OK) != -1) {
+                               msg_debug_config ("url_tld option is not specified but %s is available,"
+                                               " therefore this file is assumed as TLD file for URL"
+                                               " extraction", fpath->str);
+                               cfg->tld_file = rspamd_mempool_strdup (cfg->cfg_pool, fpath->str);
+                       }
+                       else {
+                               if (opts & RSPAMD_CONFIG_INIT_VALIDATE) {
+                                       msg_err_config ("no url_tld option has been specified");
+                                       ret = FALSE;
+                               }
+                       }
+
+                       g_string_free (fpath, TRUE);
                }
                else {
-                       if (validate_cache) {
-                               msg_err_config ("no url_tld option has been specified");
-                               ret = FALSE;
+                       if (access (cfg->tld_file, R_OK) == -1) {
+                               if (opts & RSPAMD_CONFIG_INIT_VALIDATE) {
+                                       ret = FALSE;
+                                       msg_err_config ("cannot access tld file %s: %s", cfg->tld_file,
+                                                       strerror (errno));
+                               }
+                               else {
+                                       msg_debug_config ("cannot access tld file %s: %s", cfg->tld_file,
+                                                       strerror (errno));
+                                       cfg->tld_file = NULL;
+                               }
                        }
                }
 
-               g_string_free (fpath, TRUE);
-       }
-       else {
-               if (access (cfg->tld_file, R_OK) == -1) {
-                       if (validate_cache) {
-                               ret = FALSE;
-                               msg_err_config ("cannot access tld file %s: %s", cfg->tld_file,
-                                               strerror (errno));
-                       }
-                       else {
-                               msg_debug_config ("cannot access tld file %s: %s", cfg->tld_file,
-                                               strerror (errno));
-                               cfg->tld_file = NULL;
-                       }
-               }
+               rspamd_url_init (cfg->tld_file);
        }
 
        init_dynamic_config (cfg);
-       rspamd_url_init (cfg->tld_file);
-
        /* Insert classifiers symbols */
        rspamd_config_insert_classify_symbols (cfg);
 
@@ -699,14 +702,18 @@ rspamd_config_post_load (struct rspamd_config *cfg, gboolean validate_cache)
                msg_err_config ("cannot parse log format, task logging will not be available");
        }
 
-       /* Init config cache */
-       rspamd_symbols_cache_init (cfg->cache);
+       if (opts & RSPAMD_CONFIG_INIT_SYMCACHE) {
+               /* Init config cache */
+               rspamd_symbols_cache_init (cfg->cache);
 
-       /* Init re cache */
-       rspamd_re_cache_init (cfg->re_cache, cfg);
+               /* Init re cache */
+               rspamd_re_cache_init (cfg->re_cache, cfg);
+       }
 
-       /* Config other libraries */
-       rspamd_config_libs (cfg->libs_ctx, cfg);
+       if (opts & RSPAMD_CONFIG_INIT_LIBS) {
+               /* Config other libraries */
+               rspamd_config_libs (cfg->libs_ctx, cfg);
+       }
 
        /* Execute post load scripts */
        LL_FOREACH (cfg->on_load, sc) {
@@ -725,7 +732,7 @@ rspamd_config_post_load (struct rspamd_config *cfg, gboolean validate_cache)
        }
 
        /* Validate cache */
-       if (validate_cache) {
+       if (opts & RSPAMD_CONFIG_INIT_VALIDATE) {
                return rspamd_symbols_cache_validate (cfg->cache, cfg, FALSE) && ret;
        }
 
index 272e39463ae11f8ed2a2bbf1f9e2d55cfd3a4882..e50e704ee95def5474ba4932232cc1df92e98917 100644 (file)
@@ -399,7 +399,7 @@ lua_util_load_rspamd_config (lua_State *L)
                        lua_pushnil (L);
                }
                else {
-                       rspamd_config_post_load (cfg, FALSE);
+                       rspamd_config_post_load (cfg, 0);
                        pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *));
                        rspamd_lua_setclass (L, "rspamd{config}", -1);
                        *pcfg = cfg;
@@ -433,7 +433,7 @@ lua_util_config_from_ucl (lua_State *L)
                        lua_pushnil (L);
                }
                else {
-                       rspamd_config_post_load (cfg, FALSE);
+                       rspamd_config_post_load (cfg, 0);
                        pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *));
                        rspamd_lua_setclass (L, "rspamd{config}", -1);
                        *pcfg = cfg;
index e7d51b442f66eb7b47c1b37194ac7a5042b7c9d7..1cc85676c0f18f91a0ef49bcd848a1ee6b1a333e 100644 (file)
@@ -290,7 +290,7 @@ rspamadm_configdump (gint argc, gchar **argv)
                }
 
                if (ret) {
-                       ret = rspamd_config_post_load (cfg, FALSE);
+                       ret = rspamd_config_post_load (cfg, RSPAMD_CONFIG_INIT_SYMCACHE);
                }
        }
 
index 7ba9af3fef60c9d9df78d961b79435954f0db222..967bca2baa56992de8b9401bd5eac4f549783f2c 100644 (file)
@@ -150,7 +150,7 @@ rspamadm_configtest (gint argc, gchar **argv)
                }
 
                if (ret) {
-                       ret = rspamd_config_post_load (cfg, FALSE);
+                       ret = rspamd_config_post_load (cfg, RSPAMD_CONFIG_INIT_SYMCACHE);
                }
 
                if (!rspamd_symbols_cache_validate (rspamd_main->cfg->cache,
index 523011a644196389041367609ec6b93396e607da..38d5e77ebe05193db4d74dec23b054ea1d662e03 100644 (file)
@@ -285,7 +285,7 @@ main (gint argc, gchar **argv, gchar **env)
        (void) rspamd_log_open (rspamd_main->logger);
        g_log_set_default_handler (rspamd_glib_log_function, rspamd_main->logger);
        g_set_printerr_handler (rspamd_glib_printerr_function);
-       rspamd_config_post_load (cfg, FALSE);
+       rspamd_config_post_load (cfg, RSPAMD_CONFIG_INIT_LIBS);
 
        /* Setup logger */
        if (verbose) {
index eaf6c6d30952c55d4e20f32b4ad02d16eec1d260..cb8eb18063ec3278adc6fc3b0cae9925e5a6ba83 100644 (file)
@@ -68,7 +68,7 @@
 static gboolean load_rspamd_config (struct rspamd_main *rspamd_main,
                struct rspamd_config *cfg,
                gboolean init_modules,
-               gboolean validate);
+               enum rspamd_post_load_options opts);
 
 /* Control socket */
 static gint control_fd;
@@ -280,7 +280,7 @@ reread_config (struct rspamd_main *rspamd_main)
        /* Save some variables */
        tmp_cfg->cfg_name = cfg_file;
 
-       if (!load_rspamd_config (rspamd_main, tmp_cfg, FALSE, TRUE)) {
+       if (!load_rspamd_config (rspamd_main, tmp_cfg, FALSE, RSPAMD_CONFIG_INIT_VALIDATE)) {
                rspamd_set_logger (rspamd_main->cfg, g_quark_try_string (
                                "main"), rspamd_main);
                msg_err_main ("cannot parse new config file, revert to old one");
@@ -749,7 +749,8 @@ reopen_log_handler (gpointer key, gpointer value, gpointer unused)
 
 static gboolean
 load_rspamd_config (struct rspamd_main *rspamd_main,
-               struct rspamd_config *cfg, gboolean init_modules, gboolean validate)
+               struct rspamd_config *cfg, gboolean init_modules,
+               enum rspamd_post_load_options opts)
 {
        cfg->compiled_modules = modules;
        cfg->compiled_workers = workers;
@@ -782,7 +783,7 @@ load_rspamd_config (struct rspamd_main *rspamd_main,
        }
 
        /* Do post-load actions */
-       rspamd_config_post_load (cfg, validate);
+       rspamd_config_post_load (cfg, opts);
 
        return TRUE;
 }
@@ -1120,7 +1121,7 @@ main (gint argc, gchar **argv, gchar **env)
        }
 
        if (config_test || dump_cache) {
-               if (!load_rspamd_config (rspamd_main, rspamd_main->cfg, FALSE, FALSE)) {
+               if (!load_rspamd_config (rspamd_main, rspamd_main->cfg, FALSE, 0)) {
                        exit (EXIT_FAILURE);
                }
 
@@ -1146,7 +1147,8 @@ main (gint argc, gchar **argv, gchar **env)
        }
 
        /* Load config */
-       if (!load_rspamd_config (rspamd_main, rspamd_main->cfg, TRUE, TRUE)) {
+       if (!load_rspamd_config (rspamd_main, rspamd_main->cfg, TRUE,
+                       RSPAMD_CONFIG_LOAD_ALL)) {
                exit (EXIT_FAILURE);
        }