]> git.ipfire.org Git - thirdparty/squid.git/blob - src/FadingCounter.h
Merge from trunk
[thirdparty/squid.git] / src / FadingCounter.h
1 /*
2 * $Id$
3 */
4
5 #ifndef SQUID_FADING_COUNTER_H
6 #define SQUID_FADING_COUNTER_H
7
8 #include "Array.h"
9
10 /// Counts events, forgetting old ones. Usefull for "3 errors/minute" limits.
11 class FadingCounter
12 {
13 public:
14 FadingCounter();
15
16 /// 0=remember nothing; -1=forget nothing; new value triggers clear()
17 void configure(double horizonSeconds);
18
19 void clear(); ///< forgets all events
20
21 int count(int howMany); ///< count fresh, return #events remembered
22 int remembered() const { return total; } ///< possibly stale #events
23
24 /// read-only memory horizon in seconds; older events are forgotten
25 double horizon;
26
27 private:
28 const int precision; ///< #counting slots, controls measur. occuracy
29 double delta; ///< sub-interval duration = horizon/precision
30
31 double lastTime; ///< time of the last update
32 Vector<int> counters; ///< events per delta (possibly stale)
33 int total; ///< number of remembered events (possibly stale)
34 };
35
36 #endif /* SQUID_FADING_COUNTER_H */