commit 0846fb2: [Rework] Unify task_timeout

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Dec 27 18:28:04 UTC 2018


Author: Vsevolod Stakhov
Date: 2018-12-06 15:17:29 +0000
URL: https://github.com/rspamd/rspamd/commit/0846fb2f60229ae713d9253b80876d9cccf36879

[Rework] Unify task_timeout

---
 src/controller.c          | 32 ++++++++++++++++++++++++++++++++
 src/libserver/cfg_file.h  |  3 +--
 src/libserver/cfg_rcl.c   | 24 ++++++++++++------------
 src/libserver/cfg_utils.c |  7 +++++--
 src/rspamd_proxy.c        | 13 +++++++++++++
 src/worker.c              | 18 ++++++++++++------
 6 files changed, 75 insertions(+), 22 deletions(-)

diff --git a/src/controller.c b/src/controller.c
index bed689b61..4050ed13c 100644
--- a/src/controller.c
+++ b/src/controller.c
@@ -23,6 +23,7 @@
 #include "libstat/stat_api.h"
 #include "rspamd.h"
 #include "libserver/worker_util.h"
+#include "worker_private.h"
 #include "lua/lua_common.h"
 #include "cryptobox.h"
 #include "ottery.h"
@@ -182,6 +183,7 @@ struct rspamd_controller_worker_ctx {
 	struct rspamd_rrd_file *rrd;
 	struct event save_stats_event;
 	struct rspamd_lang_detector *lang_det;
+	gdouble task_timeout;
 };
 
 struct rspamd_controller_plugin_cbdata {
@@ -2129,6 +2131,16 @@ rspamd_controller_handle_scan (struct rspamd_http_connection_entry *conn_ent,
 		goto end;
 	}
 
+	if (ctx->task_timeout > 0.0) {
+		struct timeval task_tv;
+
+		event_set (&task->timeout_ev, -1, EV_TIMEOUT, rspamd_task_timeout,
+				task);
+		event_base_set (ctx->ev_base, &task->timeout_ev);
+		double_to_tv (ctx->task_timeout, &task_tv);
+		event_add (&task->timeout_ev, &task_tv);
+	}
+
 end:
 	session->task = task;
 	rspamd_session_pending (task->s);
@@ -3322,6 +3334,7 @@ init_controller_worker (struct rspamd_config *cfg)
 
 	ctx->magic = rspamd_controller_ctx_magic;
 	ctx->timeout = DEFAULT_WORKER_IO_TIMEOUT;
+	ctx->task_timeout = NAN;
 
 	rspamd_rcl_register_worker_option (cfg,
 			type,
@@ -3426,6 +3439,16 @@ init_controller_worker (struct rspamd_config *cfg)
 			0,
 			"Directory where controller saves server's statistics between restarts");
 
+	rspamd_rcl_register_worker_option (cfg,
+			type,
+			"task_timeout",
+			rspamd_rcl_parse_struct_time,
+			ctx,
+			G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx,
+					task_timeout),
+			RSPAMD_CL_FLAG_TIME_FLOAT,
+			"Maximum task processing time, default: 8.0 seconds");
+
 	return ctx;
 }
 
@@ -3704,6 +3727,15 @@ start_controller_worker (struct rspamd_worker *worker)
 				rspamd_strcase_equal, g_free,
 				rspamd_plugin_cbdata_dtor);
 
