F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
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 
16 namespace Utils {
17 
18 TokenBucket ::TokenBucket(U32 replenishInterval, U32 maxTokens, U32 replenishRate, U32 startTokens, Fw::Time startTime)
19  : m_replenishInterval(replenishInterval),
20  m_maxTokens(maxTokens),
21  m_replenishRate(replenishRate),
22  m_tokens(startTokens),
23  m_time(startTime) {}
24 
25 TokenBucket ::TokenBucket(U32 replenishInterval, U32 maxTokens)
26  : m_replenishInterval(replenishInterval),
27  m_maxTokens(maxTokens),
28  m_replenishRate(1),
29  m_tokens(maxTokens),
30  m_time(0, 0) {
31  FW_ASSERT(this->m_maxTokens <= MAX_TOKEN_BUCKET_TOKENS, static_cast<FwAssertArgType>(this->m_maxTokens));
32 }
33 
34 void TokenBucket ::setReplenishInterval(U32 replenishInterval) {
35  this->m_replenishInterval = replenishInterval;
36 }
37 
38 void TokenBucket ::setMaxTokens(U32 maxTokens) {
39  this->m_maxTokens = maxTokens;
40 }
41 
42 void TokenBucket ::setReplenishRate(U32 replenishRate) {
43  this->m_replenishRate = replenishRate;
44 }
45 
47  if (this->m_tokens < this->m_maxTokens) {
48  this->m_tokens = this->m_maxTokens;
49  }
50 }
51 
53  return this->m_replenishInterval;
54 }
55 
57  return this->m_maxTokens;
58 }
59 
61  return this->m_replenishRate;
62 }
63 
65  return this->m_tokens;
66 }
67 
68 bool TokenBucket ::trigger(const Fw::Time time) {
69  // attempt replenishing
70  if (this->m_replenishRate > 0) {
71  Fw::Time replenishInterval = Fw::Time(this->m_replenishInterval / 1000000, this->m_replenishInterval % 1000000);
72  Fw::Time nextTime = Fw::Time::add(this->m_time, replenishInterval);
73  while (this->m_tokens < this->m_maxTokens && nextTime <= time) {
74  // replenish by replenish rate, or up to maxTokens
75  this->m_tokens += FW_MIN(this->m_replenishRate, this->m_maxTokens - this->m_tokens);
76  this->m_time = nextTime;
77  nextTime = Fw::Time::add(this->m_time, replenishInterval);
78  }
79  if (this->m_tokens >= this->m_maxTokens && this->m_time < time) {
80  this->m_time = time;
81  }
82  }
83 
84  // attempt consuming token
85  if (this->m_tokens > 0) {
86  this->m_tokens--;
87  return true;
88 
89  } else {
90  return false;
91  }
92 }
93 
94 } // end namespace Utils
void setReplenishInterval(U32 replenishInterval)
Definition: TokenBucket.cpp:34
U32 getReplenishRate() const
Definition: TokenBucket.cpp:60
#define MAX_TOKEN_BUCKET_TOKENS
Definition: TokenBucket.hpp:20
U32 getReplenishInterval() const
Definition: TokenBucket.cpp:52
TokenBucket(U32 replenishInterval, U32 maxTokens, U32 replenishRate, U32 startTokens, Fw::Time startTime)
Definition: TokenBucket.cpp:18
bool trigger(const Fw::Time time)
Definition: TokenBucket.cpp:68
U32 getMaxTokens() const
Definition: TokenBucket.cpp:56
void setReplenishRate(U32 replenishRate)
Definition: TokenBucket.cpp:42
void setMaxTokens(U32 maxTokens)
Definition: TokenBucket.cpp:38
#define FW_MIN(a, b)
MIN macro.
Definition: BasicTypes.h:92
U32 getTokens() const
Definition: TokenBucket.cpp:64
static Time add(const Time &a, const Time &b)
Definition: Time.cpp:134
#define FW_ASSERT(...)
Definition: Assert.hpp:14