RateLimiter.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_RATELIMITER_HPP
  28. #define _ZT_RATELIMITER_HPP
  29. #include <math.h>
  30. #include "Constants.hpp"
  31. #include "Utils.hpp"
  32. #ifdef __WINDOWS__
  33. #define fmin(a,b) (((a) <= (b)) ? (a) : (b))
  34. #define fmax(a,b) (((a) >= (b)) ? (a) : (b))
  35. #endif
  36. namespace ZeroTier {
  37. /**
  38. * Data transfer accounting used for 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 RateLimiter
  51. {
  52. public:
  53. /**
  54. * Rate and min/max to apply on rate limiter update
  55. */
  56. struct Rate
  57. {
  58. /**
  59. * Rate of balance accrual in bytes per second
  60. */
  61. double bytesPerSecond;
  62. /**
  63. * Maximum balance that can ever be accrued (should be > 0.0)
  64. */
  65. double maxBalance;
  66. /**
  67. * Minimum balance, or maximum allowable "debt" (should be <= 0.0)
  68. */
  69. double minBalance;
  70. };
  71. /**
  72. * Create an uninitialized rate limiter
  73. *
  74. * init() must be called before this is used.
  75. */
  76. RateLimiter() throw() {}
  77. /**
  78. * Create an initialize rate limiter
  79. *
  80. * @param preload Initial balance to place in account
  81. */
  82. RateLimiter(double preload)
  83. throw()
  84. {
  85. init(preload);
  86. }
  87. /**
  88. * Initialize or re-initialize rate limiter
  89. *
  90. * @param preload Initial balance to place in account
  91. */
  92. inline void init(double preload)
  93. throw()
  94. {
  95. _lastTime = Utils::nowf();
  96. _balance = preload;
  97. }
  98. /**
  99. * Update balance based on current clock and supplied rate
  100. *
  101. * @param lim Current limits in effect
  102. * @param deduct Amount to deduct, or 0.0 to just update
  103. * @return New balance with deduction applied
  104. */
  105. inline double update(const Rate &r,double deduct)
  106. throw()
  107. {
  108. double lt = _lastTime;
  109. double now = _lastTime = Utils::nowf();
  110. return (_balance = fmax(r.minBalance,fmin(r.maxBalance,(_balance + (r.bytesPerSecond * (now - lt))) - deduct)));
  111. }
  112. private:
  113. double _lastTime;
  114. double _balance;
  115. };
  116. } // namespace ZeroTier
  117. #endif