F´ Flight Software - C/C++ Documentation NASA-v1.6.0
A framework for building embedded system applications to NASA flight quality standards.
Loading...
Searching...
No Matches
TokenBucket.cpp
Go to the documentation of this file.
1// ======================================================================
2// \title TokenBucket.cpp
3// \author vwong
4// \brief cpp file for a rate limiter utility class
5//
6// \copyright
7//
8// Copyright (C) 2009-2020 California Institute of Technology.
9//
10// ALL RIGHTS RESERVED. United States Government Sponsorship
11// acknowledged.
12// ======================================================================
13
14#include <Utils/TokenBucket.hpp>
15
16namespace Utils {
17
18 TokenBucket ::
19 TokenBucket (
20 U32 replenishInterval,
21 U32 maxTokens,
22 U32 replenishRate,
23 U32 startTokens,
24 Fw::Time startTime
25 ) :
26 m_replenishInterval(replenishInterval),
27 m_maxTokens(maxTokens),
28 m_replenishRate(replenishRate),
29 m_tokens(startTokens),
30 m_time(startTime)
31 {
32 }
33
34 TokenBucket ::
35 TokenBucket (
36 U32 replenishInterval,
37 U32 maxTokens
38 ) :
39 m_replenishInterval(replenishInterval),
40 m_maxTokens(maxTokens),
41 m_replenishRate(1),
42 m_tokens(maxTokens),
43 m_time(0, 0)
44 {
45 FW_ASSERT(this->m_maxTokens <= MAX_TOKEN_BUCKET_TOKENS, this->m_maxTokens);
46 }
47
48 void TokenBucket ::
49 setReplenishInterval(
50 U32 replenishInterval
51 )
52 {
53 this->m_replenishInterval = replenishInterval;
54 }
55
56 void TokenBucket ::
57 setMaxTokens(
58 U32 maxTokens
59 )
60 {
61 this->m_maxTokens = maxTokens;
62 }
63
64 void TokenBucket ::
65 setReplenishRate(
66 U32 replenishRate
67 )
68 {
69 this->m_replenishRate = replenishRate;
70 }
71
72 void TokenBucket ::
73 replenish()
74 {
75 if (this->m_tokens < this->m_maxTokens) {
76 this->m_tokens = this->m_maxTokens;
77 }
78 }
79
80 U32 TokenBucket ::
81 getReplenishInterval() const
82 {
83 return this->m_replenishInterval;
84 }
85
86 U32 TokenBucket ::
87 getMaxTokens() const
88 {
89 return this->m_maxTokens;
90 }
91
92 U32 TokenBucket ::
93 getReplenishRate() const
94 {
95 return this->m_replenishRate;
96 }
97
98 U32 TokenBucket ::
99 getTokens() const
100 {
101 return this->m_tokens;
102 }
103
104 bool TokenBucket ::
105 trigger(
106 const Fw::Time time
107 )
108 {
109 // attempt replenishing
110 if (this->m_replenishRate > 0) {
111 Fw::Time replenishInterval = Fw::Time(this->m_replenishInterval / 1000000, this->m_replenishInterval % 1000000);
112 Fw::Time nextTime = Fw::Time::add(this->m_time, replenishInterval);
113 while (this->m_tokens < this->m_maxTokens && nextTime <= time) {
114 // replenish by replenish rate, or up to maxTokens
115 this->m_tokens += FW_MIN(this->m_replenishRate, this->m_maxTokens - this->m_tokens);
116 this->m_time = nextTime;
117 nextTime = Fw::Time::add(this->m_time, replenishInterval);
118 }
119 if (this->m_tokens >= this->m_maxTokens && this->m_time < time) {
120 this->m_time = time;
121 }
122 }
123
124 // attempt consuming token
125 if (this->m_tokens > 0) {
126 this->m_tokens--;
127 return true;
128
129 } else {
130 return false;
131 }
132 }
133
134} // end namespace Utils
#define FW_ASSERT(...)
Definition Assert.hpp:7
#define FW_MIN(a, b)
MIN macro.
Definition BasicTypes.h:68
static Time add(const Time &a, const Time &b)
Definition Time.cpp:193