BandwidthAccount.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "Constants.hpp"
  32. #include "Utils.hpp"
  33. #ifdef __WINDOWS__
  34. #define round(x) ((x-floor(x))>0.5 ? ceil(x) : floor(x))
  35. #endif
  36. namespace ZeroTier {
  37. /**
  38. * Bandwidth account used for rate limiting multicast groups
  39. *
  40. * This is used to apply a bank account model to multicast groups. Each
  41. * multicast packet counts against a balance, which accrues at a given
  42. * rate in bytes per second. Debt is possible. These parameters are
  43. * configurable.
  44. *
  45. * A bank account model permits bursting behavior, which correctly models
  46. * how OSes and apps typically use multicast. It's common for things to
  47. * spew lots of multicast messages at once, wait a while, then do it
  48. * again. A consistent bandwidth limit model doesn't fit.
  49. */
  50. class BandwidthAccount
  51. {
  52. public:
  53. /**
  54. * Create an uninitialized account
  55. *
  56. * init() must be called before this is used.
  57. */
  58. BandwidthAccount() throw() {}
  59. /**
  60. * Create and initialize
  61. *
  62. * @param preload Initial balance to place in account
  63. * @param maxb Maximum allowed balance (> 0)
  64. * @param acc Rate of accrual in bytes per second
  65. */
  66. BandwidthAccount(uint32_t preload,uint32_t maxb,uint32_t acc)
  67. throw()
  68. {
  69. init(preload,maxb,acc);
  70. }
  71. /**
  72. * Initialize or re-initialize account
  73. *
  74. * @param preload Initial balance to place in account
  75. * @param maxb Maximum allowed balance (> 0)
  76. * @param acc Rate of accrual in bytes per second
  77. */
  78. inline void init(uint32_t preload,uint32_t maxb,uint32_t acc)
  79. throw()
  80. {
  81. _lastTime = Utils::nowf();
  82. _balance = preload;
  83. _maxBalance = maxb;
  84. _accrual = acc;
  85. }
  86. /**
  87. * Update and retrieve balance of this account
  88. *
  89. * @return New balance updated from current clock
  90. */
  91. inline uint32_t update()
  92. throw()
  93. {
  94. double lt = _lastTime;
  95. double now = Utils::nowf();
  96. _lastTime = now;
  97. return (_balance = std::min(_maxBalance,(uint32_t)round((double)_balance + ((double)_accrual * (now - lt)))));
  98. }
  99. /**
  100. * Update balance and conditionally deduct
  101. *
  102. * If the deduction amount fits, it is deducted after update. Otherwise
  103. * balance is updated and false is returned.
  104. *
  105. * @param amt Amount to deduct
  106. * @return True if amount fit within balance and was deducted
  107. */
  108. inline bool deduct(uint32_t amt)
  109. throw()
  110. {
  111. if (update() >= amt) {
  112. _balance -= amt;
  113. return true;
  114. }
  115. return false;
  116. }
  117. /**
  118. * @return Most recent balance without update
  119. */
  120. inline uint32_t balance() const
  121. throw()
  122. {
  123. return _balance;
  124. }
  125. private:
  126. double _lastTime;
  127. uint32_t _balance;
  128. uint32_t _maxBalance;
  129. uint32_t _accrual;
  130. };
  131. } // namespace ZeroTier
  132. #endif