-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
LeakyBucketCounter.h
91 lines (79 loc) · 2.73 KB
/
LeakyBucketCounter.h
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//==============================================================================
//
// LeakyBucketCounter.h
//
// Copyright (C) 2013-2022 Greg Utas
//
// This file is part of the Robust Services Core (RSC).
//
// RSC is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// RSC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with RSC. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef LEAKYBUCKETCOUNTER_H_INCLUDED
#define LEAKYBUCKETCOUNTER_H_INCLUDED
#include "Object.h"
#include <cstddef>
#include <cstdint>
#include "Duration.h"
#include "SteadyTime.h"
//------------------------------------------------------------------------------
namespace NodeBase
{
// A leaky bucket counter overflows when more than N events occur within S
// seconds. An application uses it to determine if an event has occurred
// more often than allowed. The event is usually a fault whose occasional
// occurrence is acceptable but which points to a more serious problem if
// it occurs with sufficient frequency.
//
class LeakyBucketCounter : public Object
{
public:
// Public so that instances can be declared as members.
//
LeakyBucketCounter();
// Virtual to allow subclassing.
//
virtual ~LeakyBucketCounter();
// Initializes the counter to detect LIMIT events in SECONDS.
//
void Initialize(size_t limit, uint32_t seconds);
// Invoked by the application when an event occurs. Returns
// true if more than LIMIT events have occurred in SECONDS.
//
bool HasReachedLimit();
// Overridden to display member variables.
//
void Display(std::ostream& stream,
const std::string& prefix, const Flags& options) const override;
// Overridden for patching.
//
void Patch(sel_t selector, void* arguments) override;
private:
// The length of the interval during which a threshold number
// of events have to occur to drain the bucket.
//
msecs_t interval_;
// The last time that HasReachedLimit was invoked (i.e. the
// last time than an event occurred).
//
SteadyTime::Point lastTime_;
// The maximum number of events allowed during an interval.
//
size_t limit_;
// Initialized to 0, incremented by an event, and decremented
// at a constant rate (but never dropping below zero).
//
size_t count_;
};
}
#endif