commit de721b4: [Project] Fix usage of periodic events

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


Author: Vsevolod Stakhov
Date: 2019-06-17 13:24:30 +0100
URL: https://github.com/rspamd/rspamd/commit/de721b490bce9f95385ab451da1190f934cf3425

[Project] Fix usage of periodic events

---
 src/libutil/http_context.c | 19 ++++++++-----------
 src/libutil/http_private.h |  2 +-
 src/libutil/map.c          | 25 ++++++++++++++++++-------
 src/libutil/map_private.h  |  3 ++-
 4 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/src/libutil/http_context.c b/src/libutil/http_context.c
index 3f7f86910..95ab7021c 100644
--- a/src/libutil/http_context.c
+++ b/src/libutil/http_context.c
@@ -65,19 +65,16 @@ rspamd_http_keepalive_queue_cleanup (GQueue *conns)
 }
 
 static void
-rspamd_http_context_client_rotate_ev (struct ev_loop *loop, ev_periodic *w, int revents)
+rspamd_http_context_client_rotate_ev (struct ev_loop *loop, ev_timer *w, int revents)
 {
-	struct timeval rot_tv;
 	struct rspamd_http_context *ctx = (struct rspamd_http_context *)w->data;
 	gpointer kp;
 
-	double_to_tv (ctx->config.client_key_rotate_time, &rot_tv);
-	rot_tv.tv_sec += ottery_rand_range (rot_tv.tv_sec);
+	w->repeat = rspamd_time_jitter (ctx->config.client_key_rotate_time, 0);
+	msg_debug_http_context ("rotate local keypair, next rotate in %.0f seconds",
+			w->repeat);
 
-	msg_debug_http_context ("rotate local keypair, next rotate in %d seconds",
-			(int)rot_tv.tv_sec);
-
-	ev_periodic_again (loop, w);
+	ev_timer_again (loop, w);
 
 	kp = ctx->client_kp;
 	ctx->client_kp = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX,
@@ -190,9 +187,9 @@ rspamd_http_context_init (struct rspamd_http_context *ctx)
 		double jittered = rspamd_time_jitter (ctx->config.client_key_rotate_time,
 				0);
 
-		ev_periodic_init (&ctx->client_rotate_ev,
-				rspamd_http_context_client_rotate_ev, 0.0, jittered, NULL);
-		ev_periodic_start (ctx->event_loop, &ctx->client_rotate_ev);
+		ev_timer_init (&ctx->client_rotate_ev,
+				rspamd_http_context_client_rotate_ev, jittered, 0);
+		ev_timer_start (ctx->event_loop, &ctx->client_rotate_ev);
 		ctx->client_rotate_ev.data = ctx;
 	}
 
diff --git a/src/libutil/http_private.h b/src/libutil/http_private.h
index e09dbef40..f5a7dd9cc 100644
--- a/src/libutil/http_private.h
+++ b/src/libutil/http_private.h
@@ -101,7 +101,7 @@ struct rspamd_http_context {
 	gpointer ssl_ctx;
 	gpointer ssl_ctx_noverify;
 	struct ev_loop *event_loop;
-	ev_periodic client_rotate_ev;
+	ev_timer client_rotate_ev;
 	khash_t (rspamd_keep_alive_hash) *keep_alive_hash;
 };
 
diff --git a/src/libutil/map.c b/src/libutil/map.c
index 1b3419a17..3d9c84ea7 100644
--- a/src/libutil/map.c
+++ b/src/libutil/map.c
@@ -330,7 +330,7 @@ http_map_error (struct rspamd_http_connection *conn,
 }
 
 static void
