diff options
author | Lennart Poettering <lennart@poettering.net> | 2010-01-29 04:42:57 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2010-01-29 04:42:57 +0100 |
commit | 1e2e81336b2d7691d51a6f3e82df9ae20c22aee1 (patch) | |
tree | eedbc1b5b0d24efcc6437f987efa4fbdf9b5a986 /ratelimit.c | |
parent | c20cae324d93e7fcdb8618e37c3ac377d87edb97 (diff) |
ratelimit start requests
Diffstat (limited to 'ratelimit.c')
-rw-r--r-- | ratelimit.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/ratelimit.c b/ratelimit.c new file mode 100644 index 0000000000..d9eb493496 --- /dev/null +++ b/ratelimit.c @@ -0,0 +1,43 @@ +/*-*- Mode: C; c-basic-offset: 8 -*-*/ + +#include <assert.h> + +#include "ratelimit.h" +#include "log.h" + +/* Modelled after Linux' lib/ratelimit.c by Dave Young + * <hidave.darkstar@gmail.com>, which is licensed GPLv2. */ + +bool ratelimit_test(RateLimit *r) { + usec_t timestamp; + + timestamp = now(CLOCK_MONOTONIC); + + assert(r); + assert(r->interval > 0); + assert(r->burst > 0); + + if (r->begin <= 0 || + r->begin + r->interval < timestamp) { + + if (r->n_missed > 0) + log_warning("%u events suppressed", r->n_missed); + + r->begin = timestamp; + + /* Reset counters */ + r->n_printed = 0; + r->n_missed = 0; + goto good; + } + + if (r->n_printed <= r->burst) + goto good; + + r->n_missed++; + return false; + +good: + r->n_printed++; + return true; +} |