LCOV - code coverage report
Current view: top level - basic - ratelimit.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 16 16 100.0 %
Date: 2019-08-22 15:41:25 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       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             : 
      11       10034 : bool ratelimit_below(RateLimit *r) {
      12             :         usec_t ts;
      13             : 
      14       10034 :         assert(r);
      15             : 
      16       10034 :         if (r->interval <= 0 || r->burst <= 0)
      17       10013 :                 return true;
      18             : 
      19          21 :         ts = now(CLOCK_MONOTONIC);
      20             : 
      21          21 :         if (r->begin <= 0 ||
      22          20 :             r->begin + r->interval < ts) {
      23           2 :                 r->begin = ts;
      24             : 
      25             :                 /* Reset counter */
      26           2 :                 r->num = 0;
      27           2 :                 goto good;
      28             :         }
      29             : 
      30          19 :         if (r->num < r->burst)
      31          18 :                 goto good;
      32             : 
      33           1 :         return false;
      34             : 
      35          20 : good:
      36          20 :         r->num++;
      37          20 :         return true;
      38             : }

Generated by: LCOV version 1.14