| 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 | 6 | TokenBucket ::TokenBucket(U32 replenishInterval, U32 maxTokens, U32 replenishRate, U32 startTokens, Fw::Time startTime) | |
| 20 | 6 | : m_replenishInterval(replenishInterval), | |
| 21 | 6 | m_maxTokens(maxTokens), | |
| 22 | 6 | m_replenishRate(replenishRate), | |
| 23 | 6 | m_tokens(startTokens), | |
| 24 | 6 | m_time(startTime) {} | |
| 25 | |||
| 26 | 7 | TokenBucket ::TokenBucket(U32 replenishInterval, U32 maxTokens) | |
| 27 | 7 | : m_replenishInterval(replenishInterval), | |
| 28 | 7 | m_maxTokens(maxTokens), | |
| 29 | 7 | m_replenishRate(1), | |
| 30 | 7 | m_tokens(maxTokens), | |
| 31 | 7 | m_time(0, 0) { | |
| 32 | 7 | FW_ASSERT(this->m_maxTokens <= MAX_TOKEN_BUCKET_TOKENS, static_cast<FwAssertArgType>(this->m_maxTokens)); | |
| 33 | 7 | } | |
| 34 | |||
| 35 | 2 | void TokenBucket ::setReplenishInterval(U32 replenishInterval) { | |
| 36 | 2 | this->m_replenishInterval = replenishInterval; | |
| 37 | 2 | } | |
| 38 | |||
| 39 | 1 | void TokenBucket ::setMaxTokens(U32 maxTokens) { | |
| 40 | 1 | this->m_maxTokens = maxTokens; | |
| 41 | 1 | } | |
| 42 | |||
| 43 | 1 | void TokenBucket ::setReplenishRate(U32 replenishRate) { | |
| 44 | 1 | this->m_replenishRate = replenishRate; | |
| 45 | 1 | } | |
| 46 | |||
| 47 | 5 | void TokenBucket ::replenish() { | |
| 48 |
2/2✓ Branch 4 taken 4 times.
✓ Branch 5 taken 1 times.
|
5 | if (this->m_tokens < this->m_maxTokens) { |
| 49 | 4 | this->m_tokens = this->m_maxTokens; | |
| 50 | } | ||
| 51 | 5 | } | |
| 52 | |||
| 53 | 2 | U32 TokenBucket ::getReplenishInterval() const { | |
| 54 | 2 | return this->m_replenishInterval; | |
| 55 | } | ||
| 56 | |||
| 57 | 2 | U32 TokenBucket ::getMaxTokens() const { | |
| 58 | 2 | return this->m_maxTokens; | |
| 59 | } | ||
| 60 | |||
| 61 | 2 | U32 TokenBucket ::getReplenishRate() const { | |
| 62 | 2 | return this->m_replenishRate; | |
| 63 | } | ||
| 64 | |||
| 65 | 900 | U32 TokenBucket ::getTokens() const { | |
| 66 | 900 | return this->m_tokens; | |
| 67 | } | ||
| 68 | |||
| 69 | 5369 | bool TokenBucket ::trigger(const Fw::Time time) { | |
| 70 | // attempt replenishing | ||
| 71 |
2/2✓ Branch 2 taken 5366 times.
✓ Branch 3 taken 3 times.
|
5369 | if (this->m_replenishRate > 0) { |
| 72 | // A stored time in another base (e.g. TB_NONE from the short constructor) is incomparable | ||
| 73 | // with the caller's time, so elapsed time is unknown: restart replenishment from this time | ||
| 74 |
4/4✓ Branch 2 taken 5366 times.
✓ Branch 12 taken 5366 times.
✓ Branch 24 taken 2 times.
✓ Branch 25 taken 5364 times.
|
5366 | if (this->m_time.getTimeBase() != time.getTimeBase()) { |
| 75 |
1/1✓ Branch 6 taken 2 times.
|
2 | this->m_time = time; |
| 76 | } | ||
| 77 | 5366 | const U32 intervalSeconds = this->m_replenishInterval / 1000000; | |
| 78 | 5366 | const U32 intervalUSeconds = this->m_replenishInterval % 1000000; | |
| 79 | // Member add keeps the time base/context of m_time so nextTime stays comparable with time | ||
| 80 |
1/1✓ Branch 5 taken 5366 times.
|
5366 | Fw::Time nextTime = this->m_time; |
| 81 |
1/1✓ Branch 2 taken 5366 times.
|
5366 | nextTime.add(intervalSeconds, intervalUSeconds); |
| 82 |
7/7✓ Branch 4 taken 6476 times.
✓ Branch 5 taken 14 times.
✓ Branch 8 taken 6476 times.
✓ Branch 10 taken 1124 times.
✓ Branch 11 taken 5352 times.
✓ Branch 12 taken 1124 times.
✓ Branch 13 taken 5366 times.
|
6490 | while (this->m_tokens < this->m_maxTokens && nextTime <= time) { |
| 83 | // replenish by replenish rate, or up to maxTokens | ||
| 84 | 1124 | this->m_tokens += std::min(this->m_replenishRate, this->m_maxTokens - this->m_tokens); | |
| 85 |
1/1✓ Branch 6 taken 1124 times.
|
1124 | this->m_time = nextTime; |
| 86 |
1/1✓ Branch 2 taken 1124 times.
|
1124 | nextTime.add(intervalSeconds, intervalUSeconds); |
| 87 | } | ||
| 88 |
7/7✓ Branch 4 taken 14 times.
✓ Branch 5 taken 5352 times.
✓ Branch 12 taken 14 times.
✓ Branch 14 taken 1 times.
✓ Branch 15 taken 13 times.
✓ Branch 16 taken 1 times.
✓ Branch 17 taken 5365 times.
|
5366 | if (this->m_tokens >= this->m_maxTokens && this->m_time < time) { |
| 89 |
1/1✓ Branch 6 taken 1 times.
|
1 | this->m_time = time; |
| 90 | } | ||
| 91 | 5366 | } | |
| 92 | |||
| 93 | // attempt consuming token | ||
| 94 |
2/2✓ Branch 2 taken 2913 times.
✓ Branch 3 taken 2456 times.
|
5369 | if (this->m_tokens > 0) { |
| 95 | 2913 | this->m_tokens--; | |
| 96 | 2913 | return true; | |
| 97 | |||
| 98 | } else { | ||
| 99 | 2456 | return false; | |
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | } // end namespace Utils | ||
| 104 |