BandwidthAccount.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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(int32_t preload,int32_t maxb,int32_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(int32_t preload,int32_t maxb,int32_t acc)
  76. throw()
  77. {
  78. _lastTime = Utils::nowf();
  79. _balance = preload;
  80. _maxBalance = maxb;
  81. _accrual = acc;
  82. }
  83. /**
  84. * Update balance by accruing and then deducting
  85. *
  86. * @param deduct Amount to deduct, or 0.0 to just update
  87. * @return New balance after deduction -- if negative, it didn't fit
  88. */
  89. inline int32_t update(int32_t deduct)
  90. throw()
  91. {
  92. double lt = _lastTime;
  93. double now = Utils::nowf();
  94. _lastTime = now;
  95. int32_t newbal = (int32_t)round((double)_balance + ((double)_accrual * (now - lt))) - deduct;
  96. _balance = std::max((int32_t)0,std::min(_maxBalance,newbal));
  97. return newbal;
  98. }
  99. private:
  100. double _lastTime;
  101. int32_t _balance;
  102. int32_t _maxBalance;
  103. int32_t _accrual;
  104. };
  105. } // namespace ZeroTier
  106. #endif