Tor
0.4.7.0-alpha-dev
lib
log
ratelim.h
Go to the documentation of this file.
1
/* Copyright (c) 2003-2004, Roger Dingledine
2
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3
* Copyright (c) 2007-2021, The Tor Project, Inc. */
4
/* See LICENSE for licensing information */
5
6
/**
7
* \file ratelim.h
8
* \brief Summarize similar messages that would otherwise flood the logs.
9
**/
10
11
#ifndef TOR_RATELIM_H
12
#define TOR_RATELIM_H
13
14
#include <
time.h
>
15
16
/* Rate-limiter */
17
18
/** A ratelim_t remembers how often an event is occurring, and how often
19
* it's allowed to occur. Typical usage is something like:
20
*
21
<pre>
22
if (possibly_very_frequent_event()) {
23
const int INTERVAL = 300;
24
static ratelim_t warning_limit = RATELIM_INIT(INTERVAL);
25
char *m;
26
if ((m = rate_limit_log(&warning_limit, approx_time()))) {
27
log_warn(LD_GENERAL, "The event occurred!%s", m);
28
tor_free(m);
29
}
30
}
31
</pre>
32
33
As a convenience wrapper for logging, you can replace the above with:
34
<pre>
35
if (possibly_very_frequent_event()) {
36
static ratelim_t warning_limit = RATELIM_INIT(300);
37
log_fn_ratelim(&warning_limit, LOG_WARN, LD_GENERAL,
38
"The event occurred!");
39
}
40
</pre>
41
*/
42
typedef
struct
ratelim_t
{
43
/** How many seconds must elapse between log messages? */
44
int
rate
;
45
/** When did this limiter last allow a message to appear? */
46
time_t
last_allowed
;
47
/** When did this limiter start suppressing messages? */
48
time_t
started_limiting
;
49
/** How many messages has this limiter suppressed since it last allowed
50
* one to appear? */
51
int
n_calls_since_last_time
;
52
}
ratelim_t
;
53
54
#ifndef COCCI
55
#define RATELIM_INIT(r) { (r), 0, 0, 0 }
56
#endif
57
#define RATELIM_TOOMANY (16*1000*1000)
58
59
char
*
rate_limit_log
(
ratelim_t
*lim, time_t now);
60
61
#endif
/* !defined(TOR_RATELIM_H) */
rate_limit_log
char * rate_limit_log(ratelim_t *lim, time_t now)
Definition:
ratelim.c:42
ratelim_t
Definition:
ratelim.h:42
ratelim_t::n_calls_since_last_time
int n_calls_since_last_time
Definition:
ratelim.h:51
ratelim_t::last_allowed
time_t last_allowed
Definition:
ratelim.h:46
ratelim_t::rate
int rate
Definition:
ratelim.h:44
ratelim_t::started_limiting
time_t started_limiting
Definition:
ratelim.h:48
time.h
Definitions for timing-related constants.
Generated by
1.9.1