From 6a4e4eaa4fd604b206747e15fa3ea3662a99a8a0 Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Fri, 15 Oct 2021 16:31:25 -0700 Subject: [PATCH] Finish moving token bucket impl from falco to libs It took a while, but we remembered to finish moving the token_bucket from falco engine to libs. There were 2 copies for a while. This brings over one change to libs--to have an optional timer function. Co-authored-by: Leonardo Grasso Co-authored-by: Loris Degioanni Signed-off-by: Mark Stemm --- userspace/engine/CMakeLists.txt | 1 - userspace/engine/token_bucket.cpp | 89 ------------------------------- userspace/engine/token_bucket.h | 77 -------------------------- 3 files changed, 167 deletions(-) delete mode 100644 userspace/engine/token_bucket.cpp delete mode 100644 userspace/engine/token_bucket.h diff --git a/userspace/engine/CMakeLists.txt b/userspace/engine/CMakeLists.txt index f5dfa56e..ab569ea6 100644 --- a/userspace/engine/CMakeLists.txt +++ b/userspace/engine/CMakeLists.txt @@ -17,7 +17,6 @@ set(FALCO_ENGINE_SOURCE_FILES falco_utils.cpp json_evt.cpp ruleset.cpp - token_bucket.cpp formats.cpp) add_library(falco_engine STATIC ${FALCO_ENGINE_SOURCE_FILES}) diff --git a/userspace/engine/token_bucket.cpp b/userspace/engine/token_bucket.cpp deleted file mode 100644 index 04893e48..00000000 --- a/userspace/engine/token_bucket.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/* -Copyright (C) 2019 The Falco Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -#include -#include -#include - -#include "token_bucket.h" -#include "utils.h" -#include "banned.h" // This raises a compilation error when certain functions are used - -token_bucket::token_bucket(): - token_bucket(sinsp_utils::get_current_time_ns) -{ -} - -token_bucket::token_bucket(std::function timer) -{ - m_timer = timer; - init(1, 1); -} - -token_bucket::~token_bucket() -{ -} - -void token_bucket::init(double rate, double max_tokens, uint64_t now) -{ - m_rate = rate; - m_max_tokens = max_tokens; - m_tokens = max_tokens; - m_last_seen = now == 0 ? m_timer() : now; -} - -bool token_bucket::claim() -{ - return claim(1, m_timer()); -} - -bool token_bucket::claim(double tokens, uint64_t now) -{ - double tokens_gained = m_rate * ((now - m_last_seen) / (1000000000.0)); - m_last_seen = now; - - m_tokens += tokens_gained; - - // - // Cap at max_tokens - // - if(m_tokens > m_max_tokens) - { - m_tokens = m_max_tokens; - } - - // - // If m_tokens is < tokens, can't claim. - // - if(m_tokens < tokens) - { - return false; - } - - m_tokens -= tokens; - - return true; -} - -double token_bucket::get_tokens() -{ - return m_tokens; -} - -uint64_t token_bucket::get_last_seen() -{ - return m_last_seen; -} diff --git a/userspace/engine/token_bucket.h b/userspace/engine/token_bucket.h deleted file mode 100644 index 4feb95dd..00000000 --- a/userspace/engine/token_bucket.h +++ /dev/null @@ -1,77 +0,0 @@ -/* -Copyright (C) 2019 The Falco Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -#pragma once - -#include -#include - -// A simple token bucket that accumulates tokens at a fixed rate and allows -// for limited bursting in the form of "banked" tokens. -class token_bucket -{ -public: - token_bucket(); - token_bucket(std::function timer); - virtual ~token_bucket(); - - // - // Initialize the token bucket and start accumulating tokens - // - void init(double rate, double max_tokens, uint64_t now = 0); - - // - // Try to claim tokens tokens from the token bucket, using a - // timestamp of now. Returns true if the tokens could be - // claimed. Also updates internal metrics. - // - bool claim(double tokens, uint64_t now); - - // Simpler version of claim that claims a single token and - // uses the current time for now - bool claim(); - - // 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: - std::function m_timer; - - // - // The number of tokens generated per second. - // - double m_rate; - - // - // The maximum number of tokens that can be banked for future - // claim()s. - // - double m_max_tokens; - - // - // The current number of tokens - // - double m_tokens; - - // - // The last time claim() was called (or the object was created). - // Nanoseconds since the epoch. - // - uint64_t m_last_seen; -};