commit bb3a48e: [Project] Refactor more, use ev_stat for cdb watching

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


Author: Vsevolod Stakhov
Date: 2019-06-15 21:38:24 +0100
URL: https://github.com/rspamd/rspamd/commit/bb3a48e42835fdbb456fdf0d0f74050fa61ff7d5

[Project] Refactor more, use ev_stat for cdb watching

---
 contrib/cdb/cdb.h                    |  8 +++---
 contrib/cdb/cdb_init.c               | 47 +++++++++++++-----------------------
 src/libcryptobox/keypairs_cache.c    |  2 +-
 src/libserver/task.h                 |  4 +--
 src/libstat/backends/redis_backend.c | 46 ++++++++++++++++++-----------------
 src/libutil/CMakeLists.txt           |  1 +
 src/libutil/util.c                   | 18 --------------
 src/libutil/util.h                   |  4 ---
 src/lua/lua_cdb.c                    | 10 +-------
 9 files changed, 51 insertions(+), 89 deletions(-)

diff --git a/contrib/cdb/cdb.h b/contrib/cdb/cdb.h
index f8071fa38..8774799a4 100644
--- a/contrib/cdb/cdb.h
+++ b/contrib/cdb/cdb.h
@@ -10,6 +10,7 @@
 
 #include "config.h"
 #include "unix-std.h"
+#include "contrib/libev/ev.h"
 
 /*
  * OpenBSD fix
@@ -34,8 +35,9 @@ struct cdb
 	int cdb_fd; /* file descriptor */
 	char *filename; /* file name */
 	time_t mtime; /* mtime of cdb file */
-	struct event *check_timer_ev; /* event structure for checking cdb for modifications */
-	struct timeval *check_timer_tv;
+	struct ev_loop *loop;
+	ev_stat stat_ev; /* event structure for checking cdb for modifications */
+	ev_tstamp check_ts;
 	/* private members */
 	unsigned cdb_fsize; /* datafile size */
 	unsigned cdb_dend; /* end of data ptr */
@@ -53,7 +55,7 @@ struct cdb
 #define cdb_fileno(c) ((c)->cdb_fd)
 
 int cdb_init(struct cdb *cdbp, int fd);
-void cdb_add_timer(struct cdb *cdbp, unsigned seconds);
+void cdb_add_timer(struct cdb *cdbp, EV_P_ ev_tstamp seconds);
 void cdb_free(struct cdb *cdbp);
 
 int cdb_read(const struct cdb *cdbp, void *buf, unsigned len, unsigned pos);
diff --git a/contrib/cdb/cdb_init.c b/contrib/cdb/cdb_init.c
index 0c0b5e353..bfc6dd0c2 100644
--- a/contrib/cdb/cdb_init.c
+++ b/contrib/cdb/cdb_init.c
@@ -83,10 +83,8 @@ cdb_free(struct cdb *cdbp)
 	}
 	cdbp->cdb_fsize = 0;
 
-	if (cdbp->check_timer_ev) {
-		evtimer_del (cdbp->check_timer_ev);
-		g_free (cdbp->check_timer_ev);
-		g_free (cdbp->check_timer_tv);
+	if (cdbp->loop) {
+		ev_stat_stop (cdbp->loop, &cdbp->stat_ev);
 	}
 }
 
@@ -111,43 +109,32 @@ cdb_read(const struct cdb *cdbp, void *buf, unsigned len, unsigned pos)
 }
 
 static void
