From ba79187398961b17d926afaf1106fc903fc25c10 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 17 Jun 2019 10:27:32 +0100 Subject: [PATCH] [Project] Further maps and http rework --- src/libutil/http_context.c | 33 ++++++++++++++------------------- src/libutil/map.c | 29 ++++++++++++----------------- 2 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/libutil/http_context.c b/src/libutil/http_context.c index f5d766d88..3f7f86910 100644 --- a/src/libutil/http_context.c +++ b/src/libutil/http_context.c @@ -23,6 +23,7 @@ #include "contrib/libottery/ottery.h" #include "contrib/http-parser/http_parser.h" #include "rspamd.h" +#include "libev_helper.h" INIT_LOG_MODULE(http_context) @@ -38,7 +39,7 @@ struct rspamd_http_keepalive_cbdata { struct rspamd_http_context *ctx; GQueue *queue; GList *link; - struct event ev; + struct rspamd_io_ev ev; }; static void @@ -64,10 +65,10 @@ rspamd_http_keepalive_queue_cleanup (GQueue *conns) } static void -rspamd_http_context_client_rotate_ev (gint fd, short what, void *arg) +rspamd_http_context_client_rotate_ev (struct ev_loop *loop, ev_periodic *w, int revents) { struct timeval rot_tv; - struct rspamd_http_context *ctx = arg; + struct rspamd_http_context *ctx = (struct rspamd_http_context *)w->data; gpointer kp; double_to_tv (ctx->config.client_key_rotate_time, &rot_tv); @@ -76,8 +77,7 @@ rspamd_http_context_client_rotate_ev (gint fd, short what, void *arg) msg_debug_http_context ("rotate local keypair, next rotate in %d seconds", (int)rot_tv.tv_sec); - event_del (&ctx->client_rotate_ev); - event_add (&ctx->client_rotate_ev, &rot_tv); + ev_periodic_again (loop, w); kp = ctx->client_kp; ctx->client_kp = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX, @@ -187,15 +187,13 @@ rspamd_http_context_init (struct rspamd_http_context *ctx) } if (ctx->config.client_key_rotate_time > 0 && ctx->event_loop) { - struct timeval tv; double jittered = rspamd_time_jitter (ctx->config.client_key_rotate_time, 0); - double_to_tv (jittered, &tv); - event_set (&ctx->client_rotate_ev, -1, EV_TIMEOUT, - rspamd_http_context_client_rotate_ev, ctx); - event_base_set (ctx->event_loop, &ctx->client_rotate_ev); - event_add (&ctx->client_rotate_ev, &tv); + 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); + ctx->client_rotate_ev.data = ctx; } if (ctx->config.http_proxy) { @@ -412,7 +410,7 @@ rspamd_http_context_check_keepalive (struct rspamd_http_context *ctx, struct rspamd_http_connection *conn; cbd = g_queue_pop_head (conns); - event_del (&cbd->ev); + rspamd_ev_watcher_stop (ctx->event_loop, &cbd->ev); conn = cbd->conn; g_free (cbd); @@ -491,6 +489,7 @@ rspamd_http_keepalive_handler (gint fd, short what, gpointer ud) cbdata->conn->keepalive_hash_key->host, cbdata->queue->length); rspamd_http_connection_unref (cbdata->conn); + rspamd_ev_watcher_stop (cbdata->ctx->event_loop, &cbdata->ev); g_free (cbdata); } @@ -498,10 +497,9 @@ void rspamd_http_context_push_keepalive (struct rspamd_http_context *ctx, struct rspamd_http_connection *conn, struct rspamd_http_message *msg, - struct ev_loop *ev_base) + struct ev_loop *event_loop) { struct rspamd_http_keepalive_cbdata *cbdata; - struct timeval tv; gdouble timeout = ctx->config.keepalive_interval; g_assert (conn->keepalive_hash_key != NULL); @@ -571,17 +569,14 @@ rspamd_http_context_push_keepalive (struct rspamd_http_context *ctx, cbdata->ctx = ctx; conn->finished = FALSE; - event_set (&cbdata->ev, conn->fd, EV_READ|EV_TIMEOUT, + rspamd_ev_watcher_init (&cbdata->ev, conn->fd, EV_READ, rspamd_http_keepalive_handler, cbdata); + rspamd_ev_watcher_start (event_loop, &cbdata->ev, timeout); msg_debug_http_context ("push keepalive element %s (%s), %d connections queued, %.1f timeout", rspamd_inet_address_to_string_pretty (cbdata->conn->keepalive_hash_key->addr), cbdata->conn->keepalive_hash_key->host, cbdata->queue->length, timeout); - - double_to_tv (timeout, &tv); - event_base_set (ev_base, &cbdata->ev); - event_add (&cbdata->ev, &tv); } \ No newline at end of file diff --git a/src/libutil/map.c b/src/libutil/map.c index 17da0062a..ffb58f8c2 100644 --- a/src/libutil/map.c +++ b/src/libutil/map.c @@ -435,7 +435,6 @@ http_map_finish (struct rspamd_http_connection *conn, struct rspamd_map_backend *bk; struct http_map_data *data; struct rspamd_http_map_cached_cbdata *cache_cbd; - struct timeval tv; const rspamd_ftok_t *expires_hdr, *etag_hdr; char next_check_date[128]; guchar *aux_data, *in = NULL; @@ -1669,9 +1668,8 @@ check: } static void -rspamd_map_http_check_callback (gint fd, short what, void *ud) +rspamd_map_http_check_callback (struct map_periodic_cbdata *cbd) { - struct map_periodic_cbdata *cbd = ud; struct rspamd_map *map; struct rspamd_map_backend *bk; @@ -1682,9 +1680,8 @@ rspamd_map_http_check_callback (gint fd, short what, void *ud) } static void -rspamd_map_http_read_callback (void *ud) +rspamd_map_http_read_callback (struct map_periodic_cbdata *cbd) { - struct map_periodic_cbdata *cbd = ud; struct rspamd_map *map; struct rspamd_map_backend *bk; @@ -1694,10 +1691,9 @@ rspamd_map_http_read_callback (void *ud) } static void -rspamd_map_file_check_callback (void *ud) +rspamd_map_file_check_callback (struct map_periodic_cbdata *periodic) { struct rspamd_map *map; - struct map_periodic_cbdata *periodic = ud; struct file_map_data *data; struct rspamd_map_backend *bk; @@ -1722,10 +1718,9 @@ rspamd_map_file_check_callback (void *ud) } static void -rspamd_map_static_check_callback (gint fd, short what, void *ud) +rspamd_map_static_check_callback (struct map_periodic_cbdata *periodic) { struct rspamd_map *map; - struct map_periodic_cbdata *periodic = ud; struct static_map_data *data; struct rspamd_map_backend *bk; @@ -1854,13 +1849,13 @@ rspamd_map_process_periodic (struct map_periodic_cbdata *cbd) switch (bk->protocol) { case MAP_PROTO_HTTP: case MAP_PROTO_HTTPS: - rspamd_map_http_read_callback (fd, what, cbd); + rspamd_map_http_read_callback (cbd); break; case MAP_PROTO_FILE: - rspamd_map_file_read_callback (fd, what, cbd); + rspamd_map_file_read_callback (cbd); break; case MAP_PROTO_STATIC: - rspamd_map_static_read_callback (fd, what, cbd); + rspamd_map_static_read_callback (cbd); break; } } else { @@ -1868,13 +1863,13 @@ rspamd_map_process_periodic (struct map_periodic_cbdata *cbd) switch (bk->protocol) { case MAP_PROTO_HTTP: case MAP_PROTO_HTTPS: - rspamd_map_http_check_callback (fd, what, cbd); + rspamd_map_http_check_callback (cbd); break; case MAP_PROTO_FILE: - rspamd_map_file_check_callback (fd, what, cbd); + rspamd_map_file_check_callback (cbd); break; case MAP_PROTO_STATIC: - rspamd_map_static_check_callback (fd, what, cbd); + rspamd_map_static_check_callback (cbd); break; } } @@ -2240,7 +2235,8 @@ 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"); - event_del (&data->cur_cache_cbd->timeout); + ev_periodic_stop (ev_default_loop (0), + &data->cur_cache_cbd->timeout); g_free (data->cur_cache_cbd); data->cur_cache_cbd = NULL; } @@ -2299,7 +2295,6 @@ rspamd_map_parse_backend (struct rspamd_config *cfg, const gchar *map_line) /* Now check for each proto separately */ if (bk->protocol == MAP_PROTO_FILE) { fdata = g_malloc0 (sizeof (struct file_map_data)); - fdata->st.st_mtime = -1; if (access (bk->uri, R_OK) == -1) { if (errno != ENOENT) { -- 2.39.5