]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/ratelimit.c
Turn VALGRIND variable into a meson configuration switch
[thirdparty/systemd.git] / src / basic / ratelimit.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <sys/time.h>
9
10 #include "macro.h"
11 #include "ratelimit.h"
12
13 /* Modelled after Linux' lib/ratelimit.c by Dave Young
14 * <hidave.darkstar@gmail.com>, which is licensed GPLv2. */
15
16 bool ratelimit_test(RateLimit *r) {
17 usec_t ts;
18
19 assert(r);
20
21 if (r->interval <= 0 || r->burst <= 0)
22 return true;
23
24 ts = now(CLOCK_MONOTONIC);
25
26 if (r->begin <= 0 ||
27 r->begin + r->interval < ts) {
28 r->begin = ts;
29
30 /* Reset counter */
31 r->num = 0;
32 goto good;
33 }
34
35 if (r->num < r->burst)
36 goto good;
37
38 return false;
39
40 good:
41 r->num++;
42 return true;
43 }