]> git.ipfire.org Git - thirdparty/systemd.git/blob - ratelimit.c
update fixme
[thirdparty/systemd.git] / ratelimit.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #include <assert.h>
4
5 #include "ratelimit.h"
6 #include "log.h"
7
8 /* Modelled after Linux' lib/ratelimit.c by Dave Young
9 * <hidave.darkstar@gmail.com>, which is licensed GPLv2. */
10
11 bool ratelimit_test(RateLimit *r) {
12 usec_t timestamp;
13
14 timestamp = now(CLOCK_MONOTONIC);
15
16 assert(r);
17 assert(r->interval > 0);
18 assert(r->burst > 0);
19
20 if (r->begin <= 0 ||
21 r->begin + r->interval < timestamp) {
22
23 if (r->n_missed > 0)
24 log_warning("%u events suppressed", r->n_missed);
25
26 r->begin = timestamp;
27
28 /* Reset counters */
29 r->n_printed = 0;
30 r->n_missed = 0;
31 goto good;
32 }
33
34 if (r->n_printed <= r->burst)
35 goto good;
36
37 r->n_missed++;
38 return false;
39
40 good:
41 r->n_printed++;
42 return true;
43 }