commit a3a8b32: [Project] More rework

Vsevolod Stakhov vsevolod at highsecure.ru
Sat Jun 22 12:14:23 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-06-18 19:07:25 +0100
URL: https://github.com/rspamd/rspamd/commit/a3a8b32851bd236333af1353dad0d663f30a555d

[Project] More rework

---
 src/libserver/fuzzy_backend_redis.c  | 100 ++++++++++++++++-------------------
 src/libserver/milter.c               |  11 +---
 src/libserver/milter.h               |   3 +-
 src/libserver/milter_internal.h      |   1 -
 src/libserver/protocol.c             |   2 +-
 src/libserver/redis_pool.c           |  24 ++++-----
 src/libserver/rspamd_symcache.c      |  67 ++++++++++-------------
 src/libstat/backends/redis_backend.c |   2 +-
 src/libstat/stat_config.c            |  23 ++++----
 src/libstat/stat_internal.h          |   6 +--
 10 files changed, 104 insertions(+), 135 deletions(-)

diff --git a/src/libserver/fuzzy_backend_redis.c b/src/libserver/fuzzy_backend_redis.c
index df9103e30..79c712386 100644
--- a/src/libserver/fuzzy_backend_redis.c
+++ b/src/libserver/fuzzy_backend_redis.c
@@ -71,9 +71,9 @@ enum rspamd_fuzzy_redis_command {
 struct rspamd_fuzzy_redis_session {
 	struct rspamd_fuzzy_backend_redis *backend;
 	redisAsyncContext *ctx;
-	struct event timeout;
+	ev_timer timeout;
 	const struct rspamd_fuzzy_cmd *cmd;
-	struct ev_loop *ev_base;
+	struct ev_loop *event_loop;
 	float prob;
 	gboolean shingles_checked;
 
@@ -143,10 +143,7 @@ rspamd_fuzzy_redis_session_dtor (struct rspamd_fuzzy_redis_session *session,
 				ac, is_fatal);
 	}
 
-	if (rspamd_event_pending (&session->timeout, EV_TIMEOUT)) {
-		event_del (&session->timeout);
-	}
-
+	ev_timer_stop (session->event_loop, &session->timeout);
 	rspamd_fuzzy_redis_session_free_args (session);
 
 	REF_RELEASE (session->backend);
@@ -276,9 +273,10 @@ rspamd_fuzzy_backend_init_redis (struct rspamd_fuzzy_backend *bk,
 }
 
 static void
-rspamd_fuzzy_redis_timeout (gint fd, short what, gpointer priv)
+rspamd_fuzzy_redis_timeout (EV_P_ ev_timer *w, int revents)
 {
-	struct rspamd_fuzzy_redis_session *session = priv;
+	struct rspamd_fuzzy_redis_session *session =
+			(struct rspamd_fuzzy_redis_session *)w->data;
 	redisAsyncContext *ac;
 	static char errstr[128];
 
@@ -320,12 +318,11 @@ rspamd_fuzzy_redis_shingles_callback (redisAsyncContext *c, gpointer r,
 	struct rspamd_fuzzy_redis_session *session = priv;
 	redisReply *reply = r, *cur;
 	struct rspamd_fuzzy_reply rep;
-	struct timeval tv;
 	GString *key;
 	struct _rspamd_fuzzy_shingles_helper *shingles, *prev = NULL, *sel = NULL;
 	guint i, found = 0, max_found = 0, cur_found = 0;
 
-	event_del (&session->timeout);
+	ev_timer_stop (session->event_loop, &session->timeout);
 	memset (&rep, 0, sizeof (rep));
 
 	if (c->err == 0) {
@@ -421,12 +418,11 @@ rspamd_fuzzy_redis_shingles_callback (redisAsyncContext *c, gpointer r,
 					}
 					else {
 						/* Add timeout */
-						event_set (&session->timeout, -1, EV_TIMEOUT,
+						session->timeout.data = session;
+						ev_timer_init (&session->timeout,
 								rspamd_fuzzy_redis_timeout,
-								session);
-						event_base_set (session->ev_base, &session->timeout);
-						double_to_tv (session->backend->timeout, &tv);
-						event_add (&session->timeout, &tv);
+								session->backend->timeout, 0.0);
+						ev_timer_start (session->event_loop, &session->timeout);
 					}
 
 					return;
@@ -456,7 +452,6 @@ rspamd_fuzzy_redis_shingles_callback (redisAsyncContext *c, gpointer r,
 static void
 rspamd_fuzzy_backend_check_shingles (struct rspamd_fuzzy_redis_session *session)
 {
-	struct timeval tv;
 	struct rspamd_fuzzy_reply rep;
 	const struct rspamd_fuzzy_shingle_cmd *shcmd;
 	GString *key;
@@ -501,11 +496,11 @@ rspamd_fuzzy_backend_check_shingles (struct rspamd_fuzzy_redis_session *session)
 	}
 	else {
 		/* Add timeout */
-		event_set (&session->timeout, -1, EV_TIMEOUT, rspamd_fuzzy_redis_timeout,
-				session);
-		event_base_set (session->ev_base, &session->timeout);
-		double_to_tv (session->backend->timeout, &tv);
-		event_add (&session->timeout, &tv);
+		session->timeout.data = session;
+		ev_timer_init (&session->timeout,
+				rspamd_fuzzy_redis_timeout,
+				session->backend->timeout, 0.0);
+		ev_timer_start (session->event_loop, &session->timeout);
 	}
 }
 
@@ -519,7 +514,7 @@ rspamd_fuzzy_redis_check_callback (redisAsyncContext *c, gpointer r,
 	gulong value;
 	guint found_elts = 0;
 
-	event_del (&session->timeout);
+	ev_timer_stop (session->event_loop, &session->timeout);
 	memset (&rep, 0, sizeof (rep));
 
 	if (c->err == 0) {
@@ -602,7 +597,6 @@ rspamd_fuzzy_backend_check_redis (struct rspamd_fuzzy_backend *bk,
 	struct rspamd_fuzzy_redis_session *session;
 	struct upstream *up;
 	struct upstream_list *ups;
-	struct timeval tv;
 	rspamd_inet_addr_t *addr;
 	struct rspamd_fuzzy_reply rep;
 	GString *key;
@@ -620,7 +614,7 @@ rspamd_fuzzy_backend_check_redis (struct rspamd_fuzzy_backend *bk,
 	session->prob = 1.0;
 	memcpy (rep.digest, session->cmd->digest, sizeof (rep.digest));
 	memcpy (session->found_digest, session->cmd->digest, sizeof (rep.digest));
-	session->ev_base = rspamd_fuzzy_backend_event_base (bk);
+	session->event_loop = rspamd_fuzzy_backend_event_base (bk);
 
 	/* First of all check digest */
 	session->nargs = 5;
@@ -677,11 +671,11 @@ rspamd_fuzzy_backend_check_redis (struct rspamd_fuzzy_backend *bk,
 		}
 		else {
 			/* Add timeout */
-			event_set (&session->timeout, -1, EV_TIMEOUT, rspamd_fuzzy_redis_timeout,
-					session);
-			event_base_set (session->ev_base, &session->timeout);
-			double_to_tv (backend->timeout, &tv);
-			event_add (&session->timeout, &tv);
+			session->timeout.data = session;
+			ev_timer_init (&session->timeout,
+					rspamd_fuzzy_redis_timeout,
+					session->backend->timeout, 0.0);
+			ev_timer_start (session->event_loop, &session->timeout);
 		}
 	}
 }
@@ -694,7 +688,7 @@ rspamd_fuzzy_redis_count_callback (redisAsyncContext *c, gpointer r,
 	redisReply *reply = r;
 	gulong nelts;
 
-	event_del (&session->timeout);
+	ev_timer_stop (session->event_loop, &session->timeout);
 
 	if (c->err == 0) {
 		rspamd_upstream_ok (session->up);
@@ -741,7 +735,6 @@ rspamd_fuzzy_backend_count_redis (struct rspamd_fuzzy_backend *bk,
 	struct rspamd_fuzzy_redis_session *session;
 	struct upstream *up;
 	struct upstream_list *ups;
-	struct timeval tv;
 	rspamd_inet_addr_t *addr;
 	GString *key;
 
@@ -754,7 +747,7 @@ rspamd_fuzzy_backend_count_redis (struct rspamd_fuzzy_backend *bk,
 	session->callback.cb_count = cb;
 	session->cbdata = ud;
 	session->command = RSPAMD_FUZZY_REDIS_COMMAND_COUNT;
-	session->ev_base = rspamd_fuzzy_backend_event_base (bk);
+	session->event_loop = rspamd_fuzzy_backend_event_base (bk);
 
 	session->nargs = 2;
 	session->argv = g_malloc (sizeof (gchar *) * 2);
@@ -801,11 +794,11 @@ rspamd_fuzzy_backend_count_redis (struct rspamd_fuzzy_backend *bk,
 		}
 		else {
 			/* Add timeout */
-			event_set (&session->timeout, -1, EV_TIMEOUT, rspamd_fuzzy_redis_timeout,
-					session);
-			event_base_set (session->ev_base, &session->timeout);
-			double_to_tv (backend->timeout, &tv);
-			event_add (&session->timeout, &tv);
+			session->timeout.data = session;
+			ev_timer_init (&session->timeout,
+					rspamd_fuzzy_redis_timeout,
+					session->backend->timeout, 0.0);
+			ev_timer_start (session->event_loop, &session->timeout);
 		}
 	}
 }
@@ -818,7 +811,7 @@ rspamd_fuzzy_redis_version_callback (redisAsyncContext *c, gpointer r,
 	redisReply *reply = r;
 	gulong nelts;
 
-	event_del (&session->timeout);
+	ev_timer_stop (session->event_loop, &session->timeout);
 
 	if (c->err == 0) {
 		rspamd_upstream_ok (session->up);
@@ -866,7 +859,6 @@ rspamd_fuzzy_backend_version_redis (struct rspamd_fuzzy_backend *bk,
 	struct rspamd_fuzzy_redis_session *session;
 	struct upstream *up;
 	struct upstream_list *ups;
-	struct timeval tv;
 	rspamd_inet_addr_t *addr;
 	GString *key;
 
@@ -879,7 +871,7 @@ rspamd_fuzzy_backend_version_redis (struct rspamd_fuzzy_backend *bk,
 	session->callback.cb_version = cb;
 	session->cbdata = ud;
 	session->command = RSPAMD_FUZZY_REDIS_COMMAND_VERSION;
-	session->ev_base = rspamd_fuzzy_backend_event_base (bk);
+	session->event_loop = rspamd_fuzzy_backend_event_base (bk);
 
 	session->nargs = 2;
 	session->argv = g_malloc (sizeof (gchar *) * 2);
@@ -926,11 +918,11 @@ rspamd_fuzzy_backend_version_redis (struct rspamd_fuzzy_backend *bk,
 		}
 		else {
 			/* Add timeout */
-			event_set (&session->timeout, -1, EV_TIMEOUT, rspamd_fuzzy_redis_timeout,
-					session);
-			event_base_set (session->ev_base, &session->timeout);
-			double_to_tv (backend->timeout, &tv);
-			event_add (&session->timeout, &tv);
+			session->timeout.data = session;
+			ev_timer_init (&session->timeout,
+					rspamd_fuzzy_redis_timeout,
+					session->backend->timeout, 0.0);
+			ev_timer_start (session->event_loop, &session->timeout);
 		}
 	}
 }
@@ -1309,7 +1301,8 @@ rspamd_fuzzy_redis_update_callback (redisAsyncContext *c, gpointer r,
 {
 	struct rspamd_fuzzy_redis_session *session = priv;
 	redisReply *reply = r;
-	event_del (&session->timeout);
+
+	ev_timer_stop (session->event_loop, &session->timeout);
 
 	if (c->err == 0) {
 		rspamd_upstream_ok (session->up);
@@ -1356,12 +1349,11 @@ rspamd_fuzzy_backend_update_redis (struct rspamd_fuzzy_backend *bk,
 	struct rspamd_fuzzy_redis_session *session;
 	struct upstream *up;
 	struct upstream_list *ups;
-	struct timeval tv;
 	rspamd_inet_addr_t *addr;
 	guint i;
 	GString *key;
 	struct fuzzy_peer_cmd *io_cmd;
-	struct rspamd_fuzzy_cmd *cmd;
+	struct rspamd_fuzzy_cmd *cmd = NULL;
 	guint nargs, ncommands, cur_shift;
 
 	g_assert (backend != NULL);
@@ -1445,7 +1437,7 @@ rspamd_fuzzy_backend_update_redis (struct rspamd_fuzzy_backend *bk,
 	session->command = RSPAMD_FUZZY_REDIS_COMMAND_UPDATES;
 	session->cmd = cmd;
 	session->prob = 1.0;
-	session->ev_base = rspamd_fuzzy_backend_event_base (bk);
+	session->event_loop = rspamd_fuzzy_backend_event_base (bk);
 
 	/* First of all check digest */
 	session->nargs = nargs;
@@ -1550,11 +1542,11 @@ rspamd_fuzzy_backend_update_redis (struct rspamd_fuzzy_backend *bk,
 		}
 		else {
 			/* Add timeout */
-			event_set (&session->timeout, -1, EV_TIMEOUT, rspamd_fuzzy_redis_timeout,
-					session);
-			event_base_set (session->ev_base, &session->timeout);
-			double_to_tv (backend->timeout, &tv);
-			event_add (&session->timeout, &tv);
+			session->timeout.data = session;
+			ev_timer_init (&session->timeout,
+					rspamd_fuzzy_redis_timeout,
+					session->backend->timeout, 0.0);
+			ev_timer_start (session->event_loop, &session->timeout);
 		}
 	}
 }
diff --git a/src/libserver/milter.c b/src/libserver/milter.c
index f9b4eee8c..bb27d2ff1 100644
--- a/src/libserver/milter.c
+++ b/src/libserver/milter.c
@@ -1083,7 +1083,7 @@ rspamd_milter_handle_session (struct rspamd_milter_session *session,
 
 
 gboolean
-rspamd_milter_handle_socket (gint fd, const struct timeval *tv,
+rspamd_milter_handle_socket (gint fd, ev_tstamp timeout,
 		rspamd_mempool_t *pool,
 		struct ev_loop *ev_base, rspamd_milter_finish finish_cb,
 		rspamd_milter_error error_cb, void *ud)
@@ -1108,6 +1108,7 @@ rspamd_milter_handle_socket (gint fd, const struct timeval *tv,
 	priv->pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "milter");
 	priv->discard_on_reject = milter_ctx->discard_on_reject;
 	priv->quarantine_on_reject = milter_ctx->quarantine_on_reject;
+	priv->ev.timeout = timeout;
 
 	if (pool) {
 		/* Copy tag */
@@ -1117,14 +1118,6 @@ rspamd_milter_handle_socket (gint fd, const struct timeval *tv,
 	priv->headers = kh_init (milter_headers_hash_t);
 	kh_resize (milter_headers_hash_t, priv->headers, 32);
 
-	if (tv) {
-		memcpy (&priv->tv, tv, sizeof (*tv));
-		priv->ptv = &priv->tv;
-	}
-	else {
-		priv->ptv = NULL;
-	}
-
 	session->priv = priv;
 	REF_INIT_RETAIN (session, rspamd_milter_session_dtor);
 
diff --git a/src/libserver/milter.h b/src/libserver/milter.h
index c01538f75..7906aeadf 100644
--- a/src/libserver/milter.h
+++ b/src/libserver/milter.h
@@ -20,6 +20,7 @@
 #include "fstring.h"
 #include "addr.h"
 #include "contrib/libucl/ucl.h"
+#include "contrib/libev/ev.h"
 #include "ref.h"
 
 enum rspamd_milter_reply {
@@ -81,7 +82,7 @@ typedef void (*rspamd_milter_error) (gint fd,
  * @param ud
  * @return
  */
-gboolean rspamd_milter_handle_socket (gint fd, const struct timeval *tv,
+gboolean rspamd_milter_handle_socket (gint fd, ev_tstamp timeout,
 		rspamd_mempool_t *pool,
 		struct ev_loop *ev_base, rspamd_milter_finish finish_cb,
 		rspamd_milter_error error_cb, void *ud);
diff --git a/src/libserver/milter_internal.h b/src/libserver/milter_internal.h
index 7c0e4f26f..41862a169 100644
--- a/src/libserver/milter_internal.h
+++ b/src/libserver/milter_internal.h
@@ -62,7 +62,6 @@ struct rspamd_milter_private {
 	struct rspamd_milter_parser parser;
 	struct rspamd_io_ev ev;
 	struct rspamd_milter_outbuf *out_chain;
-	struct timeval *ptv;
 	struct ev_loop *event_loop;
 	rspamd_mempool_t *pool;
 	khash_t(milter_headers_hash_t) *headers;
diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c
index f2827828b..c75a402a4 100644
--- a/src/libserver/protocol.c
+++ b/src/libserver/protocol.c
@@ -1301,7 +1301,7 @@ rspamd_protocol_write_ucl (struct rspamd_task *task,
 		ucl_object_insert_key (top, ucl_object_fromstring (task->message_id),
 				"message-id", 0, false);
 		ucl_object_insert_key (top,
-				ucl_object_fromdouble (task->time_real_finish - task->time_real),
+				ucl_object_fromdouble (task->time_real_finish - task->task_timestamp),
 				"time_real", 0, false);
 		ucl_object_insert_key (top,
 				ucl_object_fromdouble (task->time_virtual_finish - task->time_virtual),
diff --git a/src/libserver/redis_pool.c b/src/libserver/redis_pool.c
index d7e8015e5..6c74ee6f5 100644
--- a/src/libserver/redis_pool.c
+++ b/src/libserver/redis_pool.c
@@ -30,7 +30,7 @@ struct rspamd_redis_pool_connection {
 	struct redisAsyncContext *ctx;
 	struct rspamd_redis_pool_elt *elt;
 	GList *entry;
-	struct event timeout;
+	ev_timer timeout;
 	gboolean active;
 	gchar tag[MEMPOOL_UID_LEN];
 	ref_entry_t ref;
@@ -120,9 +120,7 @@ rspamd_redis_pool_conn_dtor (struct rspamd_redis_pool_connection *conn)
 	else {
 		msg_debug_rpool ("inactive connection removed");
 
-		if (rspamd_event_pending (&conn->timeout, EV_TIMEOUT)) {
-			event_del (&conn->timeout);
-		}
+		ev_timer_stop (conn->elt->pool->event_loop, &conn->timeout);
 
 		if (conn->ctx && !(conn->ctx->c.flags & REDIS_FREEING)) {
 			redisAsyncContext *ac = conn->ctx;
@@ -173,9 +171,10 @@ rspamd_redis_pool_elt_dtor (gpointer p)
 }
 
 static void
-rspamd_redis_conn_timeout (gint fd, short what, gpointer p)
+rspamd_redis_conn_timeout (EV_P_ ev_timer *w, int revents)
 {
-	struct rspamd_redis_pool_connection *conn = p;
+	struct rspamd_redis_pool_connection *conn =
+			(struct rspamd_redis_pool_connection *)w->data;
 
 	g_assert (!conn->active);
 	msg_debug_rpool ("scheduled removal of connection %p, refcount: %d",
@@ -186,7 +185,6 @@ rspamd_redis_conn_timeout (gint fd, short what, gpointer p)
 static void
 rspamd_redis_pool_schedule_timeout (struct rspamd_redis_pool_connection *conn)
 {
-	struct timeval tv;
 	gdouble real_timeout;
 	guint active_elts;
 
@@ -203,10 +201,12 @@ rspamd_redis_pool_schedule_timeout (struct rspamd_redis_pool_connection *conn)
 
 	msg_debug_rpool ("scheduled connection %p cleanup in %.1f seconds",
 			conn->ctx, real_timeout);
-	double_to_tv (real_timeout, &tv);
-	event_set (&conn->timeout, -1, EV_TIMEOUT, rspamd_redis_conn_timeout, conn);
-	event_base_set (conn->elt->pool->event_loop, &conn->timeout);
-	event_add (&conn->timeout, &tv);
+
+	conn->timeout.data = conn;
+	ev_timer_init (&conn->timeout,
+			rspamd_redis_conn_timeout,
+			real_timeout, 0.0);
+	ev_timer_start (conn->elt->pool->event_loop, &conn->timeout);
 }
 
 static void
@@ -352,7 +352,7 @@ rspamd_redis_pool_connect (struct rspamd_redis_pool *pool,
 			g_assert (!conn->active);
 
 			if (conn->ctx->err == REDIS_OK) {
-				event_del (&conn->timeout);
+				ev_timer_stop (elt->pool->event_loop, &conn->timeout);
 				conn->active = TRUE;
 				g_queue_push_tail_link (elt->active, conn_entry);
 				msg_debug_rpool ("reused existing connection to %s:%d: %p",
diff --git a/src/libserver/rspamd_symcache.c b/src/libserver/rspamd_symcache.c
index 43a916f3a..aa83024b2 100644
--- a/src/libserver/rspamd_symcache.c
+++ b/src/libserver/rspamd_symcache.c
@@ -229,10 +229,10 @@ struct cache_savepoint {
 
 struct rspamd_cache_refresh_cbdata {
 	gdouble last_resort;
-	struct event resort_ev;
+	ev_timer resort_ev;
 	struct rspamd_symcache *cache;
 	struct rspamd_worker *w;
-	struct ev_loop *ev_base;
+	struct ev_loop *event_loop;
 };
 
 /* weight, frequency, time */
@@ -1577,16 +1577,8 @@ rspamd_symcache_check_symbol (struct rspamd_task *task,
 
 	if (check) {
 		msg_debug_cache_task ("execute %s, %d", item->symbol, item->id);
-#ifdef HAVE_EVENT_NO_CACHE_TIME_FUNC
-		struct timeval tv;
-
-		event_base_update_cache_time (task->ev_base);
-		event_base_gettimeofday_cached (task->ev_base, &tv);
-		t1 = tv_to_double (&tv);
-#else
-		t1 = rspamd_get_ticks (FALSE);
-#endif
-		dyn_item->start_msec = (t1 - task->time_real) * 1e3;
+		t1 = ev_now (task->event_loop);
+		dyn_item->start_msec = (t1 - task->time_virtual) * 1e3;
 		dyn_item->async_events = 0;
 		checkpoint->cur_item = item;
 		checkpoint->items_inflight ++;
@@ -2200,11 +2192,11 @@ rspamd_symcache_call_peak_cb (struct ev_loop *ev_base,
 }
 
 static void
-rspamd_symcache_resort_cb (gint fd, short what, gpointer ud)
+rspamd_symcache_resort_cb (EV_P_ ev_timer *w, int revents)
 {
-	struct timeval tv;
 	gdouble tm;
-	struct rspamd_cache_refresh_cbdata *cbdata = ud;
+	struct rspamd_cache_refresh_cbdata *cbdata =
+			(struct rspamd_cache_refresh_cbdata *)w->data;
 	struct rspamd_symcache *cache;
 	struct rspamd_symcache_item *item;
 	guint i;
@@ -2217,10 +2209,8 @@ rspamd_symcache_resort_cb (gint fd, short what, gpointer ud)
 	cur_ticks = rspamd_get_ticks (FALSE);
 	msg_debug_cache ("resort symbols cache, next reload in %.2f seconds", tm);
 	g_assert (cache != NULL);
-	evtimer_set (&cbdata->resort_ev, rspamd_symcache_resort_cb, cbdata);
-	event_base_set (cbdata->ev_base, &cbdata->resort_ev);
-	double_to_tv (tm, &tv);
-	event_add (&cbdata->resort_ev, &tv);
+	cbdata->resort_ev.repeat = tm;
+	ev_timer_again (EV_A_ w);
 
 	if (rspamd_worker_is_primary_controller (cbdata->w)) {
 		/* Gather stats from shared execution times */
@@ -2263,7 +2253,7 @@ rspamd_symcache_resort_cb (gint fd, short what, gpointer ud)
 							item->frequency_peaks);
 
 					if (cache->peak_cb != -1) {
-						rspamd_symcache_call_peak_cb (cbdata->ev_base,
+						rspamd_symcache_call_peak_cb (cbdata->event_loop,
 								cache, item,
 								cur_value, cur_err);
 					}
@@ -2283,36 +2273,41 @@ rspamd_symcache_resort_cb (gint fd, short what, gpointer ud)
 			}
 		}
 
-
 		cbdata->last_resort = cur_ticks;
 		/* We don't do actual sorting due to topological guarantees */
 	}
 }
 
+static void
+rspamd_symcache_refresh_dtor (void *d)
+{
+	struct rspamd_cache_refresh_cbdata *cbdata =
+			(struct rspamd_cache_refresh_cbdata *)d;
+
+	ev_timer_stop (cbdata->event_loop, &cbdata->resort_ev);
+}
+
 void
 rspamd_symcache_start_refresh (struct rspamd_symcache *cache,
 							   struct ev_loop *ev_base, struct rspamd_worker *w)
 {
-	struct timeval tv;
 	gdouble tm;
 	struct rspamd_cache_refresh_cbdata *cbdata;
 
 	cbdata = rspamd_mempool_alloc0 (cache->static_pool, sizeof (*cbdata));
 	cbdata->last_resort = rspamd_get_ticks (TRUE);
-	cbdata->ev_base = ev_base;
+	cbdata->event_loop = ev_base;
 	cbdata->w = w;
 	cbdata->cache = cache;
 	tm = rspamd_time_jitter (cache->reload_time, 0);
 	msg_debug_cache ("next reload in %.2f seconds", tm);
 	g_assert (cache != NULL);
-	evtimer_set (&cbdata->resort_ev, rspamd_symcache_resort_cb,
-			cbdata);
-	event_base_set (ev_base, &cbdata->resort_ev);
-	double_to_tv (tm, &tv);
-	event_add (&cbdata->resort_ev, &tv);
+	cbdata->resort_ev.data = cbdata;
+	ev_timer_init (&cbdata->resort_ev, rspamd_symcache_resort_cb,
+			tm, tm);
+	ev_timer_start (cbdata->event_loop, &cbdata->resort_ev);
 	rspamd_mempool_add_destructor (cache->static_pool,
-			(rspamd_mempool_destruct_t) event_del,
-			&cbdata->resort_ev);
+			rspamd_symcache_refresh_dtor, cbdata);
 }
 
 void
@@ -2838,16 +2833,8 @@ rspamd_symcache_finalize_item (struct rspamd_task *task,
 	checkpoint->items_inflight --;
 	checkpoint->cur_item = NULL;
 
-#ifdef HAVE_EVENT_NO_CACHE_TIME_FUNC
-	struct timeval tv;
-	event_base_update_cache_time (task->ev_base);
-	event_base_gettimeofday_cached (task->ev_base, &tv);
-	t2 = tv_to_double (&tv);
-#else
-	t2 = rspamd_get_ticks (FALSE);
-#endif
-
-	diff = ((t2 - task->time_real) * 1e3 - dyn_item->start_msec);
+	t2 = ev_now (task->event_loop);
+	diff = ((t2 - task->time_virtual) * 1e3 - dyn_item->start_msec);
 
 	if (G_UNLIKELY (RSPAMD_TASK_IS_PROFILING (task))) {
 		rspamd_task_profile_set (task, item->symbol, diff);
diff --git a/src/libstat/backends/redis_backend.c b/src/libstat/backends/redis_backend.c
index b5f02c270..4e0d806f9 100644
--- a/src/libstat/backends/redis_backend.c
+++ b/src/libstat/backends/redis_backend.c
@@ -1444,7 +1444,7 @@ rspamd_redis_init (struct rspamd_stat_ctx *ctx,
 	backend->stcf = stf;
 
 	st_elt = g_malloc0 (sizeof (*st_elt));
-	st_elt->event_loop = ctx->ev_base;
+	st_elt->event_loop = ctx->event_loop;
 	st_elt->ctx = backend;
 	backend->stat_elt = rspamd_stat_ctx_register_async (
 			rspamd_redis_async_stat_cb,
diff --git a/src/libstat/stat_config.c b/src/libstat/stat_config.c
index 1ad064c53..52c78ba6b 100644
--- a/src/libstat/stat_config.c
+++ b/src/libstat/stat_config.c
@@ -163,7 +163,7 @@ rspamd_stat_init (struct rspamd_config *cfg, struct ev_loop *ev_base)
 	stat_ctx->statfiles = g_ptr_array_new ();
 	stat_ctx->classifiers = g_ptr_array_new ();
 	stat_ctx->async_elts = g_queue_new ();
-	stat_ctx->ev_base = ev_base;
+	stat_ctx->event_loop = ev_base;
 	stat_ctx->lua_stat_tokens_ref = -1;
 
 	/* Interact with lua_stat */
@@ -510,25 +510,24 @@ rspamd_async_elt_dtor (struct rspamd_stat_async_elt *elt)
 		elt->cleanup (elt, elt->ud);
 	}
 
-	event_del (&elt->timer_ev);
+	ev_timer_stop (elt->event_loop, &elt->timer_ev);
 	g_free (elt);
 }
 
 static void
-rspamd_async_elt_on_timer (gint fd, short what, gpointer d)
+rspamd_async_elt_on_timer (EV_P_ ev_timer *w, int revents)
 {
-	struct rspamd_stat_async_elt *elt = d;
+	struct rspamd_stat_async_elt *elt = (struct rspamd_stat_async_elt *)w->data;
 	gdouble jittered_time;
 
-	event_del (&elt->timer_ev);
 
 	if (elt->enabled) {
 		elt->handler (elt, elt->ud);
 	}
 
 	jittered_time = rspamd_time_jitter (elt->timeout, 0);
-	double_to_tv (jittered_time, &elt->tv);
-	event_add (&elt->timer_ev, &elt->tv);
+	elt->timer_ev.repeat = jittered_time;
+	ev_timer_again (EV_A_ w);
 }
 
 struct rspamd_stat_async_elt*
@@ -548,21 +547,19 @@ rspamd_stat_ctx_register_async (rspamd_stat_async_handler handler,
 	elt->cleanup = cleanup;
 	elt->ud = d;
 	elt->timeout = timeout;
+	elt->event_loop = st_ctx->event_loop;
 	REF_INIT_RETAIN (elt, rspamd_async_elt_dtor);
 	/* Enabled by default */
 
 
-	if (st_ctx->ev_base) {
+	if (st_ctx->event_loop) {
 		elt->enabled = TRUE;
-		event_set (&elt->timer_ev, -1, EV_TIMEOUT, rspamd_async_elt_on_timer, elt);
-		event_base_set (st_ctx->ev_base, &elt->timer_ev);
 		/*
 		 * First we set timeval to zero as we want cb to be executed as
 		 * fast as possible
 		 */
-		elt->tv.tv_sec = 0;
-		elt->tv.tv_usec = 0;
-		event_add (&elt->timer_ev, &elt->tv);
+		ev_timer_init (&elt->timer_ev, rspamd_async_elt_on_timer, 0.0, 0.0);
+		ev_timer_start (st_ctx->event_loop, &elt->timer_ev);
 	}
 	else {
 		elt->enabled = FALSE;
diff --git a/src/libstat/stat_internal.h b/src/libstat/stat_internal.h
index 84c1de2be..50dbae9c1 100644
--- a/src/libstat/stat_internal.h
+++ b/src/libstat/stat_internal.h
@@ -62,8 +62,8 @@ typedef void (*rspamd_stat_async_cleanup)(struct rspamd_stat_async_elt *elt,
 struct rspamd_stat_async_elt {
 	rspamd_stat_async_handler handler;
 	rspamd_stat_async_cleanup cleanup;
-	struct event timer_ev;
-	struct timeval tv;
+	struct ev_loop *event_loop;
+	ev_timer timer_ev;
 	gdouble timeout;
 	gboolean enabled;
 	gpointer ud;
@@ -93,7 +93,7 @@ struct rspamd_stat_ctx {
 	struct rspamd_stat_tokenizer *tokenizer;
 	gpointer tkcf;
 
-	struct ev_loop *ev_base;
+	struct ev_loop *event_loop;
 };
 
 typedef enum rspamd_learn_cache_result {


More information about the Commits mailing list