From bb6169400ef16b3dd4d54b171849b3d7877c651e Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 3 Nov 2014 10:19:13 +0000 Subject: [PATCH] Use upstreams configuration. --- src/libutil/upstream.c | 63 ++++++++++++++++++++++++++++++------------ src/libutil/upstream.h | 15 ++++++++++ 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/src/libutil/upstream.c b/src/libutil/upstream.c index 2ff8f2651..010b2d0ff 100644 --- a/src/libutil/upstream.c +++ b/src/libutil/upstream.c @@ -26,6 +26,7 @@ #include "upstream.h" #include "ottery.h" #include "ref.h" +#include "cfg_file.h" #include "rdns.h" #include "xxhash.h" #include "utlist.h" @@ -68,13 +69,41 @@ struct upstream_list { static struct rdns_resolver *res = NULL; static struct event_base *ev_base = NULL; /* 4 errors in 10 seconds */ -const guint default_max_errors = 4; -const guint default_revive_time = 60; -const gdouble default_revive_jitter = 0.4; -const guint default_error_time = 10; -const gdouble default_dns_timeout = 1.0; -const guint default_dns_retransmits = 2; -const guint default_max_addresses = 1024; +static guint default_max_errors = 4; +static gdouble default_revive_time = 60; +static gdouble default_revive_jitter = 0.4; +static gdouble default_error_time = 10; +static gdouble default_dns_timeout = 1.0; +static guint default_dns_retransmits = 2; +static guint default_max_addresses = 1024; + +void +rspamd_upstreams_library_config (struct rspamd_config *cfg) +{ + if (cfg->upstream_error_time) { + default_error_time = cfg->upstream_error_time; + } + if (cfg->upstream_max_errors) { + default_max_errors = cfg->upstream_max_errors; + } + if (cfg->upstream_revive_time) { + default_revive_time = cfg->upstream_max_errors; + } + if (cfg->dns_retransmits) { + default_dns_retransmits = cfg->dns_retransmits; + } + if (cfg->dns_timeout) { + default_dns_timeout = cfg->dns_timeout; + } +} + +void +rspamd_upstreams_library_init (struct rdns_resolver *resolver, + struct event_base *base) +{ + res = resolver; + ev_base = base; +} static void rspamd_upstream_set_active (struct upstream_list *ls, struct upstream *up) @@ -188,6 +217,8 @@ rspamd_upstream_revive_cb (int fd, short what, void *arg) static void rspamd_upstream_set_inactive (struct upstream_list *ls, struct upstream *up) { + gdouble ntim; + rspamd_mutex_lock (ls->lock); g_ptr_array_remove_index (ls->alive, up->active_idx); up->active_idx = -1; @@ -212,22 +243,14 @@ rspamd_upstream_set_inactive (struct upstream_list *ls, struct upstream *up) event_base_set (ev_base, &up->ev); } - up->tv.tv_sec = default_revive_time + ottery_rand_range ( + ntim = default_revive_time + ottery_rand_range ( default_revive_time * default_revive_jitter); - up->tv.tv_usec = 0; + double_to_tv (ntim, &up->tv); event_add (&up->ev, &up->tv); rspamd_mutex_unlock (ls->lock); } -void -rspamd_upstreams_library_init (struct rdns_resolver *resolver, - struct event_base *base) -{ - res = resolver; - ev_base = base; -} - void rspamd_upstream_fail (struct upstream *up) { @@ -293,6 +316,12 @@ rspamd_upstreams_count (struct upstream_list *ups) return ups->ups->len; } +gsize +rspamd_upstreams_alive (struct upstream_list *ups) +{ + return ups->alive->len; +} + static void rspamd_upstream_dtor (struct upstream *up) { diff --git a/src/libutil/upstream.h b/src/libutil/upstream.h index f6f711034..812cc4bd8 100644 --- a/src/libutil/upstream.h +++ b/src/libutil/upstream.h @@ -13,6 +13,8 @@ enum rspamd_upstream_rotation { RSPAMD_UPSTREAM_MASTER_SLAVE }; + +struct rspamd_config; /* Opaque upstream structures */ struct upstream; struct upstream_list; @@ -24,6 +26,12 @@ struct upstream_list; void rspamd_upstreams_library_init (struct rdns_resolver *resolver, struct event_base *base); +/** + * Configure attributes of upstreams library + * @param cfg + */ +void rspamd_upstreams_library_config (struct rspamd_config *cfg); + /** * Upstream error logic * 1. During error time we count upstream_ok and upstream_fail @@ -60,6 +68,13 @@ void rspamd_upstreams_destroy (struct upstream_list *ups); */ gsize rspamd_upstreams_count (struct upstream_list *ups); +/** + * Returns the number of upstreams in the list + * @param ups + * @return + */ +gsize rspamd_upstreams_alive (struct upstream_list *ups); + /** * Add upstream from the string * @param ups upstream list -- 2.39.5