-rspamd_map_cache_cb (struct ev_loop *loop, ev_periodic *w, int revents)
+rspamd_map_cache_cb (struct ev_loop *loop, ev_timer *w, int revents)
 {
 	struct rspamd_http_map_cached_cbdata *cache_cbd = (struct rspamd_http_map_cached_cbdata *)
 			w->data;
@@ -340,8 +340,6 @@ rspamd_map_cache_cb (struct ev_loop *loop, ev_periodic *w, int revents)
 	map = cache_cbd->map;
 	data = cache_cbd->data;
 
-	ev_periodic_stop (loop, &cache_cbd->timeout);
-
 	if (cache_cbd->gen != cache_cbd->data->gen) {
 		/* We have another update, so this cache element is obviously expired */
 		/*
@@ -351,6 +349,7 @@ rspamd_map_cache_cb (struct ev_loop *loop, ev_periodic *w, int revents)
 		msg_info_map ("cached data is now expired (gen mismatch %L != %L) for %s",
 				cache_cbd->gen, cache_cbd->data->gen, map->name);
 		MAP_RELEASE (cache_cbd->shm, "rspamd_http_map_cached_cbdata");
+		ev_timer_stop (loop, &cache_cbd->timeout);
 		g_free (cache_cbd);
 	}
 	else if (cache_cbd->data->last_checked >= cache_cbd->last_checked) {
@@ -358,15 +357,25 @@ rspamd_map_cache_cb (struct ev_loop *loop, ev_periodic *w, int revents)
 		 * We checked map but we have not found anything more recent,
 		 * reschedule cache check
 		 */
+		if (cache_cbd->map->poll_timeout >
+			ev_now (loop) - cache_cbd->data->last_checked) {
+			w->repeat = cache_cbd->map->poll_timeout -
+						(ev_now (loop) - cache_cbd->data->last_checked);
+		}
+		else {
+			w->repeat = cache_cbd->map->poll_timeout;
+		}
+
 		cache_cbd->last_checked = cache_cbd->data->last_checked;
 		msg_debug_map ("cached data is up to date for %s", map->name);
-		ev_periodic_again (loop, &cache_cbd->timeout);
+		ev_timer_again (loop, &cache_cbd->timeout);
 	}
 	else {
 		data->cur_cache_cbd = NULL;
 		g_atomic_int_set (&data->cache->available, 0);
 		MAP_RELEASE (cache_cbd->shm, "rspamd_http_map_cached_cbdata");
 		msg_info_map ("cached data is now expired for %s", map->name);
+		ev_timer_stop (loop, &cache_cbd->timeout);
 		g_free (cache_cbd);
 	}
 }
@@ -675,14 +684,16 @@ read_data:
 		data->cache->last_modified = cbd->data->last_modified;
 		cache_cbd = g_malloc0 (sizeof (*cache_cbd));
 		cache_cbd->shm = cbd->shmem_data;
+		cache_cbd->event_loop = cbd->event_loop;
 		cache_cbd->map = map;
 		cache_cbd->data = cbd->data;
 		cache_cbd->last_checked = cbd->data->last_checked;
 		cache_cbd->gen = cbd->data->gen;
 		MAP_RETAIN (cache_cbd->shm, "shmem_data");
 
-		ev_periodic_set (&cache_cbd->timeout, 0.0, cached_timeout, NULL);
-		ev_periodic_start (cbd->event_loop, &cache_cbd->timeout);
+		ev_timer_init (&cache_cbd->timeout, rspamd_map_cache_cb, cached_timeout,
+				0.0);
+		ev_timer_start (cbd->event_loop, &cache_cbd->timeout);
 		cache_cbd->timeout.data = cache_cbd;
 		data->cur_cache_cbd = cache_cbd;
 
@@ -2258,7 +2269,7 @@ rspamd_map_backend_dtor (struct rspamd_map_backend *bk)
 				if (data->cur_cache_cbd) {
 					MAP_RELEASE (data->cur_cache_cbd->shm,
 							"rspamd_http_map_cached_cbdata");
-					ev_periodic_stop (ev_default_loop (0),
+					ev_timer_stop (data->cur_cache_cbd->event_loop,
 							&data->cur_cache_cbd->timeout);
 					g_free (data->cur_cache_cbd);
 					data->cur_cache_cbd = NULL;
diff --git a/src/libutil/map_private.h b/src/libutil/map_private.h
index db5a72704..455919d15 100644
--- a/src/libutil/map_private.h
+++ b/src/libutil/map_private.h
@@ -62,7 +62,8 @@ struct file_map_data {
 struct http_map_data;
 
 struct rspamd_http_map_cached_cbdata {
-	ev_periodic timeout;
+	ev_timer timeout;
+	struct ev_loop *event_loop;
 	struct rspamd_storage_shmem *shm;
 	struct rspamd_map *map;
 	struct http_map_data *data;


More information about the Commits mailing list