RateLimiter.hpp 3.5 KB

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