swift
Public Member Functions | List of all members
swift::misc::CTokenBucket Class Reference

Token bucket algorithm. More...

Public Member Functions

 CTokenBucket (int capacity, qint64 intervalMs, int numTokensToRefill)
 Constructor for given replenishment policy.
 
bool tryConsume (int numTokens=1, qint64 msSinceEpoch=-1)
 Try to consume a number of tokens. More...
 
void setNumberOfTokensToRefill (int numTokens)
 Number of tokens to refill.
 
void setCapacity (int capacity)
 Set the capacity.
 
void setCapacityAndTokensToRefill (int numTokens)
 Tokens/capacity if both are same.
 
void setInterval (qint64 intervalMs)
 Set the interval.
 
int getTokensPerSecond () const
 Tokens per second.
 

Detailed Description

Token bucket algorithm.

This class implements the token bucket algorithm. Tokens as arbitrary unit are added to the bucket at a defined rate. Token can be consumsed as long as there are enough available. This class can be used to throttle traffic and packet generation. Each time a packet needs to be generated and sent a token is consumed. If no token is available, consumption will fail the the packet cannot be sent.

Definition at line 20 of file tokenbucket.h.

Member Function Documentation

◆ tryConsume()

bool swift::misc::CTokenBucket::tryConsume ( int  numTokens = 1,
qint64  msSinceEpoch = -1 
)

Try to consume a number of tokens.

Remarks
if a current timestamp is already available, it can be passed

Definition at line 14 of file tokenbucket.cpp.


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