forked from systemd/systemd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c20cae3
commit 1e2e813
Showing
5 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
* <[email protected]>, 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/*-*- Mode: C; c-basic-offset: 8 -*-*/ | ||
|
||
#ifndef fooratelimithfoo | ||
#define fooratelimithfoo | ||
|
||
#include "util.h" | ||
|
||
typedef struct RateLimit { | ||
usec_t interval; | ||
unsigned burst; | ||
unsigned n_printed, n_missed; | ||
usec_t begin; | ||
} RateLimit; | ||
|
||
#define RATELIMIT_DEFINE(_name, _interval, _burst) \ | ||
RateLimit _name = { \ | ||
.interval = (_interval), \ | ||
.burst = (_burst), \ | ||
.n_printed = 0, \ | ||
.n_missed = 0, \ | ||
.begin = 0 \ | ||
} | ||
|
||
#define RATELIMIT_INIT(v, _interval, _burst) \ | ||
do { \ | ||
RateLimit *r = &(v); \ | ||
r->interval = (_interval); \ | ||
r->burst = (_burst); \ | ||
r->n_printed = 0; \ | ||
r->n_missed = 0; \ | ||
r->begin = 0; \ | ||
} while (false); | ||
|
||
bool ratelimit_test(RateLimit *r); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters