mirror of
https://github.com/falcosecurity/falco.git
synced 2025-09-28 21:57:37 +00:00
sinsp_utils::get_current_time_ns() has the same purpose as get_epoch_ns(), and now that we're including the token bucket in falco_engine, it's easy to package the dependency. So use that function instead.
66 lines
1.4 KiB
C++
66 lines
1.4 KiB
C++
/*
|
|
Copyright (C) 2016 Draios inc.
|
|
|
|
This file is part of falco.
|
|
|
|
falco is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License version 2 as
|
|
published by the Free Software Foundation.
|
|
|
|
falco is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with falco. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <cstdint>
|
|
|
|
// 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();
|
|
virtual ~token_bucket();
|
|
|
|
//
|
|
// Initialize the token bucket and start accumulating tokens
|
|
//
|
|
void init(uint32_t rate, uint32_t max_tokens);
|
|
|
|
//
|
|
// Returns true if a token can be claimed. Also updates
|
|
// internal metrics.
|
|
//
|
|
bool claim();
|
|
private:
|
|
|
|
//
|
|
// The number of tokens generated per second.
|
|
//
|
|
uint64_t m_rate;
|
|
|
|
//
|
|
// The maximum number of tokens that can be banked for future
|
|
// claim()s.
|
|
//
|
|
uint64_t m_max_tokens;
|
|
|
|
//
|
|
// The current number of tokens
|
|
//
|
|
uint64_t m_tokens;
|
|
|
|
//
|
|
// The last time claim() was called (or the object was created).
|
|
// Nanoseconds since the epoch.
|
|
//
|
|
uint64_t m_last_seen;
|
|
};
|
|
|