tokenBucket.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * A hierarchical token bucket for rate limiting. See
  3. * http://en.wikipedia.org/wiki/Token_bucket for more information.
  4. * @author John Hurliman <jhurliman@cull.tv>
  5. *
  6. * @param {Number} bucketSize Maximum number of tokens to hold in the bucket.
  7. * Also known as the burst rate.
  8. * @param {Number} tokensPerInterval Number of tokens to drip into the bucket
  9. * over the course of one interval.
  10. * @param {String|Number} interval The interval length in milliseconds, or as
  11. * one of the following strings: 'second', 'minute', 'hour', day'.
  12. * @param {TokenBucket} parentBucket Optional. A token bucket that will act as
  13. * the parent of this bucket.
  14. */
  15. var TokenBucket = function(bucketSize, tokensPerInterval, interval, parentBucket) {
  16. this.bucketSize = bucketSize;
  17. this.tokensPerInterval = tokensPerInterval;
  18. if (typeof interval === 'string') {
  19. switch (interval) {
  20. case 'sec': case 'second':
  21. this.interval = 1000; break;
  22. case 'min': case 'minute':
  23. this.interval = 1000 * 60; break;
  24. case 'hr': case 'hour':
  25. this.interval = 1000 * 60 * 60; break;
  26. case 'day':
  27. this.interval = 1000 * 60 * 60 * 24; break;
  28. default:
  29. throw new Error('Invaid interval ' + interval);
  30. }
  31. } else {
  32. this.interval = interval;
  33. }
  34. this.parentBucket = parentBucket;
  35. this.content = 0;
  36. this.lastDrip = +new Date();
  37. };
  38. TokenBucket.prototype = {
  39. bucketSize: 1,
  40. tokensPerInterval: 1,
  41. interval: 1000,
  42. parentBucket: null,
  43. content: 0,
  44. lastDrip: 0,
  45. /**
  46. * Remove the requested number of tokens and fire the given callback. If the
  47. * bucket (and any parent buckets) contains enough tokens this will happen
  48. * immediately. Otherwise, the removal and callback will happen when enough
  49. * tokens become available.
  50. * @param {Number} count The number of tokens to remove.
  51. * @param {Function} callback(err, remainingTokens)
  52. * @returns {Boolean} True if the callback was fired immediately, otherwise
  53. * false.
  54. */
  55. removeTokens: function(count, callback) {
  56. var self = this;
  57. // Is this an infinite size bucket?
  58. if (!this.bucketSize) {
  59. process.nextTick(callback.bind(null, null, count, Number.POSITIVE_INFINITY));
  60. return true;
  61. }
  62. // Make sure the bucket can hold the requested number of tokens
  63. if (count > this.bucketSize) {
  64. process.nextTick(callback.bind(null, 'Requested tokens ' + count + ' exceeds bucket size ' +
  65. this.bucketSize, null));
  66. return false;
  67. }
  68. // Drip new tokens into this bucket
  69. this.drip();
  70. // If we don't have enough tokens in this bucket, come back later
  71. if (count > this.content)
  72. return comeBackLater();
  73. if (this.parentBucket) {
  74. // Remove the requested from the parent bucket first
  75. return this.parentBucket.removeTokens(count, function(err, remainingTokens) {
  76. if (err) return callback(err, null);
  77. // Check that we still have enough tokens in this bucket
  78. if (count > self.content)
  79. return comeBackLater();
  80. // Tokens were removed from the parent bucket, now remove them from
  81. // this bucket and fire the callback. Note that we look at the current
  82. // bucket and parent bucket's remaining tokens and return the smaller
  83. // of the two values
  84. self.content -= count;
  85. callback(null, Math.min(remainingTokens, self.content));
  86. });
  87. } else {
  88. // Remove the requested tokens from this bucket and fire the callback
  89. this.content -= count;
  90. process.nextTick(callback.bind(null, null, this.content));
  91. return true;
  92. }
  93. function comeBackLater() {
  94. // How long do we need to wait to make up the difference in tokens?
  95. var waitInterval = Math.ceil(
  96. (count - self.content) * (self.interval / self.tokensPerInterval));
  97. setTimeout(function() { self.removeTokens(count, callback); }, waitInterval);
  98. return false;
  99. }
  100. },
  101. /**
  102. * Attempt to remove the requested number of tokens and return immediately.
  103. * If the bucket (and any parent buckets) contains enough tokens this will
  104. * return true, otherwise false is returned.
  105. * @param {Number} count The number of tokens to remove.
  106. * @param {Boolean} True if the tokens were successfully removed, otherwise
  107. * false.
  108. */
  109. tryRemoveTokens: function(count) {
  110. // Is this an infinite size bucket?
  111. if (!this.bucketSize)
  112. return true;
  113. // Make sure the bucket can hold the requested number of tokens
  114. if (count > this.bucketSize)
  115. return false;
  116. // Drip new tokens into this bucket
  117. this.drip();
  118. // If we don't have enough tokens in this bucket, return false
  119. if (count > this.content)
  120. return false;
  121. // Try to remove the requested tokens from the parent bucket
  122. if (this.parentBucket && !this.parentBucket.tryRemoveTokens(count))
  123. return false;
  124. // Remove the requested tokens from this bucket and return
  125. this.content -= count;
  126. return true;
  127. },
  128. /**
  129. * Add any new tokens to the bucket since the last drip.
  130. * @returns {Boolean} True if new tokens were added, otherwise false.
  131. */
  132. drip: function() {
  133. if (!this.tokensPerInterval) {
  134. this.content = this.bucketSize;
  135. return;
  136. }
  137. var now = +new Date();
  138. var deltaMS = Math.max(now - this.lastDrip, 0);
  139. this.lastDrip = now;
  140. var dripAmount = deltaMS * (this.tokensPerInterval / this.interval);
  141. this.content = Math.min(this.content + dripAmount, this.bucketSize);
  142. }
  143. };
  144. module.exports = TokenBucket;