RateLimiter.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. * Burstable rate limiter
  39. *
  40. * This limits a transfer rate to a maximum bytes per second using an
  41. * accounting method based on a balance rather than accumulating an
  42. * average rate. The result is a burstable rate limit rather than a
  43. * continuous rate limit; the link being limited may use all its balance
  44. * at once or slowly over time. Balance constantly replenishes over time
  45. * up to a configurable maximum balance.
  46. */
  47. class RateLimiter
  48. {
  49. public:
  50. /**
  51. * Limits to apply to a rate limiter
  52. *
  53. * Since many rate limiters may share the same fixed limit values,
  54. * save memory by breaking this out into a struct parameter that
  55. * can be passed into RateLimiter's methods.
  56. */
  57. struct Limit
  58. {
  59. /**
  60. * Speed in bytes per second, or rate of balance accrual
  61. */
  62. double bytesPerSecond;
  63. /**
  64. * Maximum balance that can ever be accrued (should be > 0.0)
  65. */
  66. double maxBalance;
  67. /**
  68. * Minimum balance, or maximum allowable "debt" (should be <= 0.0)
  69. */
  70. double minBalance;
  71. };
  72. /**
  73. * Create an uninitialized rate limiter
  74. *
  75. * init() must be called before this is used.
  76. */
  77. RateLimiter() throw() {}
  78. /**
  79. * @param preload Initial balance to place in account
  80. */
  81. RateLimiter(double preload)
  82. throw()
  83. {
  84. init(preload);
  85. }
  86. /**
  87. * Initialize or re-initialize rate limiter
  88. *
  89. * @param preload Initial balance to place in account
  90. */
  91. inline void init(double preload)
  92. throw()
  93. {
  94. _lastTime = Utils::nowf();
  95. _balance = preload;
  96. }
  97. /**
  98. * Update balance based on current clock and supplied Limits bytesPerSecond and maxBalance
  99. *
  100. * @param lim Current limits in effect
  101. * @return New balance
  102. */
  103. inline double updateBalance(const Limit &lim)
  104. throw()
  105. {
  106. double lt = _lastTime;
  107. double now = _lastTime = Utils::nowf();
  108. return (_balance = fmin(lim.maxBalance,_balance + (lim.bytesPerSecond * (now - lt))));
  109. }
  110. /**
  111. * Update balance and test if a block of 'bytes' should be permitted to be transferred
  112. *
  113. * @param lim Current limits in effect
  114. * @param bytes Number of bytes that we wish to transfer
  115. * @return True if balance was sufficient
  116. */
  117. inline bool gate(const Limit &lim,double bytes)
  118. throw()
  119. {
  120. bool allow = (updateBalance(lim) >= bytes);
  121. _balance = fmax(lim.minBalance,_balance - bytes);
  122. return allow;
  123. }
  124. /**
  125. * @return Current balance
  126. */
  127. inline double balance() const
  128. throw()
  129. {
  130. return _balance;
  131. }
  132. private:
  133. double _lastTime;
  134. double _balance;
  135. };
  136. } // namespace ZeroTier
  137. #endif