Merge pull request #236 from draios/expose-tokens

Add ability to get number of tokens.
This commit is contained in:
Mark Stemm 2017-04-27 13:19:47 -07:00 committed by GitHub
commit a3c83e7f6e
2 changed files with 26 additions and 3 deletions

View File

@ -31,12 +31,18 @@ token_bucket::~token_bucket()
{ {
} }
void token_bucket::init(double rate, double max_tokens) void token_bucket::init(double rate, double max_tokens, uint64_t now)
{ {
m_rate = rate; m_rate = rate;
m_max_tokens = max_tokens; m_max_tokens = max_tokens;
m_tokens = max_tokens; m_tokens = max_tokens;
m_last_seen = sinsp_utils::get_current_time_ns();
if(now == 0)
{
now = sinsp_utils::get_current_time_ns();
}
m_last_seen = now;
} }
bool token_bucket::claim(uint64_t now) bool token_bucket::claim(uint64_t now)
@ -73,3 +79,13 @@ bool token_bucket::claim(uint64_t now)
return true; return true;
} }
double token_bucket::get_tokens()
{
return m_tokens;
}
uint64_t token_bucket::get_last_seen()
{
return m_last_seen;
}

View File

@ -31,13 +31,20 @@ public:
// //
// Initialize the token bucket and start accumulating tokens // Initialize the token bucket and start accumulating tokens
// //
void init(double rate, double max_tokens); void init(double rate, double max_tokens, uint64_t now = 0);
// //
// Returns true if a token can be claimed. Also updates // Returns true if a token can be claimed. Also updates
// internal metrics. // internal metrics.
// //
bool claim(uint64_t now = 0); bool claim(uint64_t now = 0);
// Return the current number of tokens available
double get_tokens();
// Return the last time someone tried to claim a token.
uint64_t get_last_seen();
private: private:
// //