]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Rework] Change time handling
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 29 Oct 2018 13:19:14 +0000 (13:19 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 29 Oct 2018 13:19:14 +0000 (13:19 +0000)
src/controller.c
src/libserver/task.c
src/libserver/task.h
src/lua/lua_task.c
src/lua/lua_util.c
src/plugins/fuzzy_check.c
src/rspamadm/lua_repl.c
src/rspamd_proxy.c
src/worker.c

index 26d12e14b7a39535b317b0be7a362f709ccf028a..6875e1f3ef49660f5758f3ae6cbf6241b6b0cdb0 100644 (file)
@@ -1522,10 +1522,9 @@ rspamd_controller_handle_lua_history (lua_State *L,
 
                        if (lua_isfunction (L, -1)) {
                                task = rspamd_task_new (session->ctx->worker, session->cfg,
-                                               session->pool, ctx->lang_det);
+                                               session->pool, ctx->lang_det, ctx->ev_base);
 
                                task->resolver = ctx->resolver;
-                               task->ev_base = ctx->ev_base;
                                task->s = rspamd_session_create (session->pool,
                                                rspamd_controller_history_lua_fin_task,
                                                NULL,
@@ -1823,11 +1822,9 @@ rspamd_controller_handle_lua (struct rspamd_http_connection_entry *conn_ent,
        }
 
        task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool,
-                       ctx->lang_det);
+                       ctx->lang_det, ctx->ev_base);
 
        task->resolver = ctx->resolver;
-       task->ev_base = ctx->ev_base;
-
        task->s = rspamd_session_create (session->pool,
                        rspamd_controller_lua_fin_task,
                        NULL,
@@ -2008,12 +2005,9 @@ rspamd_controller_handle_learn_common (
        }
 
        task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool,
-                       session->ctx->lang_det);
+                       session->ctx->lang_det, ctx->ev_base);
 
        task->resolver = ctx->resolver;
-       task->ev_base = ctx->ev_base;
-
-
        task->s = rspamd_session_create (session->pool,
                        rspamd_controller_learn_fin_task,
                        NULL,
@@ -2109,12 +2103,9 @@ rspamd_controller_handle_scan (struct rspamd_http_connection_entry *conn_ent,
        }
 
        task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool,
-                       ctx->lang_det);
-       task->ev_base = session->ctx->ev_base;
+                       ctx->lang_det, ctx->ev_base);
 
        task->resolver = ctx->resolver;
-       task->ev_base = ctx->ev_base;
-
        task->s = rspamd_session_create (session->pool,
                        rspamd_controller_check_fin_task,
                        NULL,
@@ -2600,9 +2591,8 @@ rspamd_controller_handle_stat_common (
        ctx = session->ctx;
 
        task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool,
-                       ctx->lang_det);
+                       ctx->lang_det, ctx->ev_base);
        task->resolver = ctx->resolver;
-       task->ev_base = ctx->ev_base;
        cbdata = rspamd_mempool_alloc0 (session->pool, sizeof (*cbdata));
        cbdata->conn_ent = conn_ent;
        cbdata->task = task;
@@ -2963,11 +2953,9 @@ rspamd_controller_handle_lua_plugin (struct rspamd_http_connection_entry *conn_e
        }
 
        task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool,
-                       ctx->lang_det);
+                       ctx->lang_det, ctx->ev_base);
 
        task->resolver = ctx->resolver;
-       task->ev_base = ctx->ev_base;
-
        task->s = rspamd_session_create (session->pool,
                        rspamd_controller_lua_fin_task,
                        NULL,
index 061feadedb4670686f1272959207e26fc3cc4d8e..de2745701939aa984d79899ae9161f5370cc3563 100644 (file)
@@ -62,8 +62,9 @@ rspamd_request_header_dtor (gpointer p)
  */
 struct rspamd_task *
 rspamd_task_new (struct rspamd_worker *worker, struct rspamd_config *cfg,
-               rspamd_mempool_t *pool,
-               struct rspamd_lang_detector *lang_det)
+                                rspamd_mempool_t *pool,
+                                struct rspamd_lang_detector *lang_det,
+                                struct event_base *ev_base)
 {
        struct rspamd_task *new_task;
 
@@ -89,8 +90,23 @@ rspamd_task_new (struct rspamd_worker *worker, struct rspamd_config *cfg,
                }
        }
 
