]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/ratelimit.c
tree-wide: drop license boilerplate
[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
9 #include <sys/time.h>
10
11 #include "macro.h"
12 #include "ratelimit.h"
13
14 /* Modelled after Linux' lib/ratelimit.c by Dave Young
15 * <hidave.darkstar@gmail.com>, which is licensed GPLv2. */
16
17 bool ratelimit_test(RateLimit *r) {
18 usec_t ts;
19
20 assert(r);
21
22 if (r->interval <= 0 || r->burst <= 0)
23 return true;
24
25 ts = now(CLOCK_MONOTONIC);
26
27 if (r->begin <= 0 ||
28 r->begin + r->interval < ts) {
29 r->begin = ts;
30
31 /* Reset counter */
32 r->num = 0;
33 goto good;
34 }
35
36 if (r->num < r->burst)
37 goto good;
38
39 return false;
40
41 good:
42 r->num++;
43 return true;
44 }