Created
November 17, 2017 14:32
-
-
Save dinigo/0d064221e9ef51e13d98a52a4df8e5b4 to your computer and use it in GitHub Desktop.
Request rate limiter mok implementation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// libs | |
const request = require('request'); | |
const {RateLimiter} = require('limiter'); | |
// config | |
const pool = {maxSockets: 100}; | |
const url = 'http://google.com'; | |
const numRequests = 10000; | |
const requestLimit = 5000; | |
const limiter = new RateLimiter(requestLimit, 'hour'); | |
let completed = 0; | |
// request function | |
function doRequest(){ | |
return new Promise((good, bad) => { | |
request({url , pool}, (err, res) => err? bad(err) : good(res)); | |
}; | |
} | |
// limited request function | |
function doLimitedRequest(){ | |
limiter.removeTokens(1, function(err, remaining) { | |
const timestamp = (new Date()).toTimeString().split(' ')[0]; | |
doRequest().then(() => { | |
completed++; | |
if(completed % 100 === 0) console.log(timestamp + ' completed: ' + ~~completed); | |
}); | |
}); | |
} | |
// run the requests | |
for(let i=0; i<numRequests; i++){ | |
doLimitedRequest(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install dependencies with
yarn add request limiter
and run the script withnode requester.js