BandwidthAccount.hpp 3.6 KB

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