+	if (isnan (ctx->task_timeout)) {
+		if (isnan (ctx->cfg->task_timeout)) {
+			ctx->task_timeout = 0;
+		}
+		else {
+			ctx->task_timeout = ctx->cfg->task_timeout;
+		}
+	}
+
 	if (ctx->secure_ip != NULL) {
 		rspamd_config_radix_from_ucl (ctx->cfg, ctx->secure_ip,
 				"Allow unauthenticated requests from these addresses",
diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h
index c507bf3a9..edc1258dc 100644
--- a/src/libserver/cfg_file.h
+++ b/src/libserver/cfg_file.h
@@ -318,7 +318,6 @@ struct rspamd_config {
 	gboolean disable_lua_squeeze;                   /**< Disable lua rules squeezing						*/
 	gboolean own_lua_state;                         /**< True if we have created lua_state internally		*/
 
-	gsize max_diff;                                 /**< maximum diff size for text parts					*/
 	gsize max_cores_size;                           /**< maximum size occupied by rspamd core files			*/
 	gsize max_cores_count;                          /**< maximum number of core files						*/
 	gchar *cores_dir;                               /**< directory for core files							*/
@@ -326,6 +325,7 @@ struct rspamd_config {
 	gsize max_pic_size;                             /**< maximum size for a picture to process				*/
 	gsize images_cache_size;                        /**< size of LRU cache for DCT data from images			*/
 	gint default_max_shots;                         /**< default maximum count of symbols hits permitted (-1 for unlimited) */
+	gdouble task_timeout;                           /**< maximum message processing time					*/
 
 	enum rspamd_log_type log_type;                  /**< log type											*/
 	gint log_facility;                              /**< log facility in case of syslog						*/
@@ -336,7 +336,6 @@ struct rspamd_config {
 	guint32 log_buf_size;                           /**< length of log buffer								*/
 	const ucl_object_t *debug_ip_map;               /**< turn on debugging for specified ip addresses       */
 	gboolean log_urls;                              /**< whether we should log URLs                         */
-	GList *debug_symbols;                           /**< symbols to debug									*/
 	GHashTable *debug_modules;                      /**< logging modules to debug							*/
 	struct rspamd_cryptobox_pubkey *log_encryption_key; /**< encryption key for logs						*/
 	guint log_flags;                                /**< logging flags										*/
diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c
index b8f7b9738..b9934929f 100644
--- a/src/libserver/cfg_rcl.c
+++ b/src/libserver/cfg_rcl.c
@@ -1540,12 +1540,6 @@ rspamd_rcl_config_init (struct rspamd_config *cfg, GHashTable *skip_sections)
 				G_STRUCT_OFFSET (struct rspamd_config, debug_ip_map),
 				0,
 				"Enable debugging log for the specified IP addresses");
-		rspamd_rcl_add_default_handler (sub,
-				"debug_symbols",
-				rspamd_rcl_parse_struct_string_list,
-				G_STRUCT_OFFSET (struct rspamd_config, debug_symbols),
-				0,
-				"Enable debug for the specified symbols");
 		rspamd_rcl_add_default_handler (sub,
 				"debug_modules",
 				rspamd_rcl_parse_struct_string_list,
@@ -1738,12 +1732,6 @@ rspamd_rcl_config_init (struct rspamd_config *cfg, GHashTable *skip_sections)
 				G_STRUCT_OFFSET (struct rspamd_config, filters),
 				0,
 				"List of internal filters enabled");
-		rspamd_rcl_add_default_handler (sub,
-				"max_diff",
-				rspamd_rcl_parse_struct_integer,
-				G_STRUCT_OFFSET (struct rspamd_config, max_diff),
-				RSPAMD_CL_FLAG_INT_SIZE,
-				"Legacy option, do not use");
 		rspamd_rcl_add_default_handler (sub,
 				"map_watch_interval",
 				rspamd_rcl_parse_struct_time,
@@ -1990,6 +1978,18 @@ rspamd_rcl_config_init (struct rspamd_config *cfg, GHashTable *skip_sections)
 				G_STRUCT_OFFSET (struct rspamd_config, max_sessions_cache),
 				0,
 				"Maximum number of sessions in cache before warning (default: 100)");
+		rspamd_rcl_add_default_handler (sub,
+				"task_timeout",
+				rspamd_rcl_parse_struct_time,
+				G_STRUCT_OFFSET (struct rspamd_config, task_timeout),
+				RSPAMD_CL_FLAG_TIME_FLOAT,
+				"Maximum time for checking a message");
+		rspamd_rcl_add_default_handler (sub,
+				"check_timeout",
+				rspamd_rcl_parse_struct_time,
+				G_STRUCT_OFFSET (struct rspamd_config, task_timeout),
+				RSPAMD_CL_FLAG_TIME_FLOAT,
+				"Maximum time for checking a message (alias for task_timeout)");
 
 		/* Neighbours configuration */
 		rspamd_rcl_add_section_doc (&sub->subsections, "neighbours", "name",
diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c
index ec2e48786..2125a2854 100644
--- a/src/libserver/cfg_utils.c
+++ b/src/libserver/cfg_utils.c
@@ -47,6 +47,8 @@
 #define DEFAULT_MAX_SHOTS 100
 #define DEFAULT_MAX_SESSIONS 100
 #define DEFAULT_MAX_WORKERS 4
+/* Timeout for task processing */
+#define DEFAULT_TASK_TIMEOUT 8.0
 
 struct rspamd_ucl_map_cbdata {
 	struct rspamd_config *cfg;
@@ -131,8 +133,9 @@ rspamd_config_new (enum rspamd_config_init_flags flags)
 	/* 16 sockets per DNS server */
 	cfg->dns_io_per_server = 16;
 
-	/* 20 Kb */
-	cfg->max_diff = 20480;
+	/* Disable timeout */
+	cfg->task_timeout = DEFAULT_TASK_TIMEOUT;
+
 
 	rspamd_config_init_metric (cfg);
 	cfg->composite_symbols =
diff --git a/src/rspamd_proxy.c b/src/rspamd_proxy.c
index aed642752..45af47cbb 100644
--- a/src/rspamd_proxy.c
+++ b/src/rspamd_proxy.c
@@ -1749,6 +1749,19 @@ rspamd_proxy_self_scan (struct rspamd_proxy_session *session)
 		event_add (&task->timeout_ev, &task_tv);
 	}
 
+	if (session->ctx->has_self_scan) {
+		if (!isnan (session->ctx->cfg->task_timeout) &&
+				session->ctx->cfg->task_timeout > 0) {
+			struct timeval task_tv;
+
+			event_set (&task->timeout_ev, -1, EV_TIMEOUT, rspamd_task_timeout,
+					task);
+			event_base_set (session->ctx->ev_base, &task->timeout_ev);
+			double_to_tv (session->ctx->default_upstream->timeout, &task_tv);
+			event_add (&task->timeout_ev, &task_tv);
+		}
+	}
+
 	session->master_conn->task = task;
 	rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL);
 
diff --git a/src/worker.c b/src/worker.c
index 9778cee08..81c59a786 100644
--- a/src/worker.c
+++ b/src/worker.c
@@ -35,14 +35,13 @@
 #include "utlist.h"
 #include "libutil/http_private.h"
 #include "libmime/lang_detection.h"
+#include <math.h>
 #include "unix-std.h"
 
 #include "lua/lua_common.h"
 
 /* 60 seconds for worker's IO */
 #define DEFAULT_WORKER_IO_TIMEOUT 60000
-/* Timeout for task processing */
-#define DEFAULT_TASK_TIMEOUT 8.0
 
 gpointer init_worker (struct rspamd_config *cfg);
 void start_worker (struct rspamd_worker *worker);
@@ -538,7 +537,7 @@ init_worker (struct rspamd_config *cfg)
 	ctx->is_mime = TRUE;
 	ctx->timeout = DEFAULT_WORKER_IO_TIMEOUT;
 	ctx->cfg = cfg;
-	ctx->task_timeout = DEFAULT_TASK_TIMEOUT;
+	ctx->task_timeout = NAN;
 
 	rspamd_rcl_register_worker_option (cfg,
 			type,
@@ -577,9 +576,7 @@ init_worker (struct rspamd_config *cfg)
 			G_STRUCT_OFFSET (struct rspamd_worker_ctx,
 						task_timeout),
 			RSPAMD_CL_FLAG_TIME_FLOAT,
-			"Maximum task processing time, default: "
-					G_STRINGIFY(DEFAULT_TASK_TIMEOUT)
-					" seconds");
+			"Maximum task processing time, default: 8.0 seconds");
 
 	rspamd_rcl_register_worker_option (cfg,
 			type,
@@ -658,6 +655,15 @@ start_worker (struct rspamd_worker *worker)
 	rspamd_symcache_start_refresh (worker->srv->cfg->cache, ctx->ev_base,
 			worker);
 
+	if (isnan (ctx->task_timeout)) {
+		if (isnan (ctx->cfg->task_timeout)) {
+			ctx->task_timeout = 0;
+		}
+		else {
+			ctx->task_timeout = ctx->cfg->task_timeout;
+		}
+	}
+
 	ctx->resolver = dns_resolver_init (worker->srv->logger,
 			ctx->ev_base,
 			worker->srv->cfg);


More information about the Commits mailing list