]> git.ipfire.org Git - thirdparty/squid.git/blame - src/FadingCounter.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / FadingCounter.h
CommitLineData
bbc27441 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
bbc27441
AJ
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
8277060a
CT
9#ifndef SQUID_FADING_COUNTER_H
10#define SQUID_FADING_COUNTER_H
11
c8ea3cc0 12#include <vector>
8277060a
CT
13
14/// Counts events, forgetting old ones. Usefull for "3 errors/minute" limits.
a459e80a
A
15class FadingCounter
16{
17public:
18 FadingCounter();
8277060a 19
a459e80a
A
20 /// 0=remember nothing; -1=forget nothing; new value triggers clear()
21 void configure(double horizonSeconds);
8277060a 22
a459e80a 23 void clear(); ///< forgets all events
8277060a 24
a459e80a
A
25 int count(int howMany); ///< count fresh, return #events remembered
26 int remembered() const { return total; } ///< possibly stale #events
8277060a 27
a459e80a
A
28 /// read-only memory horizon in seconds; older events are forgotten
29 double horizon;
8277060a 30
a459e80a
A
31private:
32 const int precision; ///< #counting slots, controls measur. occuracy
33 double delta; ///< sub-interval duration = horizon/precision
8277060a 34
a459e80a 35 double lastTime; ///< time of the last update
c8ea3cc0 36 std::vector<int> counters; ///< events per delta (possibly stale)
a459e80a 37 int total; ///< number of remembered events (possibly stale)
8277060a
CT
38};
39
40#endif /* SQUID_FADING_COUNTER_H */
f53969cc 41