Systemd/src/basic/ratelimit.c

39 lines
748 B
C
Raw Normal View History

/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <sys/time.h>
#include "macro.h"
#include "ratelimit.h"
2010-01-29 04:42:57 +01:00
/* Modelled after Linux' lib/ratelimit.c by Dave Young
* <hidave.darkstar@gmail.com>, which is licensed GPLv2. */
bool ratelimit_below(RateLimit *r) {
2010-05-24 01:45:54 +02:00
usec_t ts;
2010-01-29 04:42:57 +01:00
assert(r);
if (!ratelimit_configured(r))
return true;
ts = now(CLOCK_MONOTONIC);
2010-01-29 04:42:57 +01:00
if (r->begin <= 0 ||
ts - r->begin > r->interval) {
2010-05-24 01:45:54 +02:00
r->begin = ts;
2010-01-29 04:42:57 +01:00
/* Reset counter */
r->num = 0;
2010-01-29 04:42:57 +01:00
goto good;
}
if (r->num < r->burst)
2010-01-29 04:42:57 +01:00
goto good;
return false;
good:
r->num++;
2010-01-29 04:42:57 +01:00
return true;
}