]> git.ipfire.org Git - thirdparty/squid.git/blob - src/FadingCounter.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / FadingCounter.h
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_FADING_COUNTER_H
10 #define SQUID_FADING_COUNTER_H
11
12 #include <vector>
13
14 /// Counts events, forgetting old ones. Usefull for "3 errors/minute" limits.
15 class FadingCounter
16 {
17 public:
18 FadingCounter();
19
20 /// 0=remember nothing; -1=forget nothing; new value triggers clear()
21 void configure(double horizonSeconds);
22
23 void clear(); ///< forgets all events
24
25 int count(int howMany); ///< count fresh, return #events remembered
26 int remembered() const { return total; } ///< possibly stale #events
27
28 /// read-only memory horizon in seconds; older events are forgotten
29 double horizon;
30
31 private:
32 const int precision; ///< #counting slots, controls measur. occuracy
33 double delta; ///< sub-interval duration = horizon/precision
34
35 double lastTime; ///< time of the last update
36 std::vector<int> counters; ///< events per delta (possibly stale)
37 int total; ///< number of remembered events (possibly stale)
38 };
39
40 #endif /* SQUID_FADING_COUNTER_H */
41