File: FadingCounter.h

package info (click to toggle)
squid3 3.4.8-6
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 31,084 kB
  • sloc: cpp: 165,325; ansic: 21,998; sh: 12,166; makefile: 5,964; perl: 2,153; sql: 322; awk: 118
file content (32 lines) | stat: -rw-r--r-- 987 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef SQUID_FADING_COUNTER_H
#define SQUID_FADING_COUNTER_H

#include "base/Vector.h"

/// Counts events, forgetting old ones. Usefull for "3 errors/minute" limits.
class FadingCounter
{
public:
    FadingCounter();

    /// 0=remember nothing; -1=forget nothing; new value triggers clear()
    void configure(double horizonSeconds);

    void clear(); ///< forgets all events

    int count(int howMany); ///< count fresh, return #events remembered
    int remembered() const { return total; } ///< possibly stale #events

    /// read-only memory horizon in seconds; older events are forgotten
    double horizon;

private:
    const int precision; ///< #counting slots, controls measur. occuracy
    double delta; ///< sub-interval duration = horizon/precision

    double lastTime; ///< time of the last update
    Vector<int> counters; ///< events per delta (possibly stale)
    int total; ///< number of remembered events (possibly stale)
};

#endif /* SQUID_FADING_COUNTER_H */