{"maintainers":[{"name":"jhurliman","email":"jhurliman@cull.tv"}],"keywords":["rate","limiting","throttling"],"dist-tags":{"latest":"1.1.0"},"author":{"name":"John Hurliman","email":"jhurliman@jhurliman.org"},"description":"A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled","readme":"# limiter #\n\n[![Build Status](https://travis-ci.org/jhurliman/node-rate-limiter.png)](https://travis-ci.org/jhurliman/node-rate-limiter)\n[![NPM Downloads](https://img.shields.io/npm/dm/rate-limiter.svg?style=flat)](https://www.npmjs.com/package/limiter)\n\nProvides a generic rate limiter for node.js. Useful for API clients, web \ncrawling, or other tasks that need to be throttled. Two classes are exposed, \nRateLimiter and TokenBucket. TokenBucket provides a lower level interface to \nrate limiting with a configurable burst rate and drip rate. RateLimiter sits \non top of the token bucket and adds a restriction on the maximum number of \ntokens that can be removed each interval to comply with common API \nrestrictions like \"150 requests per hour maximum\".\n\n## Installation ##\n\nUse NPM to install:\n\n    npm install limiter\n\n## Usage ##\n\nA simple example allowing 150 requests per hour:\n\n```javascript\nvar RateLimiter = require('limiter').RateLimiter;\n// Allow 150 requests per hour (the Twitter search limit). Also understands\n// 'second', 'minute', 'day', or a number of milliseconds\nvar limiter = new RateLimiter(150, 'hour');\n\n// Throttle requests\nlimiter.removeTokens(1, function(err, remainingRequests) {\n  // err will only be set if we request more than the maximum number of\n  // requests we set in the constructor\n  \n  // remainingRequests tells us how many additional requests could be sent\n  // right this moment\n  \n  callMyRequestSendingFunction(...);\n});\n```\n\nAnother example allowing one message to be sent every 250ms:\n\n```javascript\nvar RateLimiter = require('limiter').RateLimiter;\nvar limiter = new RateLimiter(1, 250);\n\nlimiter.removeTokens(1, function() {\n  callMyMessageSendingFunction(...);\n});\n```\n\nThe default behaviour is to wait for the duration of the rate limiting\nthat’s currently in effect before the callback is fired, but if you \npass in ```true``` as the third parameter, the callback will be fired \nimmediately with remainingRequests set to -1:\n\n```javascript\nvar RateLimiter = require('limiter').RateLimiter;\nvar limiter = new RateLimiter(150, 'hour', true);  // fire CB immediately\n\n// Immediately send 429 header to client when rate limiting is in effect\nlimiter.removeTokens(1, function(err, remainingRequests) {\n  if (remainingRequests < 0) {\n    response.writeHead(429, {'Content-Type': 'text/plain;charset=UTF-8'});\n    response.end('429 Too Many Requests - your IP is being rate limited');\n  } else {\n    callMyMessageSendingFunction(...);\n  }\n});\n```\n\nA synchronous method, tryRemoveTokens(), is available in both RateLimiter and TokenBucket. This will return immediately with a boolean value indicating if the token removal was successful.\n```javascript\nvar RateLimiter = require('limiter').RateLimiter;\nvar limiter = new RateLimiter(10, 'second');\n\nif (limiter.tryRemoveTokens(5))\n  console.log('Tokens removed');\nelse\n  console.log('No tokens removed');\n```\n\nTo get the number of remaining tokens **outside** the `removeTokens`-callback\nsimply use the `getTokensRemaining`-method.\n```javascript\nvar RateLimiter = require('limiter').RateLimiter;\nvar limiter = new RateLimiter(1, 250);\n\n// returns 1 since we did not remove a token and our number of tokens per interval is 1\nlimiter.getTokensRemaining();\n```\n\nUsing the token bucket directly to throttle at the byte level:\n\n```javascript\nvar BURST_RATE = 1024 * 1024 * 150; // 150KB/sec burst rate\nvar FILL_RATE = 1024 * 1024 * 50; // 50KB/sec sustained rate\nvar TokenBucket = require('limiter').TokenBucket;\n// We could also pass a parent token bucket in as the last parameter to\n// create a hierarchical token bucket\nvar bucket = new TokenBucket(BURST_RATE, FILL_RATE, 'second', null);\n\nbucket.removeTokens(myData.byteLength, function() {\n  sendMyData(myData);\n});\n```\n\n## Additional Notes ##\n\nBoth the token bucket and rate limiter should be used with a message queue or \nsome way of preventing multiple simultaneous calls to removeTokens(). \nOtherwise, earlier messages may get held up for long periods of time if more \nrecent messages are continually draining the token bucket. This can lead to \nout of order messages or the appearance of \"lost\" messages under heavy load.\n\n## License ##\n\n(The MIT License)\n\nCopyright (c) 2013 John Hurliman. &lt;jhurliman@jhurliman.org&gt;\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","repository":{"type":"git","url":"git://github.com/jhurliman/node-rate-limiter.git"},"users":{"zeke":true,"mimmo1":true,"joakin":true,"shahzaib":true,"jpepe":true,"robruffler":true,"cliff":true,"markthethomas":true,"panlw":true,"tarkeshwar":true,"andersonsantos":true,"gihankarunarathne":true,"blitzprog":true,"milfromoz":true,"spiros.politis":true,"djjaron":true,"ahsanshafiq":true,"maxidev":true,"jovinbm":true,"joypeterson":true,"scotchulous":true,"firerishi":true,"quocnguyen":true,"markstos":true},"bugs":{"url":"http://github.com/jhurliman/node-rate-limiter/issues"},"versions":{"0.0.2":{"name":"limiter","description":"A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled","version":"0.0.2","author":{"name":"John Hurliman","email":"jhurliman@cull.tv"},"dependencies":{},"devDependencies":{},"keywords":["rate","limiting","throttling"],"repository":{"type":"git","url":"git://github.com/jhurliman/node-rate-limiter.git"},"bugs":{"url":"http://github.com/jhurliman/node-rate-limiter/issues"},"directories":{"lib":"./lib/"},"main":"./index.js","licenses":[{"type":"MIT","url":"http://github.com/jhurliman/node-rate-limiter/raw/master/LICENSE.txt"}],"_npmUser":{"name":"jhurliman","email":"jhurliman@cull.tv"},"_id":"limiter@0.0.2","optionalDependencies":{},"engines":{"node":"*"},"_engineSupported":true,"_npmVersion":"1.1.4","_nodeVersion":"v0.6.12","_defaultsLoaded":true,"dist":{"shasum":"784da91388a1f6d9a1d056e44f8f253f1b0d9abe","tarball":"http://nexus.dui88.com:8081/nexus/content/groups/npm-all/limiter/-/limiter-0.0.2.tgz"},"maintainers":[{"name":"jhurliman","email":"jhurliman@cull.tv"}]},"1.0.0":{"name":"limiter","description":"A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled","version":"1.0.0","author":{"name":"John Hurliman","email":"jhurliman@cull.tv"},"dependencies":{},"devDependencies":{"assert":"0.4.9","vows":"0.6.3"},"keywords":["rate","limiting","throttling"],"repository":{"type":"git","url":"git://github.com/jhurliman/node-rate-limiter"},"bugs":{"url":"http://github.com/jhurliman/node-rate-limiter/issues"},"directories":{"lib":"./lib/"},"main":"./index.js","licenses":[{"type":"MIT","url":"http://github.com/jhurliman/node-rate-limiter/raw/master/LICENSE.txt"}],"_id":"limiter@1.0.0","dist":{"shasum":"017dd406145e0ff058ac9b72ba2525543dda716a","tarball":"http://nexus.dui88.com:8081/nexus/content/groups/npm-all/limiter/-/limiter-1.0.0.tgz"},"maintainers":[{"name":"jhurliman","email":"jhurliman@cull.tv"}]},"1.0.1":{"name":"limiter","description":"A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled","version":"1.0.1","author":{"name":"John Hurliman","email":"jhurliman@cull.tv"},"dependencies":{},"devDependencies":{"assert":"0.4.9","vows":"0.6.3"},"keywords":["rate","limiting","throttling"],"repository":{"type":"git","url":"git://github.com/jhurliman/node-rate-limiter"},"bugs":{"url":"http://github.com/jhurliman/node-rate-limiter/issues"},"directories":{"lib":"./lib/"},"main":"./index.js","licenses":[{"type":"MIT","url":"http://github.com/jhurliman/node-rate-limiter/raw/master/LICENSE.txt"}],"_id":"limiter@1.0.1","dist":{"shasum":"72b87782ca42a2a8adc1064aa718c22596c8a7aa","tarball":"http://nexus.dui88.com:8081/nexus/content/groups/npm-all/limiter/-/limiter-1.0.1.tgz"},"_npmVersion":"1.1.61","_npmUser":{"name":"jhurliman","email":"jhurliman@jhurliman.org"},"maintainers":[{"name":"jhurliman","email":"jhurliman@cull.tv"}]},"1.0.2":{"name":"limiter","description":"A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled","version":"1.0.2","author":{"name":"John Hurliman","email":"jhurliman@cull.tv"},"dependencies":{},"devDependencies":{"assert":"0.4.9","vows":"0.6.3"},"keywords":["rate","limiting","throttling"],"repository":{"type":"git","url":"git://github.com/jhurliman/node-rate-limiter"},"bugs":{"url":"http://github.com/jhurliman/node-rate-limiter/issues"},"directories":{"lib":"./lib/"},"main":"./index.js","licenses":[{"type":"MIT","url":"http://github.com/jhurliman/node-rate-limiter/raw/master/LICENSE.txt"}],"_id":"limiter@1.0.2","dist":{"shasum":"dd89358fccd22facf8372b2f922e4bf33e503d52","tarball":"http://nexus.dui88.com:8081/nexus/content/groups/npm-all/limiter/-/limiter-1.0.2.tgz"},"_npmVersion":"1.1.61","_npmUser":{"name":"jhurliman","email":"jhurliman@jhurliman.org"},"maintainers":[{"name":"jhurliman","email":"jhurliman@cull.tv"}]},"1.0.3":{"name":"limiter","description":"A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled","version":"1.0.3","author":{"name":"John Hurliman","email":"jhurliman@cull.tv"},"dependencies":{},"devDependencies":{"assert":"0.4.9","vows":"0.6.3"},"keywords":["rate","limiting","throttling"],"repository":{"type":"git","url":"git://github.com/jhurliman/node-rate-limiter"},"bugs":{"url":"http://github.com/jhurliman/node-rate-limiter/issues"},"directories":{"lib":"./lib/"},"main":"./index.js","licenses":[{"type":"MIT","url":"http://github.com/jhurliman/node-rate-limiter/raw/master/LICENSE.txt"}],"_id":"limiter@1.0.3","dist":{"shasum":"160e7d2c614d05c735d24dc33ccc12a836d1b07a","tarball":"http://nexus.dui88.com:8081/nexus/content/groups/npm-all/limiter/-/limiter-1.0.3.tgz"},"_npmVersion":"1.1.61","_npmUser":{"name":"jhurliman","email":"jhurliman@jhurliman.org"},"maintainers":[{"name":"jhurliman","email":"jhurliman@cull.tv"}]},"1.0.4":{"name":"limiter","description":"A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled","version":"1.0.4","author":{"name":"John Hurliman","email":"jhurliman@cull.tv"},"dependencies":{},"devDependencies":{"assert":"0.4.9","vows":"0.6.3"},"keywords":["rate","limiting","throttling"],"repository":{"type":"git","url":"git://github.com/jhurliman/node-rate-limiter"},"bugs":{"url":"http://github.com/jhurliman/node-rate-limiter/issues"},"directories":{"lib":"./lib/"},"main":"./index.js","licenses":[{"type":"MIT","url":"http://github.com/jhurliman/node-rate-limiter/raw/master/LICENSE.txt"}],"_id":"limiter@1.0.4","dist":{"shasum":"441aea9385f807681e29bb606eb6e6bd05f6bd82","tarball":"http://nexus.dui88.com:8081/nexus/content/groups/npm-all/limiter/-/limiter-1.0.4.tgz"},"_from":".","_npmVersion":"1.3.2","_npmUser":{"name":"jhurliman","email":"jhurliman@jhurliman.org"},"maintainers":[{"name":"jhurliman","email":"jhurliman@cull.tv"}]},"1.0.5":{"name":"limiter","description":"A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled","version":"1.0.5","author":{"name":"John Hurliman","email":"jhurliman@cull.tv"},"dependencies":{},"devDependencies":{"assert":"0.4.9","vows":"0.6.3"},"keywords":["rate","limiting","throttling"],"repository":{"type":"git","url":"git://github.com/jhurliman/node-rate-limiter"},"bugs":{"url":"http://github.com/jhurliman/node-rate-limiter/issues"},"directories":{"lib":"./lib/"},"main":"./index.js","licenses":[{"type":"MIT","url":"http://github.com/jhurliman/node-rate-limiter/raw/master/LICENSE.txt"}],"_id":"limiter@1.0.5","dist":{"shasum":"9630b2a0d3bad63203f96e3d96f32f83d442dfc8","tarball":"http://nexus.dui88.com:8081/nexus/content/groups/npm-all/limiter/-/limiter-1.0.5.tgz"},"_from":".","_npmVersion":"1.3.2","_npmUser":{"name":"jhurliman","email":"jhurliman@jhurliman.org"},"maintainers":[{"name":"jhurliman","email":"jhurliman@cull.tv"}]},"1.1.0":{"name":"limiter","description":"A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled","version":"1.1.0","author":{"name":"John Hurliman","email":"jhurliman@jhurliman.org"},"scripts":{"test":"vows --spec"},"dependencies":{},"devDependencies":{"assert":"1.3.0","vows":"0.8.1"},"keywords":["rate","limiting","throttling"],"repository":{"type":"git","url":"git://github.com/jhurliman/node-rate-limiter.git"},"bugs":{"url":"http://github.com/jhurliman/node-rate-limiter/issues"},"directories":{"lib":"./lib/"},"main":"./index.js","licenses":[{"type":"MIT","url":"http://github.com/jhurliman/node-rate-limiter/raw/master/LICENSE.txt"}],"gitHead":"bf4286a31db48dec2a1a0029bf2ab703dd016b12","homepage":"https://github.com/jhurliman/node-rate-limiter#readme","_id":"limiter@1.1.0","_shasum":"6e2bd12ca3fcdaa11f224e2e53c896df3f08d913","_from":".","_npmVersion":"3.3.5","_nodeVersion":"4.1.0","_npmUser":{"name":"jhurliman","email":"jhurliman@jhurliman.org"},"dist":{"shasum":"6e2bd12ca3fcdaa11f224e2e53c896df3f08d913","tarball":"http://nexus.dui88.com:8081/nexus/content/groups/npm-all/limiter/-/limiter-1.1.0.tgz"},"maintainers":[{"name":"jhurliman","email":"jhurliman@cull.tv"}]}},"name":"limiter","time":{"modified":"2017-04-24T13:16:28.022Z","created":"2012-04-17T00:38:04.350Z","0.0.2":"2012-04-17T00:38:05.679Z","1.0.0":"2012-07-17T20:10:01.194Z","1.0.1":"2013-02-26T06:39:53.518Z","1.0.2":"2013-02-26T17:14:37.773Z","1.0.3":"2013-03-20T19:00:10.384Z","1.0.4":"2013-08-02T03:47:13.585Z","1.0.5":"2013-12-31T20:38:01.470Z","1.1.0":"2015-10-28T17:43:46.389Z"},"readmeFilename":"README.md","homepage":"https://github.com/jhurliman/node-rate-limiter#readme"}