CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
token_bucket.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_ALGORITHMS_TOKEN_BUCKET_H
10#define CPPCOMMON_ALGORITHMS_TOKEN_BUCKET_H
11
12#include <atomic>
13#include <cstdint>
14
15namespace CppCommon {
16
18
34{
35public:
37
44 TokenBucket(uint64_t rate, uint64_t burst);
45 TokenBucket(const TokenBucket& tb);
47 ~TokenBucket() = default;
48
51
53
57 bool Consume(uint64_t tokens = 1);
58
59private:
60 std::atomic<uint64_t> _time;
61 std::atomic<uint64_t> _time_per_token;
62 std::atomic<uint64_t> _time_per_burst;
63};
64
67} // namespace CppCommon
68
69#include "token_bucket.inl"
70
71#endif // CPPCOMMON_ALGORITHMS_TOKEN_BUCKET_H
Token bucket rate limit algorithm.
TokenBucket(TokenBucket &&)=delete
bool Consume(uint64_t tokens=1)
Try to consume the given count of tokens.
TokenBucket & operator=(TokenBucket &&)=delete
TokenBucket & operator=(const TokenBucket &tb)
C++ Common project definitions.
Token bucket rate limit algorithm inline implementation.