router.get('/http_stats', (req, res) => { res.json({ http: { sockets: socketStats(http.globalAgent.sockets), freeSockets: socketStats(http.globalAgent.freeSockets), }, https: { sockets: socketStats(https.globalAgent.sockets), freeSockets: socketStats(https.globalAgent.freeSockets), }, }); });
// NOTE: A rewrite occurred in 0.11 that changed the addRequest signature // from (req, host, port, localAddress) to (req, options) // Here, I use the longer signature to maintain 0.10 support, even though // the rest of the arguments aren't actually used wrap(http.Agent.prototype, 'addRequest', function (original) { return function (req) { var onSocket = req.onSocket req.onSocket = ins.bindFunction(function (socket) { patchOnRead(socket) return onSocket.apply(this, arguments) }) return original.apply(this, arguments) } })
//Not a clean implementation (Only for demonstarting the capabilities) keepAliveAgent.on('free', (socket) => { console.log('FREE: agent socket freed'); if (socket._startTimer) { let aliveTime = process.hrtime(socket._startTimer); aliveTime = Math.round((aliveTime[0] * 1e9 + aliveTime[1]) / 1e6) console.log("Socket has been alive for time = ", aliveTime); if (aliveTime > 50000) { console.log("Socket destroyed after alive time = ", aliveTime); //Steps to do before destroy : // 1. make sure the socket is not being used by a request. // 2. Clear the socket from the freeSockets array. socket.destroy(); return; } } //Destroy the Socket if its been used more than 5 times if (socket._counter > 5) { console.log("Socket destroyed after usage count = ", socket._counter); socket.destroy(); } });
/** * An HTTP Agent for proxying requests through Tor using SOCKS5. * * @param {object} opts */ function TorAgent(opts) { if (!(this instanceof TorAgent)) { return new TorAgent(); } http.Agent.call(this, opts); this.socksHost = opts.socksHost || 'localhost'; this.socksPort = opts.socksPort || 9050; this.defaultPort = 80; // Used when invoking TorAgent.create this.tor = opts.tor; // Prevent protocol check, wrap destroy this.protocol = null; this.defaultDestroy = this.destroy; this.destroy = this.destroyWrapper; }
}; expect(http.globalAgent.maxSockets).toEqual(Infinity); expect(http.globalAgent.maxSockets).toEqual(100); expect(app.set).toBeCalledWith('port', options.port); expect(configManager.configManager).toBeCalledWith('logging');
console.log(`Request ${i}`); console.log('\n---> Before Request'); if (keepAliveAgent.freeSockets[servicename]) { console.log(`freeSockets : ${keepAliveAgent.freeSockets[servicename].length}`); if (keepAliveAgent.sockets[servicename]) { console.log(`sockets : ${(keepAliveAgent.sockets[servicename].length)}`); console.log('END: No more data in response.'); console.log('\n---> response end'); if (keepAliveAgent.freeSockets[servicename]) { console.log(`freeSockets : ${keepAliveAgent.freeSockets[servicename].length}`); if (keepAliveAgent.requests[servicename]) { console.log(`requests : ${keepAliveAgent.requests[servicename].length}`); if (keepAliveAgent.sockets[servicename]) { console.log(`sockets : ${(keepAliveAgent.sockets[servicename].length)}`); if (keepAliveAgent.freeSockets[servicename]) { console.log(`freeSockets : ${keepAliveAgent.freeSockets[servicename].length}`); if (keepAliveAgent.sockets[servicename]) { console.log(`sockets : ${(keepAliveAgent.sockets[servicename].length)}`);