]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/ratelimit.c
libudev: hide definition of struct udev_list from other libudev components
[thirdparty/systemd.git] / src / basic / ratelimit.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
a7334b09 2
11c3a366
TA
3#include <sys/time.h>
4
11c3a366 5#include "macro.h"
93cc7779 6#include "ratelimit.h"
1e2e8133
LP
7
8/* Modelled after Linux' lib/ratelimit.c by Dave Young
9 * <hidave.darkstar@gmail.com>, which is licensed GPLv2. */
10
7994ac1d 11bool ratelimit_below(RateLimit *r) {
871d7de4 12 usec_t ts;
1e2e8133 13
1e2e8133 14 assert(r);
cafac610
LP
15
16 if (r->interval <= 0 || r->burst <= 0)
17 return true;
18
19 ts = now(CLOCK_MONOTONIC);
1e2e8133
LP
20
21 if (r->begin <= 0 ||
871d7de4 22 r->begin + r->interval < ts) {
871d7de4 23 r->begin = ts;
1e2e8133 24
4ce9faa9
HH
25 /* Reset counter */
26 r->num = 0;
1e2e8133
LP
27 goto good;
28 }
29
1c008326 30 if (r->num < r->burst)
1e2e8133
LP
31 goto good;
32
1e2e8133
LP
33 return false;
34
35good:
4ce9faa9 36 r->num++;
1e2e8133
LP
37 return true;
38}