summaryrefslogtreecommitdiffstats
path: root/src/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index 19afe0611..7d636b843 100644
--- a/src/util.c
+++ b/src/util.c
@@ -29,6 +29,7 @@
#include "main.h"
sig_atomic_t do_reopen_log = 0;
+extern rspamd_hash_t *counters;
int
make_socket_nonblocking (int fd)
@@ -811,6 +812,33 @@ calculate_check_time (struct timespec *begin, int resolution)
return (const char *)res;
}
+void
+set_counter (const char *name, long int value)
+{
+ struct counter_data *cd;
+ double alpha;
+ char *key;
+
+ cd = rspamd_hash_lookup (counters, (gpointer)name);
+
+ if (cd == NULL) {
+ cd = memory_pool_alloc_shared (counters->pool, sizeof (struct counter_data));
+ cd->value = value;
+ cd->number = 1;
+ key = memory_pool_strdup_shared (counters->pool, name);
+ rspamd_hash_insert (counters, (gpointer)key, (gpointer)cd);
+ }
+ else {
+ /* Calculate new value */
+ memory_pool_wlock_rwlock (counters->lock);
+
+ alpha = 2. / (++cd->number + 1);
+ cd->value = cd->value * (1. - alpha) + value * alpha;
+
+ memory_pool_wunlock_rwlock (counters->lock);
+ }
+}
+
/*
* vi:ts=4
*/