proxygen
folly::BasicTokenBucket< Clock > Class Template Reference

#include <TokenBucket.h>

Public Member Functions

 BasicTokenBucket (double genRate, double burstSize, double zeroTime=0) noexcept
 
 BasicTokenBucket (const BasicTokenBucket &other) noexcept=default
 
BasicTokenBucketoperator= (const BasicTokenBucket &other) noexcept=default
 
void reset (double genRate, double burstSize, double nowInSeconds=defaultClockNow()) noexcept
 
void setCapacity (double tokens, double nowInSeconds) noexcept
 
bool consume (double toConsume, double nowInSeconds=defaultClockNow())
 
double consumeOrDrain (double toConsume, double nowInSeconds=defaultClockNow())
 
double available (double nowInSeconds=defaultClockNow()) const
 
double rate () const noexcept
 
double burst () const noexcept
 

Static Public Member Functions

static double defaultClockNow () noexcept(noexcept(Impl::defaultClockNow()))
 

Private Types

using Impl = BasicDynamicTokenBucket< Clock >
 

Private Attributes

Impl tokenBucket_
 
double rate_
 
double burstSize_
 

Detailed Description

template<typename Clock = std::chrono::steady_clock>
class folly::BasicTokenBucket< Clock >

Specialization of BasicDynamicTokenBucket with a fixed token generation rate and a fixed maximum burst size.

Definition at line 221 of file TokenBucket.h.

Member Typedef Documentation

template<typename Clock = std::chrono::steady_clock>
using folly::BasicTokenBucket< Clock >::Impl = BasicDynamicTokenBucket<Clock>
private

Definition at line 225 of file TokenBucket.h.

Constructor & Destructor Documentation

template<typename Clock = std::chrono::steady_clock>
folly::BasicTokenBucket< Clock >::BasicTokenBucket ( double  genRate,
double  burstSize,
double  zeroTime = 0 
)
inlinenoexcept

Construct a token bucket with a specific maximum rate and burst size.

Parameters
genRateNumber of tokens to generate per second.
burstSizeMaximum burst size. Must be greater than 0.
zeroTimeInitial time at which to consider the token bucket starting to fill. Defaults to 0, so by default token bucket is "full" after construction.

Definition at line 237 of file TokenBucket.h.

References folly::pushmi::__adl::noexcept(), and folly::BasicDynamicTokenBucket< Clock >::operator=().

241  : tokenBucket_(zeroTime), rate_(genRate), burstSize_(burstSize) {
242  assert(rate_ > 0);
243  assert(burstSize_ > 0);
244  }
template<typename Clock = std::chrono::steady_clock>
folly::BasicTokenBucket< Clock >::BasicTokenBucket ( const BasicTokenBucket< Clock > &  other)
defaultnoexcept

Copy constructor.

Warning: not thread safe!

Member Function Documentation

template<typename Clock = std::chrono::steady_clock>
double folly::BasicTokenBucket< Clock >::available ( double  nowInSeconds = defaultClockNow()) const
inline

Returns the number of tokens currently available.

Thread-safe (but returned value may immediately be outdated).

Definition at line 347 of file TokenBucket.h.

Referenced by TEST().

347  {
348  return tokenBucket_.available(rate_, burstSize_, nowInSeconds);
349  }
double available(double rate, double burstSize, double nowInSeconds=defaultClockNow()) const noexcept
Definition: TokenBucket.h:182
template<typename Clock = std::chrono::steady_clock>
double folly::BasicTokenBucket< Clock >::burst ( ) const
inlinenoexcept

Returns the maximum burst size.

Thread-safe (but returned value may immediately be outdated).

Definition at line 365 of file TokenBucket.h.

365  {
366  return burstSize_;
367  }
template<typename Clock = std::chrono::steady_clock>
bool folly::BasicTokenBucket< Clock >::consume ( double  toConsume,
double  nowInSeconds = defaultClockNow() 
)
inline

Attempts to consume some number of tokens. Tokens are first added to the bucket based on the time elapsed since the last attempt to consume tokens. Note: Attempts to consume more tokens than the burst size will always fail.

Thread-safe.

Parameters
toConsumeThe number of tokens to consume.
nowInSecondsCurrent time in seconds. Should be monotonically increasing from the nowInSeconds specified in this token bucket's constructor.
Returns
True if the rate limit check passed, false otherwise.

Definition at line 318 of file TokenBucket.h.

Referenced by doTokenBucketTest(), TEST(), and TEST_P().

