9 static const char rcsid[] __attribute__((used)) =
"$Id: TokenBucket.cpp 1653 2016-02-28 19:54:59Z sella $";
11 #include "TokenBucket.h"
17 using namespace sella::algo;
19 TokenBucket::TokenBucket(
const TokenBucket & chain,
double limit)
throw () :
20 chain(std::make_shared<TokenBucket>(chain)),
28 TokenBucket::TokenBucket(
double limit)
throw () :
37 bool TokenBucket::sub(
double value,
bool blocking)
throw () {
42 if (this->chain.get() != NULL) {
43 if (this->chain->sub(value, blocking) ==
false) {
48 gettimeofday(&stamp, NULL);
49 usecs = (stamp.tv_sec * 1000000) + stamp.tv_usec;
51 if ((this->value += ((usecs - this->stamp) * this->limit) / 1000000) > this->limit) {
52 this->value = this->limit;
56 if (this->value > 0.0) {
62 if (this->chain.get() != NULL) {
63 this->chain->add(value);
67 usleep((0.0 - value) / this->limit * 1000000);
73 bool TokenBucket::add(
double value)
throw () {
74 if ((this->value += value) > this->limit) {
75 this->value = this->limit;