]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Refactor more, use ev_stat for cdb watching
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 15 Jun 2019 20:38:24 +0000 (21:38 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 22 Jun 2019 09:57:29 +0000 (10:57 +0100)
contrib/cdb/cdb.h
contrib/cdb/cdb_init.c
src/libcryptobox/keypairs_cache.c
src/libserver/task.h
src/libstat/backends/redis_backend.c
src/libutil/CMakeLists.txt
src/libutil/util.c
src/libutil/util.h
src/lua/lua_cdb.c

index f8071fa38effd628703c35b2532683967070383f..8774799a4a55f098f531ff9bd643d7c4be587c1a 100644 (file)
@@ -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);
index 0c0b5e353354775b0e38ae02e08cc218c3f159ae..bfc6dd0c2a3b0908bb7d8c5917ece290a4f9989f 100644 (file)
@@ -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);
 }
index 5e3a13e1809d0ab87adf91a4f2ff9073ff9af68a..bcba5e24783561f2627c033abbf6cff86cf65d00 100644 (file)
@@ -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 {
index 736644126af8822168e0a2106d1ed8b4e1dd23fa..ca42da6b3539fb6b26cba4ac86adc71c3e98b686 100644 (file)
@@ -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                                           */
index 937327c7f4cfbd545a624f14de418924f83d5ef3..b5f02c27041e3d118f7d4796daa867e1f3ec659a 100644 (file)
@@ -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) {
index f86d650f020d181351e1d71e70f1086f56f4bba2..fd29c551214d9fd8cd92ce3c3e38140d8aa8a8da 100644 (file)
@@ -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
index c5fec19a7d0ec31d615b75fa78f9039fc3a48349..665b6accb466a0e996f8b3d26fc079dde767e813 100644 (file)
@@ -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)
index 75f39148271c79964d5c758ef3b0c90de011b290..b7d6055ce6c446b8016fe9c036f5ce8d3bad2ddb 100644 (file)
@@ -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
index a7292da03dafc1a56d7d43005bacc400978a4ec6..0b8c27b2ab9f1ab9867e7200be1b22520a37304d 100644 (file)
@@ -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) {