-cdb_timer_callback (int fd, short what, gpointer ud)
+cdb_timer_callback (EV_P_ ev_stat *w, int revents)
 {
-	struct cdb *cdbp = ud;
+	struct cdb *cdbp = w->data;
 	gint nfd;
-	struct stat st;
 
 	/* Check cdb file for modifications */
-	if (stat (cdbp->filename, &st) != -1) {
-		if (st.st_mtime > cdbp->mtime) {
-			if ((nfd = open (cdbp->filename, O_RDONLY)) != -1) {
-				if (cdbp->cdb_mem) {
+	if ((nfd = open (cdbp->filename, O_RDONLY)) != -1) {
+		if (cdbp->cdb_mem) {
 #ifdef _WIN32
-					UnmapViewOfFile((void*) cdbp->cdb_mem);
+			UnmapViewOfFile((void*) cdbp->cdb_mem);
 #else
-					munmap ((void*) cdbp->cdb_mem, cdbp->cdb_fsize);
+			munmap ((void*) cdbp->cdb_mem, cdbp->cdb_fsize);
 #endif /* _WIN32 */
-					cdbp->cdb_mem = NULL;
-				}
-				(void)close (cdbp->cdb_fd);
-				cdbp->cdb_fsize = 0;
-				(void)cdb_init (cdbp, nfd);
-			}
+			cdbp->cdb_mem = NULL;
 		}
+		(void)close (cdbp->cdb_fd);
+		cdbp->cdb_fsize = 0;
+		(void)cdb_init (cdbp, nfd);
 	}
-
-	evtimer_add (cdbp->check_timer_ev, cdbp->check_timer_tv);
 }
 
 void
-cdb_add_timer(struct cdb *cdbp, unsigned seconds)
+cdb_add_timer (struct cdb *cdbp, EV_P_ ev_tstamp seconds)
 {
-	cdbp->check_timer_ev = g_malloc (sizeof (struct event));
-	cdbp->check_timer_tv = g_malloc (sizeof (struct timeval));
-
-	cdbp->check_timer_tv->tv_sec = seconds;
-	cdbp->check_timer_tv->tv_usec = 0;
-
-	evtimer_set (cdbp->check_timer_ev, cdb_timer_callback, cdbp);
-	evtimer_add (cdbp->check_timer_ev, cdbp->check_timer_tv);
+	cdbp->loop = loop;
+	ev_stat_init (&cdbp->stat_ev, cdb_timer_callback, cdbp->filename, seconds);
+	cdbp->stat_ev.data = cdbp;
+	ev_stat_start (EV_A_ &cdbp->stat_ev);
 }
diff --git a/src/libcryptobox/keypairs_cache.c b/src/libcryptobox/keypairs_cache.c
index 5e3a13e18..bcba5e247 100644
--- a/src/libcryptobox/keypairs_cache.c
+++ b/src/libcryptobox/keypairs_cache.c
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 #include "config.h"
-#include "rspamd.h"
 #include "keypairs_cache.h"
 #include "keypair_private.h"
+#include "libutil/util.h"
 #include "hash.h"
 
 struct rspamd_keypair_elt {
diff --git a/src/libserver/task.h b/src/libserver/task.h
index 736644126..ca42da6b3 100644
--- a/src/libserver/task.h
+++ b/src/libserver/task.h
@@ -200,8 +200,8 @@ struct rspamd_task {
 
 	struct rspamd_dns_resolver *resolver;			/**< DNS resolver									*/
 	struct ev_loop *event_loop;						/**< Event base										*/
-	struct event timeout_ev;						/**< Global task timeout							*/
-	struct event *guard_ev;							/**< Event for input sanity guard 					*/
+	struct ev_timer timeout_ev;						/**< Global task timeout							*/
+	struct ev_io *guard_ev;							/**< Event for input sanity guard 					*/
 
 	gpointer checkpoint;							/**< Opaque checkpoint data							*/
 	ucl_object_t *settings;							/**< Settings applied to task						*/
diff --git a/src/libstat/backends/redis_backend.c b/src/libstat/backends/redis_backend.c
index 937327c7f..b5f02c270 100644
--- a/src/libstat/backends/redis_backend.c
+++ b/src/libstat/backends/redis_backend.c
@@ -70,7 +70,7 @@ struct redis_stat_runtime {
 	struct redis_stat_ctx *ctx;
 	struct rspamd_task *task;
 	struct upstream *selected;
-	struct event timeout_event;
+	ev_timer timeout_event;
 	GArray *results;
 	struct rspamd_statfile_config *stcf;
 	gchar *redis_object_expanded;
@@ -1019,8 +1019,8 @@ rspamd_redis_fin (gpointer data)
 
 	rt->has_event = FALSE;
 	/* Stop timeout */
-	if (rspamd_event_pending (&rt->timeout_event, EV_TIMEOUT)) {
-		event_del (&rt->timeout_event);
+	if (ev_is_active (&rt->timeout_event)) {
+		ev_timer_stop (rt->task->event_loop, &rt->timeout_event);
 	}
 
 	if (rt->redis) {
@@ -1052,9 +1052,9 @@ rspamd_redis_fin_learn (gpointer data)
 }
 
 static void
-rspamd_redis_timeout (gint fd, short what, gpointer d)
+rspamd_redis_timeout (EV_P_ ev_timer *w, int revents)
 {
-	struct redis_stat_runtime *rt = REDIS_RUNTIME (d);
+	struct redis_stat_runtime *rt = REDIS_RUNTIME (w->data);
 	struct rspamd_task *task;
 	redisAsyncContext *redis;
 
@@ -1562,7 +1562,6 @@ rspamd_redis_process_tokens (struct rspamd_task *task,
 {
 	struct redis_stat_runtime *rt = REDIS_RUNTIME (p);
 	rspamd_fstring_t *query;
-	struct timeval tv;
 	gint ret;
 	const gchar *learned_key = "learns";
 
@@ -1591,13 +1590,15 @@ rspamd_redis_process_tokens (struct rspamd_task *task,
 		rspamd_session_add_event (task->s, rspamd_redis_fin, rt, M);
 		rt->has_event = TRUE;
 
-		if (rspamd_event_pending (&rt->timeout_event, EV_TIMEOUT)) {
-			event_del (&rt->timeout_event);
+
+		if (ev_is_active (&rt->timeout_event)) {
+			ev_timer_again (task->event_loop, &rt->timeout_event);
+		}
+		else {
+			ev_timer_init (&rt->timeout_event, rspamd_redis_timeout,
+					rt->ctx->timeout, 0.);
+			ev_timer_start (task->event_loop, &rt->timeout_event);
 		}
-		event_set (&rt->timeout_event, -1, EV_TIMEOUT, rspamd_redis_timeout, rt);
-		event_base_set (task->event_loop, &rt->timeout_event);
-		double_to_tv (rt->ctx->timeout, &tv);
-		event_add (&rt->timeout_event, &tv);
 
 		query = rspamd_redis_tokens_to_query (task, rt, tokens,
 				rt->ctx->new_schema ? "HGET" : "HMGET",
@@ -1628,8 +1629,8 @@ rspamd_redis_finalize_process (struct rspamd_task *task, gpointer runtime,
 	struct redis_stat_runtime *rt = REDIS_RUNTIME (runtime);
 	redisAsyncContext *redis;
 
-	if (rspamd_event_pending (&rt->timeout_event, EV_TIMEOUT)) {
-		event_del (&rt->timeout_event);
+	if (ev_is_active (&rt->timeout_event)) {
+		ev_timer_stop (task->event_loop, &rt->timeout_event);
 	}
 
 	if (rt->redis) {
@@ -1802,13 +1803,14 @@ rspamd_redis_learn_tokens (struct rspamd_task *task, GPtrArray *tokens,
 		rt->has_event = TRUE;
 
 		/* Set timeout */
-		if (rspamd_event_pending (&rt->timeout_event, EV_TIMEOUT)) {
-			event_del (&rt->timeout_event);
+		if (ev_is_active (&rt->timeout_event)) {
+			ev_timer_again (task->event_loop, &rt->timeout_event);
+		}
+		else {
+			ev_timer_init (&rt->timeout_event, rspamd_redis_timeout,
+					rt->ctx->timeout, 0.);
+			ev_timer_start (task->event_loop, &rt->timeout_event);
 		}
-		event_set (&rt->timeout_event, -1, EV_TIMEOUT, rspamd_redis_timeout, rt);
-		event_base_set (task->event_loop, &rt->timeout_event);
-		double_to_tv (rt->ctx->timeout, &tv);
-		event_add (&rt->timeout_event, &tv);
 
 		return TRUE;
 	}
@@ -1827,8 +1829,8 @@ rspamd_redis_finalize_learn (struct rspamd_task *task, gpointer runtime,
 	struct redis_stat_runtime *rt = REDIS_RUNTIME (runtime);
 	redisAsyncContext *redis;
 
-	if (rspamd_event_pending (&rt->timeout_event, EV_TIMEOUT)) {
-		event_del (&rt->timeout_event);
+	if (ev_is_active (&rt->timeout_event)) {
+		ev_timer_stop (task->event_loop, &rt->timeout_event);
 	}
 
 	if (rt->redis) {
diff --git a/src/libutil/CMakeLists.txt b/src/libutil/CMakeLists.txt
index f86d650f0..fd29c5512 100644
--- a/src/libutil/CMakeLists.txt
+++ b/src/libutil/CMakeLists.txt
@@ -1,6 +1,7 @@
 # Librspamd-util
 SET(LIBRSPAMDUTILSRC
 				${CMAKE_CURRENT_SOURCE_DIR}/addr.c
+				${CMAKE_CURRENT_SOURCE_DIR}/libev_helper.c
 				${CMAKE_CURRENT_SOURCE_DIR}/aio_event.c
 				${CMAKE_CURRENT_SOURCE_DIR}/bloom.c
 				${CMAKE_CURRENT_SOURCE_DIR}/expression.c
diff --git a/src/libutil/util.c b/src/libutil/util.c
index c5fec19a7..665b6accb 100644
--- a/src/libutil/util.c
+++ b/src/libutil/util.c
@@ -2570,24 +2570,6 @@ rspamd_constant_memcmp (const guchar *a, const guchar *b, gsize len)
 	return (((gint32)(guint16)((guint32)r + 0x8000) - 0x8000) == 0);
 }
 
-#if !defined(LIBEVENT_VERSION_NUMBER) || LIBEVENT_VERSION_NUMBER < 0x02000000UL
-struct ev_loop *
-event_get_base (struct event *ev)
-{
-	return ev->ev_base;
-}
-#endif
-
-int
-rspamd_event_pending (struct event *ev, short what)
-{
-	if (ev->ev_base == NULL) {
-		return 0;
-	}
-
-	return event_pending (ev, what, NULL);
-}
-
 int
 rspamd_file_xopen (const char *fname, int oflags, guint mode,
 		gboolean allow_symlink)
diff --git a/src/libutil/util.h b/src/libutil/util.h
index 75f391482..b7d6055ce 100644
--- a/src/libutil/util.h
+++ b/src/libutil/util.h
@@ -426,10 +426,6 @@ void rspamd_random_seed_fast (void);
  */
 gboolean rspamd_constant_memcmp (const guchar *a, const guchar *b, gsize len);
 
-
-/* Avoid stupidity in libevent > 1.4 */
-int rspamd_event_pending (struct event *ev, short what);
-
 /**
  * Open file without following symlinks or special stuff
  * @param fname filename
diff --git a/src/lua/lua_cdb.c b/src/lua/lua_cdb.c
index a7292da03..0b8c27b2a 100644
--- a/src/lua/lua_cdb.c
+++ b/src/lua/lua_cdb.c
@@ -64,13 +64,12 @@ lua_cdb_create (lua_State *L)
 	else {
 		cdb = g_malloc (sizeof (struct cdb));
 		cdb->filename = g_strdup (filename);
-		cdb->check_timer_ev = NULL;
-		cdb->check_timer_tv = NULL;
 		if (cdb_init (cdb, fd) == -1) {
 			msg_warn ("cannot open cdb: %s, %s", filename, strerror (errno));
 			lua_pushnil (L);
 		}
 		else {
+			cdb_add_timer (cdb, ev_default_loop (0), CDB_REFRESH_TIME);
 			pcdb = lua_newuserdata (L, sizeof (struct cdb *));
 			rspamd_lua_setclass (L, "rspamd{cdb}", -1);
 			*pcdb = cdb;
@@ -106,13 +105,6 @@ lua_cdb_lookup (lua_State *L)
 		lua_error (L);
 		return 1;
 	}
-	/*
-	 * XXX: this code is placed here because event_loop is called inside workers, so start
-	 * monitoring on first check, not on creation
-	 */
-	if (cdb->check_timer_ev == NULL) {
-		cdb_add_timer (cdb, CDB_REFRESH_TIME);
-	}
 
 	what = luaL_checkstring (L, 2);
 	if (cdb_find (cdb, what, strlen (what)) > 0) {


More information about the Commits mailing list