fix flood protection (#19358)
Costa Tsaousis committed
Jan 9, 2025 at 13:29 UTC
d83b6280d0c18e4c7a7571fe62c624815bf2d96e
2 files changed
+21
-6
src/libnetdata/log/nd_log_limit.c
+7
-4
@@ -32,13 +32,16 @@ bool nd_log_limit_reached(struct nd_log_source *source) {
32
if(source->limits.throttle_period == 0 || source->limits.logs_per_period == 0)
33
return false;
34
35
+ spinlock_lock(&source->limits.spinlock);
36
+
37
usec_t now_ut = now_monotonic_usec();
38
if(!source->limits.started_monotonic_ut)
39
source->limits.started_monotonic_ut = now_ut;
40
41
source->limits.counter++;
42
41
- if(now_ut - source->limits.started_monotonic_ut > (usec_t)source->limits.throttle_period) {
43
+ // Check if we need to reset the period
44
+ if(now_ut - source->limits.started_monotonic_ut > (usec_t)source->limits.throttle_period * USEC_PER_SEC) {
45
if(source->limits.prevented) {
46
BUFFER *wb = buffer_create(1024, NULL);
47
buffer_sprintf(wb,
@@ -51,7 +54,6 @@ bool nd_log_limit_reached(struct nd_log_source *source) {
54
freez((void *)source->pending_msg);
55
56
source->pending_msg = strdupz(buffer_tostring(wb));
54
-
57
buffer_free(wb);
58
}
59
@@ -60,7 +62,7 @@ bool nd_log_limit_reached(struct nd_log_source *source) {
62
source->limits.counter = 1;
63
source->limits.prevented = 0;
64
63
- // log this error
65
+ spinlock_unlock(&source->limits.spinlock);
66
return false;
67
}
68
@@ -82,11 +84,11 @@ bool nd_log_limit_reached(struct nd_log_source *source) {
84
freez((void *)source->pending_msg);
85
86
source->pending_msg = strdupz(buffer_tostring(wb));
85
-
87
buffer_free(wb);
88
}
89
90
source->limits.prevented++;
91
+ spinlock_unlock(&source->limits.spinlock);
92
93
// prevent logging this error
94
#ifdef NETDATA_INTERNAL_CHECKS
@@ -96,5 +98,6 @@ bool nd_log_limit_reached(struct nd_log_source *source) {
98
#endif
99
}
100
101
+ spinlock_unlock(&source->limits.spinlock);
102
return false;
103
}
src/libnetdata/log/nd_log_limit.h
+14
-2
@@ -9,6 +9,7 @@ struct nd_log_source;
9
bool nd_log_limit_reached(struct nd_log_source *source);
10
11
struct nd_log_limit {
12
+ SPINLOCK spinlock;
13
usec_t started_monotonic_ut;
14
uint32_t counter;
15
uint32_t prevented;
@@ -18,8 +19,19 @@ struct nd_log_limit {
19
uint32_t logs_per_period_backup;
20
};
21
21
-#define ND_LOG_LIMITS_DEFAULT (struct nd_log_limit){ .logs_per_period = ND_LOG_DEFAULT_THROTTLE_LOGS, .logs_per_period_backup = ND_LOG_DEFAULT_THROTTLE_LOGS, .throttle_period = ND_LOG_DEFAULT_THROTTLE_PERIOD, }
22
-#define ND_LOG_LIMITS_UNLIMITED (struct nd_log_limit){ .logs_per_period = 0, .logs_per_period_backup = 0, .throttle_period = 0, }
22
+#define ND_LOG_LIMITS_DEFAULT (struct nd_log_limit){ \
23
+ .spinlock = SPINLOCK_INITIALIZER, \
24
+ .logs_per_period = ND_LOG_DEFAULT_THROTTLE_LOGS, \
25
+ .logs_per_period_backup = ND_LOG_DEFAULT_THROTTLE_LOGS, \
26
+ .throttle_period = ND_LOG_DEFAULT_THROTTLE_PERIOD, \
27
+}
28
+
29
+#define ND_LOG_LIMITS_UNLIMITED (struct nd_log_limit){ \
30
+ .spinlock = SPINLOCK_INITIALIZER, \
31
+ .logs_per_period = 0, \
32
+ .logs_per_period_backup = 0, \
33
+ .throttle_period = 0, \
34
+}
35
36
#include "nd_log-internals.h"
37