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