47 template <
typename Clock = std::chrono::steady_clock>
49 static_assert(Clock::is_steady,
"clock must be steady");
100 using dur = std::chrono::duration<double>;
102 return std::chrono::duration_cast<dur>(
now).
count();
127 assert(burstSize > 0);
130 rate, burstSize, nowInSeconds, [toConsume](
double&
tokens) {
131 if (tokens < toConsume) {
160 assert(burstSize > 0);
164 rate, burstSize, nowInSeconds, [&consumed, toConsume](
double&
tokens) {
165 if (tokens < toConsume) {
169 consumed = toConsume;
187 assert(burstSize > 0);
193 template <
typename TCallback>
198 const TCallback& callback) {
202 auto tokens =
std::min((nowInSeconds - zeroTimeOld) * rate, burstSize);
206 zeroTimeNew = nowInSeconds -
tokens / rate;
220 template <
typename Clock = std::chrono::steady_clock>
222 static_assert(Clock::is_steady,
"clock must be steady");
241 : tokenBucket_(zeroTime), rate_(genRate), burstSize_(burstSize) {
243 assert(burstSize_ > 0);
264 return Impl::defaultClockNow();
283 assert(burstSize > 0);
284 const double availTokens =
available(nowInSeconds);
286 burstSize_ = burstSize;
287 setCapacity(availTokens, nowInSeconds);
319 return tokenBucket_.consume(toConsume, rate_, burstSize_, nowInSeconds);
338 return tokenBucket_.consumeOrDrain(
339 toConsume, rate_, burstSize_, nowInSeconds);
348 return tokenBucket_.available(rate_, burstSize_, nowInSeconds);
BasicTokenBucket(double genRate, double burstSize, double zeroTime=0) noexcept
std::atomic< double > zeroTime_
std::chrono::steady_clock::time_point now()
BasicDynamicTokenBucket & operator=(const BasicDynamicTokenBucket &other) noexcept
bool consumeImpl(double rate, double burstSize, double nowInSeconds, const TCallback &callback)
—— Concurrent Priority Queue Implementation ——
requires E e noexcept(noexcept(s.error(std::move(e))))
double available(double nowInSeconds=defaultClockNow()) const
static double defaultClockNow() noexcept
constexpr std::size_t hardware_destructive_interference_size
bool consume(double toConsume, double rate, double burstSize, double nowInSeconds=defaultClockNow())
void reset(double zeroTime=0) noexcept
double available(double rate, double burstSize, double nowInSeconds=defaultClockNow()) const noexcept
double consumeOrDrain(double toConsume, double rate, double burstSize, double nowInSeconds=defaultClockNow())
bool consume(double toConsume, double nowInSeconds=defaultClockNow())
double rate() const noexcept
BasicDynamicTokenBucket(const BasicDynamicTokenBucket &other) noexcept
void reset(double genRate, double burstSize, double nowInSeconds=defaultClockNow()) noexcept
BasicDynamicTokenBucket(double zeroTime=0) noexcept
void setCapacity(double tokens, double nowInSeconds) noexcept
static const char tokens[256]
double burst() const noexcept
static double defaultClockNow() noexcept(noexcept(Impl::defaultClockNow()))
double consumeOrDrain(double toConsume, double nowInSeconds=defaultClockNow())