318  {
319  return tokenBucket_.consume(toConsume, rate_, burstSize_, nowInSeconds);
320  }
bool consume(double toConsume, double rate, double burstSize, double nowInSeconds=defaultClockNow())
Definition: TokenBucket.h:121
template<typename Clock = std::chrono::steady_clock>
double folly::BasicTokenBucket< Clock >::consumeOrDrain ( double  toConsume,
double  nowInSeconds = defaultClockNow() 
)
inline

Similar to consume, but always consumes some number of tokens. If the bucket contains enough tokens - consumes toConsume tokens. Otherwise the bucket is drained.

Thread-safe.

Parameters
toConsumeThe number of tokens to consume.
nowInSecondsCurrent time in seconds. Should be monotonically increasing from the nowInSeconds specified in this token bucket's constructor.
Returns
number of tokens that were consumed.

Definition at line 335 of file TokenBucket.h.

337  {
339  toConsume, rate_, burstSize_, nowInSeconds);
340  }
double consumeOrDrain(double toConsume, double rate, double burstSize, double nowInSeconds=defaultClockNow())
Definition: TokenBucket.h:154
template<typename Clock = std::chrono::steady_clock>
static double folly::BasicTokenBucket< Clock >::defaultClockNow ( )
inlinestaticnoexcept

Returns the current time in seconds since Epoch.

Definition at line 263 of file TokenBucket.h.

263  {
264  return Impl::defaultClockNow();
265  }
static double defaultClockNow() noexcept
Definition: TokenBucket.h:99
template<typename Clock = std::chrono::steady_clock>
BasicTokenBucket& folly::BasicTokenBucket< Clock >::operator= ( const BasicTokenBucket< Clock > &  other)
defaultnoexcept

Copy-assignment operator.

Warning: not thread safe!

template<typename Clock = std::chrono::steady_clock>
double folly::BasicTokenBucket< Clock >::rate ( ) const
inlinenoexcept

Returns the number of tokens generated per second.

Thread-safe (but returned value may immediately be outdated).

Definition at line 356 of file TokenBucket.h.

356  {
357  return rate_;
358  }
template<typename Clock = std::chrono::steady_clock>
void folly::BasicTokenBucket< Clock >::reset ( double  genRate,
double  burstSize,
double  nowInSeconds = defaultClockNow() 
)
inlinenoexcept

Change rate and burst size.

Warning: not thread safe!

Parameters
genRateNumber of tokens to generate per second.
burstSizeMaximum burst size. Must be greater than 0.
nowInSecondsCurrent time in seconds. Should be monotonically increasing from the nowInSeconds specified in this token bucket's constructor.

Definition at line 278 of file TokenBucket.h.

References folly::BasicDynamicTokenBucket< Clock >::available().

Referenced by folly::BasicTokenBucket< Clock >::setCapacity().

281  {
282  assert(genRate > 0);
283  assert(burstSize > 0);
284  const double availTokens = available(nowInSeconds);
285  rate_ = genRate;
286  burstSize_ = burstSize;
287  setCapacity(availTokens, nowInSeconds);
288  }
double available(double nowInSeconds=defaultClockNow()) const
Definition: TokenBucket.h:347
void setCapacity(double tokens, double nowInSeconds) noexcept
Definition: TokenBucket.h:300
template<typename Clock = std::chrono::steady_clock>
void folly::BasicTokenBucket< Clock >::setCapacity ( double  tokens,
double  nowInSeconds 
)
inlinenoexcept

Change number of tokens in bucket.

Warning: not thread safe!

Parameters
tokensDesired number of tokens in bucket after the call.
nowInSecondsCurrent time in seconds. Should be monotonically increasing from the nowInSeconds specified in this token bucket's constructor.

Definition at line 300 of file TokenBucket.h.

References folly::BasicTokenBucket< Clock >::reset(), and tokens.

300  {
301  tokenBucket_.reset(nowInSeconds - tokens / rate_);
302  }
void reset(double zeroTime=0) noexcept
Definition: TokenBucket.h:92
static const char tokens[256]
Definition: http_parser.c:184

Member Data Documentation

template<typename Clock = std::chrono::steady_clock>
double folly::BasicTokenBucket< Clock >::burstSize_
private

Definition at line 372 of file TokenBucket.h.

template<typename Clock = std::chrono::steady_clock>
double folly::BasicTokenBucket< Clock >::rate_
private

Definition at line 371 of file TokenBucket.h.

template<typename Clock = std::chrono::steady_clock>
Impl folly::BasicTokenBucket< Clock >::tokenBucket_
private

Definition at line 370 of file TokenBucket.h.


The documentation for this class was generated from the following file: