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