mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-26 18:55:38 +08:00
refs #806, minor adjustments of token bucket
This commit is contained in:
@@ -14,6 +14,9 @@
|
|||||||
|
|
||||||
namespace BlackCore
|
namespace BlackCore
|
||||||
{
|
{
|
||||||
|
CTokenBucket::CTokenBucket(int capacity, BlackMisc::PhysicalQuantities::CTime interval, int numTokensToRefill)
|
||||||
|
: m_capacity(capacity), m_interval(interval), m_numTokensToRefill(numTokensToRefill) {}
|
||||||
|
|
||||||
bool CTokenBucket::tryConsume(int numTokens)
|
bool CTokenBucket::tryConsume(int numTokens)
|
||||||
{
|
{
|
||||||
Q_ASSERT(numTokens > 0 && numTokens < m_capacity);
|
Q_ASSERT(numTokens > 0 && numTokens < m_capacity);
|
||||||
@@ -33,15 +36,24 @@ namespace BlackCore
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CTokenBucket::getTokens()
|
void CTokenBucket::setNumberOfTokensToRefill(int noTokens)
|
||||||
{
|
{
|
||||||
auto now = QDateTime::currentDateTime();
|
m_numTokensToRefill = noTokens;
|
||||||
auto deltaSeconds = m_lastReplenishmentTime.secsTo(now);
|
|
||||||
int numberOfTokens = static_cast<int>( m_numTokensToRefill * deltaSeconds / m_interval.value(BlackMisc::PhysicalQuantities::CTimeUnit::s()));
|
|
||||||
|
|
||||||
// Update the time only when replenishment actually took place. We will end up in a infinite loop otherwise.
|
|
||||||
if (numberOfTokens > 0 ) m_lastReplenishmentTime = now;
|
|
||||||
return numberOfTokens;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CTokenBucket::setCapacity(int capacity)
|
||||||
|
{
|
||||||
|
m_capacity = capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
int CTokenBucket::getTokens()
|
||||||
|
{
|
||||||
|
const auto now = QDateTime::currentDateTime();
|
||||||
|
const auto deltaSeconds = m_lastReplenishmentTime.secsTo(now);
|
||||||
|
const int numberOfTokens = static_cast<int>(m_numTokensToRefill * deltaSeconds / m_interval.value(BlackMisc::PhysicalQuantities::CTimeUnit::s()));
|
||||||
|
|
||||||
|
// Update the time only when replenishment actually took place. We will end up in a infinite loop otherwise.
|
||||||
|
if (numberOfTokens > 0) { m_lastReplenishmentTime = now; }
|
||||||
|
return numberOfTokens;
|
||||||
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
@@ -12,52 +12,45 @@
|
|||||||
|
|
||||||
#include "blackcore/blackcoreexport.h"
|
#include "blackcore/blackcoreexport.h"
|
||||||
#include "blackmisc/pq/time.h"
|
#include "blackmisc/pq/time.h"
|
||||||
|
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
|
||||||
namespace BlackCore
|
namespace BlackCore
|
||||||
{
|
{
|
||||||
/*!
|
/*!
|
||||||
* Token bucket algorithm
|
* \brief Token bucket algorithm
|
||||||
*
|
* \details This class implements the token bucket algorithm. Tokens as arbitrary unit are added to the bucket at a defined rate.
|
||||||
* This class implements the token bucket algorithm. Tokens as arbitrary unit are added to the bucket at a defined rate.
|
* Token can be consumsed as long as there are enough available. This class can be used to throttle traffic and packet
|
||||||
* Token can be consumsed as long as there are enough available. This class can be used to throttle traffic and packet
|
* generation. Each time a packet needs to be generated and sent a token is consumed. If no token is available, consumption
|
||||||
* generation. Each time a packet needs to be generated and sent a token is consumed. If no token is available, consumption
|
* will fail the the packet cannot be sent.
|
||||||
* will fail the the packet cannot be sent.
|
|
||||||
*/
|
*/
|
||||||
class BLACKCORE_EXPORT CTokenBucket
|
class BLACKCORE_EXPORT CTokenBucket
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
//! Constructor for given replenishment policy
|
||||||
//! Constructor
|
CTokenBucket(int capacity, BlackMisc::PhysicalQuantities::CTime interval, int numTokensToRefill);
|
||||||
//! \tparam Replenishment policy
|
|
||||||
CTokenBucket(int capacity, BlackMisc::PhysicalQuantities::CTime interval, int numTokensToRefill)
|
|
||||||
: m_capacity(capacity), m_interval(interval), m_numTokensToRefill(numTokensToRefill) {}
|
|
||||||
|
|
||||||
//! Try to consume a token
|
|
||||||
bool tryConsume()
|
|
||||||
{
|
|
||||||
return tryConsume(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
//! Try to consume a number of tokens
|
//! Try to consume a number of tokens
|
||||||
bool tryConsume(int numTokens);
|
bool tryConsume(int numTokens = 1);
|
||||||
|
|
||||||
|
//! Number of tokens to refill
|
||||||
|
void setNumberOfTokensToRefill(int noTokens);
|
||||||
|
|
||||||
|
//! Set the capacity
|
||||||
|
void setCapacity(int capacity);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
//! Get available tokens since last replenishment.
|
//! Get available tokens since last replenishment.
|
||||||
// Note that replenishment is implemented lazy. This means, tokens will not replenished
|
//! \note Note that replenishment is implemented lazy.
|
||||||
// on regular basis via a running timer, but they will be replenished while trying to consume
|
//! This means, tokens will not replenished on regular basis via a running timer,
|
||||||
// them.
|
//! but they will be replenished while trying to consume them.
|
||||||
int getTokens();
|
int getTokens();
|
||||||
|
|
||||||
int m_capacity = 10; //!< Maximum capacity of tokens
|
int m_capacity = 10; //!< Maximum capacity of tokens
|
||||||
int m_availableTokens = 10; //!< Currently available tokens. The initial value is 10
|
int m_availableTokens = 10; //!< Currently available tokens. The initial value is 10
|
||||||
BlackMisc::PhysicalQuantities::CTime m_interval; //!< Refill interval, e.g. every 5 secs
|
BlackMisc::PhysicalQuantities::CTime m_interval; //!< Refill interval, e.g. every 5 secs
|
||||||
int m_numTokensToRefill; //!< Number of tokens to be refilled each interval
|
int m_numTokensToRefill; //!< Number of tokens to be refilled each interval
|
||||||
QDateTime m_lastReplenishmentTime = QDateTime::currentDateTime(); //!< Last time
|
QDateTime m_lastReplenishmentTime = QDateTime::currentDateTime(); //!< Last time
|
||||||
};
|
};
|
||||||
}
|
} // ns
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user