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