]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Make min_resolve_interval configurable
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 8 Jun 2024 12:36:11 +0000 (13:36 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 8 Jun 2024 12:36:11 +0000 (13:36 +0100)
src/libserver/cfg_file.h
src/libserver/cfg_rcl.cxx
src/libutil/upstream.c

index a1ede5669dc01c2acb738f372b65a5a820e2ba9b..1ba1d84ad699f490f27c22ad98b1a527e6f840c7 100644 (file)
@@ -450,6 +450,7 @@ struct rspamd_config {
        double upstream_error_time;               /**< rate of upstream errors                                                  */
        double upstream_revive_time;              /**< revive timeout for upstreams                                             */
        double upstream_lazy_resolve_time;        /**< lazy resolve time for upstreams                                  */
+       double upstream_resolve_min_interval;     /**< minimum interval for resolving attempts (60 seconds by default) */
        struct upstream_ctx *ups_ctx;             /**< upstream context                                                                 */
        struct rspamd_dns_resolver *dns_resolver; /**< dns resolver if loaded                                                           */
 
index 4feb55de00c02305f5c16afc8eee3d7019fa1dec..eb89e6301a9d6d1113dc3f9823d3fbf3ebf2d8c5 100644 (file)
@@ -2285,6 +2285,12 @@ rspamd_rcl_config_init(struct rspamd_config *cfg, GHashTable *skip_sections)
                                                                           G_STRUCT_OFFSET(struct rspamd_config, upstream_lazy_resolve_time),
                                                                           RSPAMD_CL_FLAG_TIME_FLOAT,
                                                                           "Time to resolve upstreams addresses in lazy mode");
+               rspamd_rcl_add_default_handler(ssub,
+                                                                          "resolve_min_interval",
+                                                                          rspamd_rcl_parse_struct_time,
+                                                                          G_STRUCT_OFFSET(struct rspamd_config, upstream_resolve_min_interval),
+                                                                          RSPAMD_CL_FLAG_TIME_FLOAT,
+                                                                          "Minumum interval to perform resolving (60 seconds by default)");
        }
 
        if (!(skip_sections && g_hash_table_lookup(skip_sections, "actions"))) {
index 300f907a79792bbc3b310e4f6ce3d8809f5c84e5..49570dec0ab4465c07b647a892b414b4d4bbf85b 100644 (file)
@@ -87,6 +87,7 @@ struct upstream_limits {
        double error_time;
        double dns_timeout;
        double lazy_resolve_time;
+       double resolve_min_interval;
        unsigned int max_errors;
        unsigned int dns_retransmits;
 };
@@ -157,9 +158,10 @@ static const double default_error_time = DEFAULT_ERROR_TIME;
 static const double default_dns_timeout = DEFAULT_DNS_TIMEOUT;
 #define DEFAULT_DNS_RETRANSMITS 2
 static const unsigned int default_dns_retransmits = DEFAULT_DNS_RETRANSMITS;
-/* TODO: make it configurable */
 #define DEFAULT_LAZY_RESOLVE_TIME 3600.0
 static const double default_lazy_resolve_time = DEFAULT_LAZY_RESOLVE_TIME;
+#define DEFAULT_RESOLVE_MIN_INTERVAL 60.0
+static const double default_resolve_min_interval = DEFAULT_RESOLVE_MIN_INTERVAL;
 
 static const struct upstream_limits default_limits = {
        .revive_time = DEFAULT_REVIVE_TIME,
@@ -169,6 +171,7 @@ static const struct upstream_limits default_limits = {
        .dns_retransmits = DEFAULT_DNS_RETRANSMITS,
        .max_errors = DEFAULT_MAX_ERRORS,
        .lazy_resolve_time = DEFAULT_LAZY_RESOLVE_TIME,
+       .resolve_min_interval = DEFAULT_RESOLVE_MIN_INTERVAL,
 };
 
 static void rspamd_upstream_lazy_resolve_cb(struct ev_loop *, ev_timer *, int);
@@ -199,6 +202,15 @@ void rspamd_upstreams_library_config(struct rspamd_config *cfg,
        if (cfg->dns_timeout) {
                ctx->limits.dns_timeout = cfg->dns_timeout;
        }
+       if (cfg->upstream_resolve_min_interval) {
+               ctx->limits.resolve_min_interval = cfg->upstream_resolve_min_interval;
+       }
+
+       /* Some sanity checks */
+       if (ctx->limits.resolve_min_interval > ctx->limits.revive_time) {
+               /* We must be able to resolve host during the revive time */
+               ctx->limits.resolve_min_interval = ctx->limits.revive_time;
+       }
 
        ctx->event_loop = event_loop;
        ctx->res = resolver;
@@ -641,8 +653,6 @@ static void
 rspamd_upstream_resolve_addrs(const struct upstream_list *ls,
                                                          struct upstream *upstream)
 {
-       /* XXX: maybe make it configurable */
-       static const double min_resolve_interval = 60.0;
 
        if (upstream->ctx->res != NULL &&
                upstream->ctx->configured &&
@@ -651,11 +661,11 @@ rspamd_upstream_resolve_addrs(const struct upstream_list *ls,
 
                double now = ev_now(upstream->ctx->event_loop);
 
-               if (now - upstream->last_resolve < min_resolve_interval) {
+               if (now - upstream->last_resolve < upstream->ctx->limits.resolve_min_interval) {
                        msg_info_upstream("do not resolve upstream %s as it was checked %.0f "
                                                          "seconds ago (%.0f is minimum)",
                                                          upstream->name, now - upstream->last_resolve,
-                                                         min_resolve_interval);
+                                                         upstream->ctx->limits.resolve_min_interval);
 
                        return;
                }
@@ -756,6 +766,7 @@ rspamd_upstream_set_inactive(struct upstream_list *ls, struct upstream *upstream
        struct upstream *cur;
        struct upstream_list_watcher *w;
 
+       g_assert(upstream != NULL);
        RSPAMD_UPSTREAM_LOCK(ls);
        g_ptr_array_remove_index(ls->alive, upstream->active_idx);
        upstream->active_idx = -1;
@@ -806,6 +817,7 @@ void rspamd_upstream_fail(struct upstream *upstream,
        struct upstream_addr_elt *addr_elt;
        struct upstream_list_watcher *w;
 
+       g_assert(upstream != NULL);
        msg_debug_upstream("upstream %s failed; reason: %s",
                                           upstream->name,
                                           reason);
@@ -1652,6 +1664,7 @@ void rspamd_upstream_reresolve(struct upstream_ctx *ctx)
 
        while (cur) {
                up = cur->data;
+               g_assert(up != NULL);
                REF_RETAIN(up);
                rspamd_upstream_resolve_addrs(up->ls, up);
                REF_RELEASE(up);