ratelimit: handle counter overflows somewhat sanely

An overflow here (i.e. the counter reaching 2^32 within a ratelimit time
window) is not so unlikely. Let's handle this somewhat sanely
and simply stop counting, while remaining in the "limit is hit" state until
the time window has passed.
This commit is contained in:
Lennart Poettering
2023-04-20 13:39:06 +02:00
parent e002b8a28a
commit ed6a6bac45
+11 -9
View File
@@ -10,7 +10,6 @@
bool ratelimit_below(RateLimit *r) {
usec_t ts;
bool good = false;
assert(r);
@@ -21,22 +20,25 @@ bool ratelimit_below(RateLimit *r) {
if (r->begin <= 0 ||
usec_sub_unsigned(ts, r->begin) > r->interval) {
r->begin = ts;
r->begin = ts; /* Start a new time window */
r->num = 1; /* Reset counter */
return true;
}
/* Reset counter */
r->num = 0;
good = true;
} else if (r->num < r->burst)
good = true;
if (_unlikely_(r->num == UINT_MAX))
return false;
r->num++;
return good;
return r->num <= r->burst;
}
unsigned ratelimit_num_dropped(RateLimit *r) {
assert(r);
return r->num > r->burst ? r->num - r->burst : 0;
if (r->num == UINT_MAX) /* overflow, return as special case */
return UINT_MAX;
return LESS_BY(r->num, r->burst);
}
usec_t ratelimit_end(const RateLimit *rl) {