ref_entry_t ref;
};
+#ifndef UPSTREAMS_THREAD_SAFE
+#define RSPAMD_UPSTREAM_LOCK(x) do { } while (0)
+#define RSPAMD_UPSTREAM_UNLOCK(x) do { } while (0)
+#else
+#define RSPAMD_UPSTREAM_LOCK(x) rspamd_mutex_lock(x)
+#define RSPAMD_UPSTREAM_UNLOCK(x) rspamd_mutex_unlock(x)
+#endif
+
/* 4 errors in 10 seconds */
static guint default_max_errors = 4;
static gdouble default_revive_time = 60;
static void
rspamd_upstream_set_active (struct upstream_list *ls, struct upstream *up)
{
- rspamd_mutex_lock (ls->lock);
+ RSPAMD_UPSTREAM_LOCK (ls->lock);
g_ptr_array_add (ls->alive, up);
up->active_idx = ls->alive->len - 1;
- rspamd_mutex_unlock (ls->lock);
+ RSPAMD_UPSTREAM_UNLOCK (ls->lock);
}
static void
* We need first of all get the saved port, since DNS gives us no
* idea about what port has been used previously
*/
- rspamd_mutex_lock (up->lock);
+ RSPAMD_UPSTREAM_LOCK (up->lock);
if (up->addrs.addr->len > 0 && up->new_addrs) {
addr_elt = g_ptr_array_index (up->addrs.addr, 0);
}
up->new_addrs = NULL;
- rspamd_mutex_unlock (up->lock);
+ RSPAMD_UPSTREAM_UNLOCK (up->lock);
}
static void
if (reply->code == RDNS_RC_NOERROR) {
entry = reply->entries;
- rspamd_mutex_lock (up->lock);
+ RSPAMD_UPSTREAM_LOCK (up->lock);
while (entry) {
if (entry->type == RDNS_REQUEST_A) {
entry = entry->next;
}
- rspamd_mutex_unlock (up->lock);
+ RSPAMD_UPSTREAM_UNLOCK (up->lock);
}
up->dns_requests--;
{
struct upstream *up = (struct upstream *)arg;
- rspamd_mutex_lock (up->lock);
+ RSPAMD_UPSTREAM_LOCK (up->lock);
event_del (&up->ev);
if (up->ls) {
rspamd_upstream_set_active (up->ls, up);
}
- rspamd_mutex_unlock (up->lock);
+ RSPAMD_UPSTREAM_UNLOCK (up->lock);
REF_RELEASE (up);
}
rspamd_upstream_set_inactive (struct upstream_list *ls, struct upstream *up)
{
gdouble ntim;
+ guint i;
+ struct upstream *cur;
- rspamd_mutex_lock (ls->lock);
+ RSPAMD_UPSTREAM_LOCK (ls->lock);
g_ptr_array_remove_index (ls->alive, up->active_idx);
up->active_idx = -1;
+ /* We need to update all indicies */
+ for (i = 0; i < ls->alive->len; i ++) {
+ cur = g_ptr_array_index (ls->alive, i);
+ cur->active_idx = i;
+ }
+
if (up->ctx->res != NULL && up->ctx->configured &&
!(ls->flags & RSPAMD_UPSTREAM_FLAG_NORESOLVE)) {
/* Resolve name of the upstream one more time */
double_to_tv (ntim, &up->tv);
event_add (&up->ev, &up->tv);
- rspamd_mutex_unlock (ls->lock);
+ RSPAMD_UPSTREAM_UNLOCK (ls->lock);
}
void
gdouble sec_last, sec_cur;
struct upstream_addr_elt *addr_elt;
- gettimeofday (&tv, NULL);
+ if (up->active_idx != -1) {
+ gettimeofday (&tv, NULL);
- rspamd_mutex_lock (up->lock);
- if (up->errors == 0 && up->active_idx != -1) {
- /* We have the first error */
- up->tv = tv;
- up->errors = 1;
- }
- else if (up->active_idx != -1) {
- sec_last = tv_to_double (&up->tv);
- sec_cur = tv_to_double (&tv);
-
- if (sec_cur >= sec_last) {
- up->errors ++;
-
- if (sec_cur > sec_last) {
- error_rate = ((gdouble)up->errors) / (sec_cur - sec_last);
- max_error_rate = ((gdouble)up->ctx->max_errors) / up->ctx->error_time;
- }
- else {
- error_rate = 1;
- max_error_rate = 0;
+ RSPAMD_UPSTREAM_LOCK (up->lock);
+ if (up->errors == 0) {
+ /* We have the first error */
+ up->tv = tv;
+ up->errors = 1;
+ }
+ else {
+ sec_last = tv_to_double (&up->tv);
+ sec_cur = tv_to_double (&tv);
+
+ if (sec_cur >= sec_last) {
+ up->errors ++;
+
+ if (sec_cur > sec_last) {
+ error_rate = ((gdouble)up->errors) / (sec_cur - sec_last);
+ max_error_rate = ((gdouble)up->ctx->max_errors) / up->ctx->error_time;
+ }
+ else {
+ error_rate = 1;
+ max_error_rate = 0;
+ }
+
+ if (error_rate > max_error_rate && up->active_idx != -1) {
+ /* Remove upstream from the active list */
+ up->errors = 0;
+ rspamd_upstream_set_inactive (up->ls, up);
+ }
}
+ }
- if (error_rate > max_error_rate && up->active_idx != -1) {
- /* Remove upstream from the active list */
- up->errors = 0;
- rspamd_upstream_set_inactive (up->ls, up);
- }
+ /* Also increase count of errors for this specific address */
+ if (up->addrs.addr) {
+ addr_elt = g_ptr_array_index (up->addrs.addr, up->addrs.cur);
+ addr_elt->errors ++;
}
- }
- /* Also increase count of errors for this specific address */
- if (up->addrs.addr) {
- addr_elt = g_ptr_array_index (up->addrs.addr, up->addrs.cur);
- addr_elt->errors ++;
+ RSPAMD_UPSTREAM_UNLOCK (up->lock);
}
-
- rspamd_mutex_unlock (up->lock);
}
void
{
struct upstream_addr_elt *addr_elt;
- rspamd_mutex_lock (up->lock);
+ RSPAMD_UPSTREAM_LOCK (up->lock);
if (up->errors > 0 && up->active_idx != -1) {
/* We touch upstream if and only if it is active */
up->errors = 0;
}
}
- rspamd_mutex_unlock (up->lock);
+ RSPAMD_UPSTREAM_UNLOCK (up->lock);
}
#define SEED_CONSTANT 0xa574de7df64e9b9dULL
struct upstream_list *ups = (struct upstream_list *)ls;
/* Here the upstreams list is already locked */
- rspamd_mutex_lock (up->lock);
+ RSPAMD_UPSTREAM_LOCK (up->lock);
event_del (&up->ev);
g_ptr_array_add (ups->alive, up);
up->active_idx = ups->alive->len - 1;
- rspamd_mutex_unlock (up->lock);
+ RSPAMD_UPSTREAM_UNLOCK (up->lock);
/* For revive event */
REF_RELEASE (up);
}
guint i;
/* Select upstream with the maximum cur_weight */
- rspamd_mutex_lock (ups->lock);
+ RSPAMD_UPSTREAM_LOCK (ups->lock);
for (i = 0; i < ups->alive->len; i ++) {
up = g_ptr_array_index (ups->alive, i);
if (use_cur) {
}
}
- rspamd_mutex_unlock (ups->lock);
+ RSPAMD_UPSTREAM_UNLOCK (ups->lock);
return selected;
}
k = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64,
key, keylen, ups->hash_seed);
- rspamd_mutex_lock (ups->lock);
+ RSPAMD_UPSTREAM_LOCK (ups->lock);
idx = rspamd_consistent_hash (k, ups->alive->len);
- rspamd_mutex_unlock (ups->lock);
+ RSPAMD_UPSTREAM_UNLOCK (ups->lock);
return g_ptr_array_index (ups->alive, idx);
}
{
enum rspamd_upstream_rotation type;
- rspamd_mutex_lock (ups->lock);
+ RSPAMD_UPSTREAM_LOCK (ups->lock);
if (ups->alive->len == 0) {
/* We have no upstreams alive */
g_ptr_array_foreach (ups->ups, rspamd_upstream_restore_cb, ups);
}
- rspamd_mutex_unlock (ups->lock);
+ RSPAMD_UPSTREAM_UNLOCK (ups->lock);
if (!forced) {
type = ups->rot_alg != RSPAMD_UPSTREAM_UNDEF ? ups->rot_alg : default_type;