+       new_task->ev_base = ev_base;
+
+#ifdef HAVE_EVENT_NO_CACHE_TIME_FUNC
+       if (ev_base) {
+               event_base_update_cache_time (ev_base);
+               event_base_gettimeofday_cached (ev_base, &new_task->tv);
+               new_task->time_real = tv_to_double (&new_task->tv);
+       }
+       else {
+               gettimeofday (&new_task->tv, NULL);
+               new_task->time_real = tv_to_double (&new_task->tv);
+       }
+#else
        gettimeofday (&new_task->tv, NULL);
-       new_task->time_real = rspamd_get_ticks (FALSE);
+       new_task->time_real = tv_to_double (&new_task->tv);
+#endif
+
        new_task->time_virtual = rspamd_get_virtual_ticks ();
        new_task->time_real_finish = NAN;
        new_task->time_virtual_finish = NAN;
@@ -1645,8 +1661,24 @@ rspamd_task_profile_get (struct rspamd_task *task, const gchar *key)
 gboolean
 rspamd_task_set_finish_time (struct rspamd_task *task)
 {
+       struct timeval tv;
+
        if (isnan (task->time_real_finish)) {
-               task->time_real_finish = rspamd_get_ticks (FALSE);
+
+#ifdef HAVE_EVENT_NO_CACHE_TIME_FUNC
+               if (task->ev_base) {
+                       event_base_update_cache_time (task->ev_base);
+                       event_base_gettimeofday_cached (task->ev_base, &tv);
+                       task->time_real_finish = tv_to_double (&tv);
+               }
+               else {
+                       gettimeofday (&tv, NULL);
+                       task->time_real_finish = tv_to_double (&tv);
+               }
+#else
+               gettimeofday (&tv, NULL);
+               task->time_real_finish = tv_to_double (&tv);
+#endif
                task->time_virtual_finish = rspamd_get_virtual_ticks ();
 
                return TRUE;
index c1eec96edff9093865774115f31caf661763fb23..005f6af26787cb475ab4f9852d081234be02e8fd 100644 (file)
@@ -212,9 +212,10 @@ struct rspamd_task {
  * Construct new task for worker
  */
 struct rspamd_task *rspamd_task_new (struct rspamd_worker *worker,
-               struct rspamd_config *cfg,
-               rspamd_mempool_t *pool,
-               struct rspamd_lang_detector *lang_det);
+                                                                        struct rspamd_config *cfg,
+                                                                        rspamd_mempool_t *pool,
+                                                                        struct rspamd_lang_detector *lang_det,
+                                                                        struct event_base *ev_base);
 /**
  * Destroy task object and remove its IO dispatcher if it exists
  */
index 15af4cf5a35ae6fc5a5b90e268a79336ca0b4d42..7aa4f3f1844c5a1f12ae736183badd75ebaab820 100644 (file)
@@ -1303,7 +1303,7 @@ lua_task_load_from_file (lua_State * L)
                        err = strerror (errno);
                }
                else {
-                       task = rspamd_task_new (NULL, cfg, NULL, NULL);
+                       task = rspamd_task_new (NULL, cfg, NULL, NULL, NULL);
                        task->msg.begin = map;
                        task->msg.len = sz;
                        rspamd_mempool_add_destructor (task->task_pool,
@@ -1356,7 +1356,7 @@ lua_task_load_from_string (lua_State * L)
                        }
                }
 
-               task = rspamd_task_new (NULL, cfg, NULL, NULL);
+               task = rspamd_task_new (NULL, cfg, NULL, NULL, NULL);
                task->msg.begin = g_strdup (str_message);
                task->msg.len   = message_len;
                rspamd_mempool_add_destructor (task->task_pool, lua_task_free_dtor, task);
index ace99048e0af88a60966ad56c81a2455123f2c0c..15f6c1b8189fa71be3e3248c6a4f06c5f32dda45 100644 (file)
@@ -780,8 +780,7 @@ lua_util_process_message (lua_State *L)
        if (cfg != NULL && message != NULL) {
                base = event_init ();
                rspamd_init_filters (cfg, FALSE);
-               task = rspamd_task_new (NULL, cfg, NULL, NULL);
-               task->ev_base = base;
+               task = rspamd_task_new (NULL, cfg, NULL, NULL, base);
                task->msg.begin = rspamd_mempool_alloc (task->task_pool, mlen);
                rspamd_strlcpy ((gpointer)task->msg.begin, message, mlen);
                task->msg.len = mlen;
index 1f16c5a486e613c3a9456c1edef2d359dc4ce513..79ffc70a5ad8c60ff6b046a544ae6d07e53095cb 100644 (file)
@@ -3037,9 +3037,9 @@ fuzzy_process_handler (struct rspamd_http_connection_entry *conn_ent,
        struct fuzzy_ctx *fuzzy_module_ctx;
 
        /* Prepare task */
-       task = rspamd_task_new (session->wrk, session->cfg, NULL, session->lang_det);
+       task = rspamd_task_new (session->wrk, session->cfg, NULL,
+                       session->lang_det, conn_ent->rt->ev_base);
        task->cfg = ctx->cfg;
-       task->ev_base = conn_ent->rt->ev_base;
        saved = rspamd_mempool_alloc0 (session->pool, sizeof (gint));
        err = rspamd_mempool_alloc0 (session->pool, sizeof (GError *));
        fuzzy_module_ctx = fuzzy_get_context (ctx->cfg);
index a43527823c13d5125b7ecab89a2f92aad7a5875b..6d150110028f72afb4c919880cad1bc8be694fcb 100644 (file)
@@ -431,7 +431,7 @@ rspamadm_lua_message_handler (lua_State *L, gint argc, gchar **argv)
                        rspamd_printf ("cannot open %s: %s\n", argv[i], strerror (errno));
                }
                else {
-                       task = rspamd_task_new (NULL, rspamd_main->cfg, NULL, NULL);
+                       task = rspamd_task_new (NULL, rspamd_main->cfg, NULL, NULL, NULL);
 
                        if (!rspamd_task_load_message (task, NULL, map, len)) {
                                rspamd_printf ("cannot load %s\n", argv[i]);
index 77a5bb7e6fe7b168c18694df0c42c4044928059e..aed6427521f4a316665bec84b2f92a0102ecfe50 100644 (file)
@@ -1694,7 +1694,8 @@ rspamd_proxy_self_scan (struct rspamd_proxy_session *session)
 
        msg = session->client_message;
        task = rspamd_task_new (session->worker, session->ctx->cfg,
-                       session->pool, session->ctx->lang_det);
+                       session->pool, session->ctx->lang_det,
+                       session->ctx->ev_base);
        task->flags |= RSPAMD_TASK_FLAG_MIME;
        task->sock = -1;
 
@@ -1710,7 +1711,6 @@ rspamd_proxy_self_scan (struct rspamd_proxy_session *session)
        task->resolver = session->ctx->resolver;
        /* TODO: allow to disable autolearn in protocol */
        task->flags |= RSPAMD_TASK_FLAG_LEARN_AUTO;
-       task->ev_base = session->ctx->ev_base;
        task->s = rspamd_session_create (task->task_pool, rspamd_proxy_task_fin,
                        NULL, (event_finalizer_t )rspamd_task_free, task);
        data = rspamd_http_message_get_body (msg, &len);
index 26ce63a94605defb873499377af17e609ab106dd..544f05cbd48454b90ba4168f9e2f367e8e1cc469 100644 (file)
@@ -97,9 +97,8 @@ rspamd_worker_call_finish_handlers (struct rspamd_worker *worker)
        if (cfg->finish_callbacks) {
                ctx = worker->ctx;
                /* Create a fake task object for async events */
-               task = rspamd_task_new (worker, cfg, NULL, NULL);
+               task = rspamd_task_new (worker, cfg, NULL, NULL, ctx->ev_base);
                task->resolver = ctx->resolver;
-               task->ev_base = ctx->ev_base;
                task->flags |= RSPAMD_TASK_FLAG_PROCESSING;
                task->s = rspamd_session_create (task->task_pool,
                                rspamd_worker_finalize,
@@ -364,7 +363,7 @@ accept_socket (gint fd, short what, void *arg)
                return;
        }
 
-       task = rspamd_task_new (worker, ctx->cfg, NULL, ctx->lang_det);
+       task = rspamd_task_new (worker, ctx->cfg, NULL, ctx->lang_det, ctx->ev_base);
 
        msg_info_task ("accepted connection from %s port %d, task ptr: %p",
                rspamd_inet_address_to_string (addr),
@@ -395,7 +394,6 @@ accept_socket (gint fd, short what, void *arg)
                        ctx->keys_cache,
                        NULL);
        rspamd_http_connection_set_max_size (task->http_conn, task->cfg->max_message);
-       task->ev_base = ctx->ev_base;
        worker->nconns++;
        rspamd_mempool_add_destructor (task->task_pool,
                (rspamd_mempool_destruct_t)reduce_tasks_count, worker);