mirror of
https://github.com/falcosecurity/falco.git
synced 2025-10-22 12:27:10 +00:00
chore: clang format following the current style
Signed-off-by: Lorenzo Fontana <lo@linux.com> Co-authored-by: Leonardo Di Donato <leodidonato@gmail.com>
This commit is contained in:
committed by
Lorenzo Fontana
parent
5fdf658d0e
commit
b2ef08fd30
@@ -1,10 +1,16 @@
|
|||||||
# This coding convention's solely goal is to approximately match the current code style.
|
|
||||||
# It MUST not be intended in any other way until a real and definitive coding convention is put in.
|
|
||||||
---
|
---
|
||||||
BreakBeforeBraces: GNU
|
|
||||||
ColumnLimit: 0
|
|
||||||
IndentWidth: 4
|
|
||||||
Language: Cpp
|
Language: Cpp
|
||||||
|
BasedOnStyle: LLVM
|
||||||
|
AccessModifierOffset: -8
|
||||||
|
BreakBeforeBraces: Allman
|
||||||
|
BreakConstructorInitializers: AfterColon
|
||||||
|
ColumnLimit: 0
|
||||||
|
ConstructorInitializerIndentWidth: 8
|
||||||
|
ContinuationIndentWidth: 8
|
||||||
|
DerivePointerAlignment: true
|
||||||
|
IndentWidth: 8
|
||||||
|
SortIncludes: false
|
||||||
|
SpaceAfterTemplateKeyword: false
|
||||||
|
SpaceBeforeCtorInitializerColon: false
|
||||||
SpaceBeforeParens: Never
|
SpaceBeforeParens: Never
|
||||||
Standard: Auto
|
UseTab: Always
|
||||||
UseTab: Always
|
|
||||||
|
@@ -23,61 +23,61 @@ using namespace Catch::literals;
|
|||||||
|
|
||||||
TEST_CASE("token bucket default ctor", "[token_bucket]")
|
TEST_CASE("token bucket default ctor", "[token_bucket]")
|
||||||
{
|
{
|
||||||
auto tb = new token_bucket();
|
auto tb = new token_bucket();
|
||||||
|
|
||||||
REQUIRE(tb->get_tokens() == 1);
|
REQUIRE(tb->get_tokens() == 1);
|
||||||
|
|
||||||
SECTION("initialising with specific time, rate 2 tokens/sec")
|
SECTION("initialising with specific time, rate 2 tokens/sec")
|
||||||
{
|
{
|
||||||
auto max = 2.0;
|
auto max = 2.0;
|
||||||
uint64_t now = 1;
|
uint64_t now = 1;
|
||||||
tb->init(1.0, max, now);
|
tb->init(1.0, max, now);
|
||||||
REQUIRE(tb->get_last_seen() == now);
|
REQUIRE(tb->get_last_seen() == now);
|
||||||
REQUIRE(tb->get_tokens() == max);
|
REQUIRE(tb->get_tokens() == max);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("token bucket ctor with custom timer", "[token_bucket]")
|
TEST_CASE("token bucket ctor with custom timer", "[token_bucket]")
|
||||||
{
|
{
|
||||||
auto t = []() -> uint64_t { return 22; };
|
auto t = []() -> uint64_t { return 22; };
|
||||||
auto tb = new token_bucket(t);
|
auto tb = new token_bucket(t);
|
||||||
|
|
||||||
REQUIRE(tb->get_tokens() == 1);
|
REQUIRE(tb->get_tokens() == 1);
|
||||||
REQUIRE(tb->get_last_seen() == 22);
|
REQUIRE(tb->get_last_seen() == 22);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("token bucket with 2 tokens/sec rate, max 10 tokens", "[token_bucket]")
|
TEST_CASE("token bucket with 2 tokens/sec rate, max 10 tokens", "[token_bucket]")
|
||||||
{
|
{
|
||||||
auto tb = new token_bucket();
|
auto tb = new token_bucket();
|
||||||
tb->init(2.0, 10, 1);
|
tb->init(2.0, 10, 1);
|
||||||
|
|
||||||
SECTION("claiming 5 tokens")
|
SECTION("claiming 5 tokens")
|
||||||
{
|
|
||||||
bool claimed = tb->claim(5, 1000000001);
|
|
||||||
REQUIRE(tb->get_last_seen() == 1000000001);
|
|
||||||
REQUIRE(tb->get_tokens() == 5.0_a);
|
|
||||||
REQUIRE(claimed);
|
|
||||||
|
|
||||||
SECTION("claiming all the 7 remaining tokens")
|
|
||||||
{
|
{
|
||||||
bool claimed = tb->claim(7, 2000000001);
|
bool claimed = tb->claim(5, 1000000001);
|
||||||
REQUIRE(tb->get_last_seen() == 2000000001);
|
REQUIRE(tb->get_last_seen() == 1000000001);
|
||||||
REQUIRE(tb->get_tokens() == 0.0_a);
|
REQUIRE(tb->get_tokens() == 5.0_a);
|
||||||
REQUIRE(claimed);
|
REQUIRE(claimed);
|
||||||
|
|
||||||
SECTION("claiming 1 token more than the 2 available fails")
|
SECTION("claiming all the 7 remaining tokens")
|
||||||
{
|
{
|
||||||
bool claimed = tb->claim(3, 3000000001);
|
bool claimed = tb->claim(7, 2000000001);
|
||||||
REQUIRE(tb->get_last_seen() == 3000000001);
|
REQUIRE(tb->get_last_seen() == 2000000001);
|
||||||
REQUIRE(tb->get_tokens() == 2.0_a);
|
REQUIRE(tb->get_tokens() == 0.0_a);
|
||||||
REQUIRE_FALSE(claimed);
|
REQUIRE(claimed);
|
||||||
}
|
|
||||||
|
SECTION("claiming 1 token more than the 2 available fails")
|
||||||
|
{
|
||||||
|
bool claimed = tb->claim(3, 3000000001);
|
||||||
|
REQUIRE(tb->get_last_seen() == 3000000001);
|
||||||
|
REQUIRE(tb->get_tokens() == 2.0_a);
|
||||||
|
REQUIRE_FALSE(claimed);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("token bucket default initialization", "[token_bucket]")
|
TEST_CASE("token bucket default initialization", "[token_bucket]")
|
||||||
{
|
{
|
||||||
token_bucket tb;
|
token_bucket tb;
|
||||||
REQUIRE(tb.get_tokens() == 1);
|
REQUIRE(tb.get_tokens() == 1);
|
||||||
}
|
}
|
||||||
|
@@ -24,14 +24,15 @@ limitations under the License.
|
|||||||
#include "token_bucket.h"
|
#include "token_bucket.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
token_bucket::token_bucket() : token_bucket(sinsp_utils::get_current_time_ns)
|
token_bucket::token_bucket():
|
||||||
|
token_bucket(sinsp_utils::get_current_time_ns)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
token_bucket::token_bucket(std::function<uint64_t()> timer)
|
token_bucket::token_bucket(std::function<uint64_t()> timer)
|
||||||
{
|
{
|
||||||
m_timer = timer;
|
m_timer = timer;
|
||||||
init(1, 1);
|
init(1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
token_bucket::~token_bucket()
|
token_bucket::~token_bucket()
|
||||||
@@ -40,51 +41,51 @@ token_bucket::~token_bucket()
|
|||||||
|
|
||||||
void token_bucket::init(double rate, double max_tokens, uint64_t now)
|
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;
|
m_last_seen = now == 0 ? m_timer() : now;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool token_bucket::claim()
|
bool token_bucket::claim()
|
||||||
{
|
{
|
||||||
return claim(1, m_timer());
|
return claim(1, m_timer());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool token_bucket::claim(double tokens, uint64_t now)
|
bool token_bucket::claim(double tokens, uint64_t now)
|
||||||
{
|
{
|
||||||
double tokens_gained = m_rate * ((now - m_last_seen) / (1000000000.0));
|
double tokens_gained = m_rate * ((now - m_last_seen) / (1000000000.0));
|
||||||
m_last_seen = now;
|
m_last_seen = now;
|
||||||
|
|
||||||
m_tokens += tokens_gained;
|
m_tokens += tokens_gained;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Cap at max_tokens
|
// Cap at max_tokens
|
||||||
//
|
//
|
||||||
if(m_tokens > m_max_tokens)
|
if(m_tokens > m_max_tokens)
|
||||||
{
|
{
|
||||||
m_tokens = m_max_tokens;
|
m_tokens = m_max_tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// If m_tokens is < tokens, can't claim.
|
// If m_tokens is < tokens, can't claim.
|
||||||
//
|
//
|
||||||
if(m_tokens < tokens)
|
if(m_tokens < tokens)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_tokens -= tokens;
|
m_tokens -= tokens;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
double token_bucket::get_tokens()
|
double token_bucket::get_tokens()
|
||||||
{
|
{
|
||||||
return m_tokens;
|
return m_tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t token_bucket::get_last_seen()
|
uint64_t token_bucket::get_last_seen()
|
||||||
{
|
{
|
||||||
return m_last_seen;
|
return m_last_seen;
|
||||||
}
|
}
|
||||||
|
@@ -26,55 +26,55 @@ limitations under the License.
|
|||||||
// for limited bursting in the form of "banked" tokens.
|
// for limited bursting in the form of "banked" tokens.
|
||||||
class token_bucket
|
class token_bucket
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
token_bucket();
|
token_bucket();
|
||||||
token_bucket(std::function<uint64_t()> timer);
|
token_bucket(std::function<uint64_t()> timer);
|
||||||
virtual ~token_bucket();
|
virtual ~token_bucket();
|
||||||
|
|
||||||
//
|
//
|
||||||
// Initialize the token bucket and start accumulating tokens
|
// Initialize the token bucket and start accumulating tokens
|
||||||
//
|
//
|
||||||
void init(double rate, double max_tokens, uint64_t now = 0);
|
void init(double rate, double max_tokens, uint64_t now = 0);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Try to claim tokens tokens from the token bucket, using a
|
// Try to claim tokens tokens from the token bucket, using a
|
||||||
// timestamp of now. Returns true if the tokens could be
|
// timestamp of now. Returns true if the tokens could be
|
||||||
// claimed. Also updates internal metrics.
|
// claimed. Also updates internal metrics.
|
||||||
//
|
//
|
||||||
bool claim(double tokens, uint64_t now);
|
bool claim(double tokens, uint64_t now);
|
||||||
|
|
||||||
// Simpler version of claim that claims a single token and
|
// Simpler version of claim that claims a single token and
|
||||||
// uses the current time for now
|
// uses the current time for now
|
||||||
bool claim();
|
bool claim();
|
||||||
|
|
||||||
// Return the current number of tokens available
|
// Return the current number of tokens available
|
||||||
double get_tokens();
|
double get_tokens();
|
||||||
|
|
||||||
// Return the last time someone tried to claim a token.
|
// Return the last time someone tried to claim a token.
|
||||||
uint64_t get_last_seen();
|
uint64_t get_last_seen();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::function<uint64_t()> m_timer;
|
std::function<uint64_t()> m_timer;
|
||||||
|
|
||||||
//
|
//
|
||||||
// The number of tokens generated per second.
|
// The number of tokens generated per second.
|
||||||
//
|
//
|
||||||
double m_rate;
|
double m_rate;
|
||||||
|
|
||||||
//
|
//
|
||||||
// The maximum number of tokens that can be banked for future
|
// The maximum number of tokens that can be banked for future
|
||||||
// claim()s.
|
// claim()s.
|
||||||
//
|
//
|
||||||
double m_max_tokens;
|
double m_max_tokens;
|
||||||
|
|
||||||
//
|
//
|
||||||
// The current number of tokens
|
// The current number of tokens
|
||||||
//
|
//
|
||||||
double m_tokens;
|
double m_tokens;
|
||||||
|
|
||||||
//
|
//
|
||||||
// The last time claim() was called (or the object was created).
|
// The last time claim() was called (or the object was created).
|
||||||
// Nanoseconds since the epoch.
|
// Nanoseconds since the epoch.
|
||||||
//
|
//
|
||||||
uint64_t m_last_seen;
|
uint64_t m_last_seen;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user