mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-06 20:43:44 +02:00
Finish stripping minBalance from BandwidthAccount
This commit is contained in:
parent
a3a2b8dedb
commit
3a563250f7
4 changed files with 17 additions and 30 deletions
|
@ -1,7 +1,7 @@
|
||||||
CC=gcc
|
CC=gcc
|
||||||
CXX=g++
|
CXX=g++
|
||||||
|
|
||||||
INCLUDES=-Iext/bin/libcrypto/include -Iext/jsoncpp/include
|
INCLUDES=-Iext/bin/libcrypto/include
|
||||||
ARCH=$(shell uname -m)
|
ARCH=$(shell uname -m)
|
||||||
DEFS=-DZT_ARCH="$(ARCH)" -DZT_OSNAME="linux" -DZT_TRACE
|
DEFS=-DZT_ARCH="$(ARCH)" -DZT_OSNAME="linux" -DZT_TRACE
|
||||||
|
|
||||||
|
|
|
@ -31,9 +31,6 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#include "Constants.hpp"
|
#include "Constants.hpp"
|
||||||
#include "Utils.hpp"
|
#include "Utils.hpp"
|
||||||
|
|
||||||
|
@ -66,30 +63,27 @@ public:
|
||||||
* Create and initialize
|
* Create and initialize
|
||||||
*
|
*
|
||||||
* @param preload Initial balance to place in account
|
* @param preload Initial balance to place in account
|
||||||
* @param minb Minimum allowed balance (or maximum debt) (<= 0)
|
|
||||||
* @param maxb Maximum allowed balance (> 0)
|
* @param maxb Maximum allowed balance (> 0)
|
||||||
* @param acc Rate of accrual in bytes per second
|
* @param acc Rate of accrual in bytes per second
|
||||||
*/
|
*/
|
||||||
BandwidthAccount(int32_t preload,int32_t minb,int32_t maxb,int32_t acc)
|
BandwidthAccount(int32_t preload,int32_t maxb,int32_t acc)
|
||||||
throw()
|
throw()
|
||||||
{
|
{
|
||||||
init(preload,minb,maxb,acc);
|
init(preload,maxb,acc);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize or re-initialize account
|
* Initialize or re-initialize account
|
||||||
*
|
*
|
||||||
* @param preload Initial balance to place in account
|
* @param preload Initial balance to place in account
|
||||||
* @param minb Minimum allowed balance (or maximum debt) (<= 0)
|
|
||||||
* @param maxb Maximum allowed balance (> 0)
|
* @param maxb Maximum allowed balance (> 0)
|
||||||
* @param acc Rate of accrual in bytes per second
|
* @param acc Rate of accrual in bytes per second
|
||||||
*/
|
*/
|
||||||
inline void init(int32_t preload,int32_t minb,int32_t maxb,int32_t acc)
|
inline void init(int32_t preload,int32_t maxb,int32_t acc)
|
||||||
throw()
|
throw()
|
||||||
{
|
{
|
||||||
_lastTime = Utils::nowf();
|
_lastTime = Utils::nowf();
|
||||||
_balance = preload;
|
_balance = preload;
|
||||||
_minBalance = minb;
|
|
||||||
_maxBalance = maxb;
|
_maxBalance = maxb;
|
||||||
_accrual = acc;
|
_accrual = acc;
|
||||||
}
|
}
|
||||||
|
@ -98,23 +92,22 @@ public:
|
||||||
* Update balance by accruing and then deducting
|
* Update balance by accruing and then deducting
|
||||||
*
|
*
|
||||||
* @param deduct Amount to deduct, or 0.0 to just update
|
* @param deduct Amount to deduct, or 0.0 to just update
|
||||||
* @return New balance with deduction applied, and whether or not deduction fit
|
* @return New balance after deduction -- if negative, it didn't fit
|
||||||
*/
|
*/
|
||||||
inline std::pair<int32_t,bool> update(int32_t deduct)
|
inline int32_t update(int32_t deduct)
|
||||||
throw()
|
throw()
|
||||||
{
|
{
|
||||||
double lt = _lastTime;
|
double lt = _lastTime;
|
||||||
double now = Utils::nowf();
|
double now = Utils::nowf();
|
||||||
_lastTime = now;
|
_lastTime = now;
|
||||||
int32_t newbal = (int32_t)round((double)_balance + ((double)_accrual * (now - lt))) - deduct;
|
int32_t newbal = (int32_t)round((double)_balance + ((double)_accrual * (now - lt))) - deduct;
|
||||||
bool fits = (newbal > 0);
|
_balance = std::max((int32_t)0,std::min(_maxBalance,newbal));
|
||||||
return std::pair<int32_t,bool>((_balance = std::max(_minBalance,std::min(_maxBalance,newbal))),fits);
|
return newbal;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
double _lastTime;
|
double _lastTime;
|
||||||
int32_t _balance;
|
int32_t _balance;
|
||||||
int32_t _minBalance;
|
|
||||||
int32_t _maxBalance;
|
int32_t _maxBalance;
|
||||||
int32_t _accrual;
|
int32_t _accrual;
|
||||||
};
|
};
|
||||||
|
|
|
@ -91,7 +91,7 @@ bool Network::Certificate::qualifyMembership(const Network::Certificate &mc) con
|
||||||
}
|
}
|
||||||
|
|
||||||
// A low default global rate, fast enough for something like ARP
|
// A low default global rate, fast enough for something like ARP
|
||||||
const Network::MulticastRates::Rate Network::MulticastRates::GLOBAL_DEFAULT_RATE(128,-32,128,64);
|
const Network::MulticastRates::Rate Network::MulticastRates::GLOBAL_DEFAULT_RATE(128,128,64);
|
||||||
|
|
||||||
const char *Network::statusString(const Status s)
|
const char *Network::statusString(const Status s)
|
||||||
throw()
|
throw()
|
||||||
|
|
|
@ -166,10 +166,9 @@ public:
|
||||||
* Preload and rates of accrual for multicast group bandwidth limits
|
* Preload and rates of accrual for multicast group bandwidth limits
|
||||||
*
|
*
|
||||||
* Key is multicast group in lower case hex format: MAC (without :s) /
|
* Key is multicast group in lower case hex format: MAC (without :s) /
|
||||||
* ADI (hex). Value is a comma-delimited list of: preload, min, max,
|
* ADI (hex). Value is preload, maximum balance, and rate of accrual in
|
||||||
* rate of accrual for bandwidth accounts. A key called '*' indicates
|
* hex. These are signed hex numbers, so a negative value can be prefixed
|
||||||
* the default for unlisted groups. Values are in hexadecimal and may
|
* with '-'.
|
||||||
* be prefixed with '-' to indicate a negative value.
|
|
||||||
*/
|
*/
|
||||||
class MulticastRates : private Dictionary
|
class MulticastRates : private Dictionary
|
||||||
{
|
{
|
||||||
|
@ -180,15 +179,13 @@ public:
|
||||||
struct Rate
|
struct Rate
|
||||||
{
|
{
|
||||||
Rate() {}
|
Rate() {}
|
||||||
Rate(int32_t pl,int32_t minb,int32_t maxb,int32_t acc)
|
Rate(int32_t pl,int32_t maxb,int32_t acc)
|
||||||
{
|
{
|
||||||
preload = pl;
|
preload = pl;
|
||||||
minBalance = minb;
|
|
||||||
maxBalance = maxb;
|
maxBalance = maxb;
|
||||||
accrual = acc;
|
accrual = acc;
|
||||||
}
|
}
|
||||||
int32_t preload;
|
int32_t preload;
|
||||||
int32_t minBalance;
|
|
||||||
int32_t maxBalance;
|
int32_t maxBalance;
|
||||||
int32_t accrual;
|
int32_t accrual;
|
||||||
};
|
};
|
||||||
|
@ -234,7 +231,7 @@ public:
|
||||||
{
|
{
|
||||||
char tmp[16384];
|
char tmp[16384];
|
||||||
Utils::scopy(tmp,sizeof(tmp),s.c_str());
|
Utils::scopy(tmp,sizeof(tmp),s.c_str());
|
||||||
Rate r(0,0,0,0);
|
Rate r(0,0,0);
|
||||||
char *saveptr = (char *)0;
|
char *saveptr = (char *)0;
|
||||||
unsigned int fn = 0;
|
unsigned int fn = 0;
|
||||||
for(char *f=Utils::stok(tmp,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
|
for(char *f=Utils::stok(tmp,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
|
||||||
|
@ -243,12 +240,9 @@ public:
|
||||||
r.preload = (int32_t)Utils::hexStrToLong(f);
|
r.preload = (int32_t)Utils::hexStrToLong(f);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
r.minBalance = (int32_t)Utils::hexStrToLong(f);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
r.maxBalance = (int32_t)Utils::hexStrToLong(f);
|
r.maxBalance = (int32_t)Utils::hexStrToLong(f);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 2:
|
||||||
r.accrual = (int32_t)Utils::hexStrToLong(f);
|
r.accrual = (int32_t)Utils::hexStrToLong(f);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -577,9 +571,9 @@ public:
|
||||||
std::map< std::pair<Address,MulticastGroup>,BandwidthAccount >::iterator bal(_multicastRateAccounts.find(k));
|
std::map< std::pair<Address,MulticastGroup>,BandwidthAccount >::iterator bal(_multicastRateAccounts.find(k));
|
||||||
if (bal == _multicastRateAccounts.end()) {
|
if (bal == _multicastRateAccounts.end()) {
|
||||||
MulticastRates::Rate r(_mcRates.get(mg));
|
MulticastRates::Rate r(_mcRates.get(mg));
|
||||||
bal = _multicastRateAccounts.insert(std::make_pair(k,BandwidthAccount(r.preload,r.minBalance,r.maxBalance,r.accrual))).first;
|
bal = _multicastRateAccounts.insert(std::pair< std::pair<Address,MulticastGroup>,BandwidthAccount >(k,BandwidthAccount(r.preload,r.maxBalance,r.accrual))).first;
|
||||||
}
|
}
|
||||||
return bal->second.update((int32_t)bytes).second;
|
return (bal->second.update((int32_t)bytes) >= 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Add table
Reference in a new issue