commit 92e51ab: [Minor] Deduplicate code

Vsevolod Stakhov vsevolod at rspamd.com
Sat Nov 26 13:49:03 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-11-26 13:42:19 +0000
URL: https://github.com/rspamd/rspamd/commit/92e51ab7a970cbd2f7e69c24dfd8c76e2a3204aa

[Minor] Deduplicate code

---
 src/libserver/worker_util.c | 64 +++++++++++++++++++++++++++++++++++++++++++++
 src/libserver/worker_util.h |  5 ++++
 src/rspamd_proxy.c          | 35 +------------------------
 src/worker.c                | 29 +-------------------
 4 files changed, 71 insertions(+), 62 deletions(-)

diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c
index 3361377f4..3c90b8fb1 100644
--- a/src/libserver/worker_util.c
+++ b/src/libserver/worker_util.c
@@ -1406,6 +1406,70 @@ rspamd_worker_is_primary_controller (struct rspamd_worker *w)
 	return FALSE;
 }
 
+gboolean
+rspamd_worker_check_controller_presence (struct rspamd_worker *w)
+{
+	if (w->index == 0) {
+		GQuark our_type = w->type;
+		gboolean controller_seen = FALSE;
+		GList *cur;
+
+		enum {
+			low_priority_worker,
+			high_priority_worker
+		} our_priority;
+
+		if (our_type == g_quark_from_static_string("rspamd_proxy")) {
+			our_priority = low_priority_worker;
+		}
+		else if (our_type == g_quark_from_static_string("normal")) {
+			our_priority = high_priority_worker;
+		}
+		else {
+			msg_err ("function is called for a wrong worker type: %s", g_quark_to_string(our_type));
+			return FALSE;
+		}
+
+		cur = w->srv->cfg->workers;
+
+		while (cur) {
+			struct rspamd_worker_conf *cf;
+
+			cf = (struct rspamd_worker_conf *)cur->data;
+
+			if (our_priority == low_priority_worker) {
+				if ((cf->type == g_quark_from_static_string("controller")) ||
+					(cf->type == g_quark_from_static_string("normal"))) {
+
+					if (cf->enabled && cf->count >= 0) {
+						controller_seen = TRUE;
+						break;
+					}
+				}
+			}
+			else {
+				if (cf->type == g_quark_from_static_string("controller")) {
+					if (cf->enabled && cf->count >= 0) {
+						controller_seen = TRUE;
+						break;
+					}
+				}
+			}
+
+			cur = g_list_next (cur);
+		}
+
+		if (!controller_seen) {
+			msg_info ("no controller or normal workers defined, execute "
+					  "controller periodics in this worker");
+			w->flags |= RSPAMD_WORKER_CONTROLLER;
+			return TRUE;
+		}
+	}
+
+	return FALSE;
+}
+
 struct rspamd_worker_session_elt {
 	void *ptr;
 	guint *pref;
diff --git a/src/libserver/worker_util.h b/src/libserver/worker_util.h
index 38ba9c0d7..bf379421a 100644
--- a/src/libserver/worker_util.h
+++ b/src/libserver/worker_util.h
@@ -191,6 +191,11 @@ gdouble rspamd_worker_check_and_adjust_timeout (struct rspamd_config *cfg,
  */
 gboolean rspamd_worker_is_primary_controller (struct rspamd_worker *w);
 
+/**
+ * Returns TRUE if a specific worker should take a role of a controller
+ */
+gboolean rspamd_worker_check_controller_presence (struct rspamd_worker *w);
+
 /**
  * Creates new session cache
  * @param w
diff --git a/src/rspamd_proxy.c b/src/rspamd_proxy.c
index 80c28b975..fe0970c1e 100644
--- a/src/rspamd_proxy.c
+++ b/src/rspamd_proxy.c
@@ -2375,42 +2375,9 @@ start_rspamd_proxy (struct rspamd_worker *worker)
 		/* Additional initialisation needed */
 		rspamd_worker_init_scanner (worker, ctx->event_loop, ctx->resolver,
 				&ctx->lang_det);
-		/* Always yse cfg->task_timeout */
 		ctx->task_timeout = rspamd_worker_check_and_adjust_timeout(ctx->cfg, NAN);
 
-		if (worker->index == 0) {
-			/*
-			 * If there are no controllers and no normal workers,
-			 * then pretend that we are a controller
-			 */
-			gboolean controller_seen = FALSE;
-			GList *cur;
-
-			cur = worker->srv->cfg->workers;
-
-			while (cur) {
-				struct rspamd_worker_conf *cf;
-
-				cf = (struct rspamd_worker_conf *)cur->data;
-				if ((cf->type == g_quark_from_static_string ("controller")) ||
-						(cf->type == g_quark_from_static_string ("normal"))) {
-
-					if (cf->enabled && cf->count >= 0) {
-						controller_seen = TRUE;
-						break;
-					}
-				}
-
-				cur = g_list_next (cur);
-			}
-
-			if (!controller_seen) {
-				msg_info ("no controller or normal workers defined, execute "
-							  "controller periodics in this worker");
-				worker->flags |= RSPAMD_WORKER_CONTROLLER;
-				is_controller = TRUE;
-			}
-		}
+		is_controller = rspamd_worker_check_controller_presence (worker);
 	}
 	else {
 		worker->flags &= ~RSPAMD_WORKER_SCANNER;
diff --git a/src/worker.c b/src/worker.c
index 10f7c076c..ac8f87af5 100644
--- a/src/worker.c
+++ b/src/worker.c
@@ -509,34 +509,7 @@ start_worker (struct rspamd_worker *worker)
 	rspamd_worker_init_scanner (worker, ctx->event_loop, ctx->resolver,
 			&ctx->lang_det);
 
-	if (worker->index == 0) {
-		/* If there are no controllers, then pretend that we are a controller */
-		gboolean controller_seen = FALSE;
-		GList *cur;
-
-		cur = worker->srv->cfg->workers;
-
-		while (cur) {
-			struct rspamd_worker_conf *cf;
-
-			cf = (struct rspamd_worker_conf *)cur->data;
-			if (cf->type == g_quark_from_static_string ("controller")) {
-				if (cf->enabled && cf->count >= 0) {
-					controller_seen = TRUE;
-					break;
-				}
-			}
-
-			cur = g_list_next (cur);
-		}
-
-		if (!controller_seen) {
-			msg_info_ctx ("no controller workers defined, execute "
-				 "controller periodics in this worker");
-			worker->flags |= RSPAMD_WORKER_CONTROLLER;
-			is_controller = TRUE;
-		}
-	}
+	is_controller = rspamd_worker_check_controller_presence (worker);
 
 	if (is_controller) {
 		rspamd_worker_init_controller (worker, NULL);


More information about the Commits mailing list