GCC Code Coverage Report


Directory: ./
File: TokenBucket.cpp
Date: 2026-09-03 22:14:09
Exec Total Coverage
Lines: 0 50 0.0%
Functions: 0 11 0.0%
Branches: 0 26 0.0%

Line Branch Exec Source
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 #include <algorithm>
16
17 namespace Utils {
18
19 TokenBucket ::TokenBucket(U32 replenishInterval, U32 maxTokens, U32 replenishRate, U32 startTokens, Fw::Time startTime)
20 : m_replenishInterval(replenishInterval),
21 m_maxTokens(maxTokens),
22 m_replenishRate(replenishRate),
23 m_tokens(startTokens),
24 m_time(startTime) {}
25
26 TokenBucket ::TokenBucket(U32 replenishInterval, U32 maxTokens)
27 : m_replenishInterval(replenishInterval),
28 m_maxTokens(maxTokens),
29 m_replenishRate(1),
30 m_tokens(maxTokens),
31 m_time(0, 0) {
32 FW_ASSERT(this->m_maxTokens <= MAX_TOKEN_BUCKET_TOKENS, static_cast<FwAssertArgType>(this->m_maxTokens));
33 }
34
35 void TokenBucket ::setReplenishInterval(U32 replenishInterval) {
36 this->m_replenishInterval = replenishInterval;
37 }
38
39 void TokenBucket ::setMaxTokens(U32 maxTokens) {
40 this->m_maxTokens = maxTokens;
41 }
42
43 void TokenBucket ::setReplenishRate(U32 replenishRate) {
44 this->m_replenishRate = replenishRate;
45 }
46
47 void TokenBucket ::replenish() {
48 if (this->m_tokens < this->m_maxTokens) {
49 this->m_tokens = this->m_maxTokens;
50 }
51 }
52
53 U32 TokenBucket ::getReplenishInterval() const {
54 return this->m_replenishInterval;
55 }
56
57 U32 TokenBucket ::getMaxTokens() const {
58 return this->m_maxTokens;
59 }
60
61 U32 TokenBucket ::getReplenishRate() const {
62 return this->m_replenishRate;
63 }
64
65 U32 TokenBucket ::getTokens() const {
66 return this->m_tokens;
67 }
68
69 bool TokenBucket ::trigger(const Fw::Time time) {
70 // attempt replenishing
71 if (this->m_replenishRate > 0) {
72 Fw::Time replenishInterval = Fw::Time(this->m_replenishInterval / 1000000, this->m_replenishInterval % 1000000);
73 Fw::Time nextTime = Fw::Time::add(this->m_time, replenishInterval);
74 while (this->m_tokens < this->m_maxTokens && nextTime <= time) {
75 // replenish by replenish rate, or up to maxTokens
76 this->m_tokens += std::min(this->m_replenishRate, this->m_maxTokens - this->m_tokens);
77 this->m_time = nextTime;
78 nextTime = Fw::Time::add(this->m_time, replenishInterval);
79 }
80 if (this->m_tokens >= this->m_maxTokens && this->m_time < time) {
81 this->m_time = time;
82 }
83 }
84
85 // attempt consuming token
86 if (this->m_tokens > 0) {
87 this->m_tokens--;
88 return true;
89
90 } else {
91 return false;
92 }
93 }
94
95 } // end namespace Utils
96