diff options
author | Nick Mathewson <nickm@torproject.org> | 2010-08-18 15:55:49 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2010-08-18 15:55:49 -0400 |
commit | ba9c1275c4b2325aed6a2fe2cc236794dab11052 (patch) | |
tree | 27be4318e4d65bff4e312a5476bcc184d69aa6cf /src/common | |
parent | d72edc4b78f0104823d156e605259d9d41e6f54c (diff) | |
download | tor-ba9c1275c4b2325aed6a2fe2cc236794dab11052.tar.gz tor-ba9c1275c4b2325aed6a2fe2cc236794dab11052.zip |
Add a generic rate-limited log mechanism, and use it in a few places
Incidentally fixes bug 1042.
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/util.c | 43 | ||||
-rw-r--r-- | src/common/util.h | 28 |
2 files changed, 71 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c index 6830ef3aa4..3c735e9c7c 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -1578,6 +1578,49 @@ ftime_definitely_before(time_t now, time_t when) } /* ===== + * Rate limiting + * ===== */ + +/** If the rate-limiter <b>lim</b> is ready at <b>now</b>, return the number + * of calls to rate_limit_is_ready (including this one!) since the last time + * rate_limit_is_ready returned nonzero. Otherwise return 0. */ +int +rate_limit_is_ready(ratelim_t *lim, time_t now) +{ + if (lim->rate + lim->last_allowed >= now) { + int res = lim->n_calls_since_last_time + 1; + lim->last_allowed = now; + lim->n_calls_since_last_time = 0; + return res; + } else { + ++lim->n_calls_since_last_time; + return 0; + } +} + +/** If the rate-limiter <b>lim</b> is ready at <b>now</b>, return a newly + * allocated string indicating how many messages were suppressed, suitable to + * append to a log message. Otherwise return NULL. */ +char * +rate_limit_log(ratelim_t *lim, time_t now) +{ + int n; + if ((n = rate_limit_is_ready(lim, now))) { + if (n == 1) { + return tor_strdup(""); + } else { + char *cp=NULL; + tor_asprintf(&cp, + " [%d similar message(s) suppressed in last %d seconds]", + n-1, lim->rate); + return cp; + } + } else { + return NULL; + } +} + +/* ===== * File helpers * ===== */ diff --git a/src/common/util.h b/src/common/util.h index ba38f4c7ed..1e22bd5b1b 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -252,6 +252,34 @@ int ftime_maybe_before(time_t now, time_t when); int ftime_definitely_after(time_t now, time_t when); int ftime_definitely_before(time_t now, time_t when); +/* Rate-limiter */ + +/** A ratelim_t remembers how often an event is occurring, and how often + * it's allowed to occur. Typical usage is something like: + * + <pre> + if (possibly_very_frequent_event()) { + const int INTERVAL = 300; + static ratelim_t warning_limit = RATELIM_INIT(INTERVAL); + char *m; + if ((m = rate_limit_log(&warning_limit, approx_time()))) { + log_warn(LD_GENERAL, "The event occurred!%s", m); + tor_free(m); + } + } + </pre> + */ +typedef struct ratelim_t { + int rate; + time_t last_allowed; + int n_calls_since_last_time; +} ratelim_t; + +#define RATELIM_INIT(r) { (r), 0, 0 } + +int rate_limit_is_ready(ratelim_t *lim, time_t now); +char *rate_limit_log(ratelim_t *lim, time_t now); + /* File helpers */ ssize_t write_all(int fd, const char *buf, size_t count, int isSocket); ssize_t read_all(int fd, char *buf, size_t count, int isSocket); |