RateLimiter.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * Create an uninitialized rate limiter
  47. *
  48. * init() must be called before this is used.
  49. */
  50. RateLimiter() throw() {}
  51. /**
  52. * @param bytesPerSecond Bytes per second to permit (average)
  53. * @param preload Initial balance to place in account
  54. * @param max Maximum balance to permit to ever accrue (max burst)
  55. */
  56. RateLimiter(double bytesPerSecond,double preload,double max)
  57. throw()
  58. {
  59. init(bytesPerSecond,preload,max);
  60. }
  61. /**
  62. * Initialize or re-initialize rate limiter
  63. *
  64. * @param bytesPerSecond Bytes per second to permit (average)
  65. * @param preload Initial balance to place in account
  66. * @param max Maximum balance to permit to ever accrue (max burst)
  67. */
  68. inline void init(double bytesPerSecond,double preload,double max)
  69. throw()
  70. {
  71. _bytesPerSecond = bytesPerSecond;
  72. _lastTime = Utils::nowf();
  73. _balance = preload;
  74. _max = max;
  75. }
  76. /**
  77. * Update balance based on current clock
  78. *
  79. * This can be called at any time to check the current balance without
  80. * affecting the behavior of gate().
  81. *
  82. * @return New balance
  83. */
  84. inline double updateBalance()
  85. throw()
  86. {
  87. double now = Utils::nowf();
  88. double b = _balance = fmin(_max,_balance + (_bytesPerSecond * (now - _lastTime)));
  89. _lastTime = now;
  90. return b;
  91. }
  92. /**
  93. * Test balance and update / deduct if there is enough to transfer 'bytes'
  94. *
  95. * @param bytes Number of bytes that we wish to transfer
  96. * @return True if balance was sufficient (balance is updated), false if not (balance unchanged)
  97. */
  98. inline bool gate(double bytes)
  99. throw()
  100. {
  101. if (updateBalance() >= bytes) {
  102. _balance -= bytes;
  103. return true;
  104. }
  105. return false;
  106. }
  107. private:
  108. double _bytesPerSecond;
  109. double _lastTime;
  110. double _balance;
  111. double _max;
  112. };
  113. } // namespace ZeroTier
  114. #endif