]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Deduplicate code
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 26 Nov 2022 13:42:19 +0000 (13:42 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 26 Nov 2022 13:42:19 +0000 (13:42 +0000)
src/libserver/worker_util.c
src/libserver/worker_util.h
src/rspamd_proxy.c
src/worker.c

index 3361377f418de8131d2e1a645a5f00d33115f0f3..3c90b8fb15b632f15c0a077085f897c8145908d1 100644 (file)
@@ -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;
index 38ba9c0d765fa7ca5cf7e1df268bf839c399376e..bf379421a83e42b3915d24d929779ea84df10fb9 100644 (file)
@@ -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
index 80c28b975d66b4eb9a26ab7219cfc6bc4504c381..fe0970c1eecb04107757f4fa86ce2c7f34b95802 100644 (file)
@@ -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;
index 10f7c076c0b130a241dadb93c8831877bf30a8cf..ac8f87af53a28bbc6eb8e10c65cc9830cd1d71cb 100644 (file)
@@ -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);