BandwidthAccount.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef _ZT_BWACCOUNT_HPP
  28. #define _ZT_BWACCOUNT_HPP
  29. #include <stdint.h>
  30. #include <math.h>
  31. #include <algorithm>
  32. #include <utility>
  33. #include "Constants.hpp"
  34. #include "Utils.hpp"
  35. namespace ZeroTier {
  36. /**
  37. * Bandwidth account used for rate limiting multicast groups
  38. *
  39. * This is used to apply a bank account model to multicast groups. Each
  40. * multicast packet counts against a balance, which accrues at a given
  41. * rate in bytes per second. Debt is possible. These parameters are
  42. * configurable.
  43. *
  44. * A bank account model permits bursting behavior, which correctly models
  45. * how OSes and apps typically use multicast. It's common for things to
  46. * spew lots of multicast messages at once, wait a while, then do it
  47. * again. A consistent bandwidth limit model doesn't fit.
  48. */
  49. class BandwidthAccount
  50. {
  51. public:
  52. /**
  53. * Create an uninitialized account
  54. *
  55. * init() must be called before this is used.
  56. */
  57. BandwidthAccount() throw() {}
  58. /**
  59. * Create and initialize
  60. *
  61. * @param preload Initial balance to place in account
  62. * @param minb Minimum allowed balance (or maximum debt) (<= 0)
  63. * @param maxb Maximum allowed balance (> 0)
  64. * @param acc Rate of accrual in bytes per second
  65. */
  66. BandwidthAccount(int32_t preload,int32_t minb,int32_t maxb,int32_t acc)
  67. throw()
  68. {
  69. init(preload,minb,maxb,acc);
  70. }
  71. /**
  72. * Initialize or re-initialize account
  73. *
  74. * @param preload Initial balance to place in account
  75. * @param minb Minimum allowed balance (or maximum debt) (<= 0)
  76. * @param maxb Maximum allowed balance (> 0)
  77. * @param acc Rate of accrual in bytes per second
  78. */
  79. inline void init(int32_t preload,int32_t minb,int32_t maxb,int32_t acc)
  80. throw()
  81. {
  82. _lastTime = Utils::nowf();
  83. _balance = preload;
  84. _minBalance = minb;
  85. _maxBalance = maxb;
  86. _accrual = acc;
  87. }
  88. /**
  89. * Update balance by accruing and then deducting
  90. *
  91. * @param deduct Amount to deduct, or 0.0 to just update
  92. * @return New balance with deduction applied, and whether or not deduction fit
  93. */
  94. inline std::pair<int32_t,bool> update(int32_t deduct)
  95. throw()
  96. {
  97. double lt = _lastTime;
  98. double now = Utils::nowf();
  99. _lastTime = now;
  100. int32_t newbal = (int32_t)round((double)_balance + ((double)_accrual * (now - lt))) - deduct;
  101. bool fits = (newbal > 0);
  102. return std::pair<int32_t,bool>((_balance = std::max(_minBalance,std::min(_maxBalance,newbal))),fits);
  103. }
  104. private:
  105. double _lastTime;
  106. int32_t _balance;
  107. int32_t _minBalance;
  108. int32_t _maxBalance;
  109. int32_t _accrual;
  110. };
  111. } // namespace ZeroTier
  112. #endif