2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2010-02-03 13:03:47 +01:00
|
|
|
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include "macro.h"
|
2015-12-01 23:22:03 +01:00
|
|
|
#include "ratelimit.h"
|
2010-01-29 04:42:57 +01:00
|
|
|
|
|
|
|
/* Modelled after Linux' lib/ratelimit.c by Dave Young
|
|
|
|
* <hidave.darkstar@gmail.com>, which is licensed GPLv2. */
|
|
|
|
|
2018-05-11 11:16:52 +02:00
|
|
|
bool ratelimit_below(RateLimit *r) {
|
2010-05-24 01:45:54 +02:00
|
|
|
usec_t ts;
|
2010-01-29 04:42:57 +01:00
|
|
|
|
|
|
|
assert(r);
|
2012-02-08 21:35:49 +01:00
|
|
|
|
2020-10-08 18:40:35 +02:00
|
|
|
if (!ratelimit_configured(r))
|
2012-02-08 21:35:49 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
ts = now(CLOCK_MONOTONIC);
|
2010-01-29 04:42:57 +01:00
|
|
|
|
|
|
|
if (r->begin <= 0 ||
|
2020-11-12 11:09:54 +01:00
|
|
|
ts - r->begin > r->interval) {
|
2010-05-24 01:45:54 +02:00
|
|
|
r->begin = ts;
|
2010-01-29 04:42:57 +01:00
|
|
|
|
2011-02-21 15:32:18 +01:00
|
|
|
/* Reset counter */
|
|
|
|
r->num = 0;
|
2010-01-29 04:42:57 +01:00
|
|
|
goto good;
|
|
|
|
}
|
|
|
|
|
2013-02-13 08:00:46 +01:00
|
|
|
if (r->num < r->burst)
|
2010-01-29 04:42:57 +01:00
|
|
|
goto good;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
good:
|
2011-02-21 15:32:18 +01:00
|
|
|
r->num++;
|
2010-01-29 04:42:57 +01:00
|
|
|
return true;
|
|
|
|
}
|