mirror of
https://github.com/falcosecurity/falco.git
synced 2025-07-05 10:56:47 +00:00
new: dependency inject the timer for token bucket
Co-Authored-By: Lorenzo Fontana <lo@linux.com> Signed-off-by: Leonardo Di Donato <leodidonato@gmail.com>
This commit is contained in:
parent
1a0cf69b03
commit
a09f71b457
@ -18,13 +18,19 @@ limitations under the License.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <functional>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
#include "utils.h"
|
|
||||||
#include "token_bucket.h"
|
#include "token_bucket.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
token_bucket::token_bucket()
|
token_bucket::token_bucket() : token_bucket(sinsp_utils::get_current_time_ns)
|
||||||
{
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
token_bucket::token_bucket(std::function<uint64_t()> timer)
|
||||||
|
{
|
||||||
|
m_timer = timer;
|
||||||
init(1, 1);
|
init(1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,20 +43,12 @@ 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 = now == 0 ? m_timer() : now;
|
||||||
if(now == 0)
|
|
||||||
{
|
|
||||||
now = sinsp_utils::get_current_time_ns();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_last_seen = now;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool token_bucket::claim()
|
bool token_bucket::claim()
|
||||||
{
|
{
|
||||||
uint64_t now = sinsp_utils::get_current_time_ns();
|
return claim(1, m_timer());
|
||||||
|
|
||||||
return claim(1, now);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool token_bucket::claim(double tokens, uint64_t now)
|
bool token_bucket::claim(double tokens, uint64_t now)
|
||||||
|
@ -20,6 +20,9 @@ limitations under the License.
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
using token_timer = std::function<uint64_t()>;
|
||||||
|
|
||||||
// A simple token bucket that accumulates tokens at a fixed rate and allows
|
// A simple token bucket that accumulates tokens at a fixed rate and allows
|
||||||
// for limited bursting in the form of "banked" tokens.
|
// for limited bursting in the form of "banked" tokens.
|
||||||
@ -27,6 +30,7 @@ class token_bucket
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
token_bucket();
|
token_bucket();
|
||||||
|
token_bucket(std::function<uint64_t()> timer);
|
||||||
virtual ~token_bucket();
|
virtual ~token_bucket();
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -52,6 +56,7 @@ public:
|
|||||||
uint64_t get_last_seen();
|
uint64_t get_last_seen();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::function<uint64_t()> m_timer;
|
||||||
|
|
||||||
//
|
//
|
||||||
// The number of tokens generated per second.
|
// The number of tokens generated per second.
|
||||||
@ -75,4 +80,3 @@ private:
|
|||||||
//
|
//
|
||||||
uint64_t m_last_seen;
|
uint64_t m_last_seen;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user