CppCommon  1.0.4.1
C++ Common Library
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 
15 namespace CppCommon {
16 
18 
34 {
35 public:
37 
44  TokenBucket(uint64_t rate, uint64_t burst);
45  TokenBucket(const TokenBucket& tb);
46  TokenBucket(TokenBucket&&) = delete;
47  ~TokenBucket() = default;
48 
49  TokenBucket& operator=(const TokenBucket& tb);
51 
53 
57  bool Consume(uint64_t tokens = 1);
58 
59 private:
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.
Definition: token_bucket.h:34
TokenBucket(TokenBucket &&)=delete
bool Consume(uint64_t tokens=1)
Try to consume the given count of tokens.
TokenBucket & operator=(TokenBucket &&)=delete
TokenBucket(uint64_t rate, uint64_t burst)
Initialize the token bucket.
TokenBucket & operator=(const TokenBucket &tb)
C++ Common project definitions.
Definition: token_bucket.h:15
Token bucket rate limit algorithm inline implementation.