Created
July 6, 2015 16:51
-
-
Save cmawhorter/98c2a71ed32fab9b3277 to your computer and use it in GitHub Desktop.
node http-proxy leaks somewhere. presumably sockets
This file contains hidden or 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
| process.title = 'node http-proxy'; | |
| var CONCURRENCY = 250; | |
| var assert = require('assert') | |
| , crypto = require('crypto') | |
| , http = require('http'); | |
| http.globalAgent.maxSockets = Infinity; | |
| var httpProxy = require('http-proxy'); | |
| var proxy = httpProxy.createProxyServer({ | |
| target: 'http://127.0.0.1:8082' | |
| }); | |
| // proxy.on('proxyRes', function (proxyRes, req, res) { | |
| // proxyRes.abort(); | |
| // res.end(req.url.substr(1)); | |
| // }); | |
| var clientServer = http.createServer(function requestHandler(req, res) { | |
| proxy.web(req, res); | |
| }); | |
| var originServer = http.createServer(function(req, res) { | |
| res.end('Response'); | |
| }); | |
| clientServer.listen('8087'); | |
| originServer.listen('8082'); | |
| var completedRequests = 0; | |
| var activeRequests = 0; | |
| var totalTime = 0; | |
| function request() { | |
| var start = new Date().getTime(); | |
| activeRequests++; | |
| var uid = crypto.randomBytes(16).toString('hex'); | |
| var requestUrl = 'http://127.0.0.1:8087/' + uid; | |
| http.request(requestUrl, function(res) { | |
| var chunks = []; | |
| res.on('data', Array.prototype.push.bind(chunks)); | |
| res.on('end', function() { | |
| // console.log('%s -> %s = %s', requestUrl, uid, chunks.join('')); | |
| // assert.strictEqual(uid, chunks.join('')); | |
| activeRequests--; | |
| completedRequests++; | |
| totalTime += new Date().getTime() - start; | |
| process.nextTick(request); | |
| }); | |
| }).end(); | |
| } | |
| for (var i=0; i < CONCURRENCY; i++) { | |
| request(); | |
| } | |
| setInterval(function() { | |
| console.log('Completed: %s, Active: %s/%s, Avg. Response: %ss', completedRequests, activeRequests, CONCURRENCY, ((totalTime / completedRequests) / 1000).toFixed(2)); | |
| }, 1500); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment