]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/ratelimit.c
Add SPDX license identifiers to source files under the LGPL
[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
6
7 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
a7334b09
LP
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 15 Lesser General Public License for more details.
a7334b09 16
5430f7f2 17 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
1e2e8133 21
11c3a366
TA
22#include <sys/time.h>
23
11c3a366 24#include "macro.h"
93cc7779 25#include "ratelimit.h"
1e2e8133
LP
26
27/* Modelled after Linux' lib/ratelimit.c by Dave Young
28 * <hidave.darkstar@gmail.com>, which is licensed GPLv2. */
29
30bool ratelimit_test(RateLimit *r) {
871d7de4 31 usec_t ts;
1e2e8133 32
1e2e8133 33 assert(r);
cafac610
LP
34
35 if (r->interval <= 0 || r->burst <= 0)
36 return true;
37
38 ts = now(CLOCK_MONOTONIC);
1e2e8133
LP
39
40 if (r->begin <= 0 ||
871d7de4 41 r->begin + r->interval < ts) {
871d7de4 42 r->begin = ts;
1e2e8133 43
4ce9faa9
HH
44 /* Reset counter */
45 r->num = 0;
1e2e8133
LP
46 goto good;
47 }
48
1c008326 49 if (r->num < r->burst)
1e2e8133
LP
50 goto good;
51
1e2e8133
LP
52 return false;
53
54good:
4ce9faa9 55 r->num++;
1e2e8133
LP
56 return true;
57}