1 /* SPDX-License-Identifier: LGPL-2.1-or-later */ 2 3 #include <sys/time.h> 4 5 #include "macro.h" 6 #include "ratelimit.h" 7 8 /* Modelled after Linux' lib/ratelimit.c by Dave Young 9 * <hidave.darkstar@gmail.com>, which is licensed GPLv2. */ 10 ratelimit_below(RateLimit * r)11bool ratelimit_below(RateLimit *r) { 12 usec_t ts; 13 14 assert(r); 15 16 if (!ratelimit_configured(r)) 17 return true; 18 19 ts = now(CLOCK_MONOTONIC); 20 21 if (r->begin <= 0 || 22 usec_sub_unsigned(ts, r->begin) > r->interval) { 23 r->begin = ts; 24 25 /* Reset counter */ 26 r->num = 0; 27 goto good; 28 } 29 30 if (r->num < r->burst) 31 goto good; 32 33 return false; 34 35 good: 36 r->num++; 37 return true; 38 } 39