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