|
| 1 | +var sys = require('sys') |
| 2 | + , eyes = require('eyes') |
| 3 | + , http = require('http') |
| 4 | + , events = require('events') |
| 5 | + ; |
| 6 | + |
| 7 | +function Pool (port, host, https, credentials) { |
| 8 | + this.port = port; |
| 9 | + this.host = host; |
| 10 | + this.https = https; |
| 11 | + this.credentials = credentials; |
| 12 | + this.clients = []; |
| 13 | + this.pending = []; |
| 14 | + this.minClients = 0; |
| 15 | + this.maxClients = 8; |
| 16 | +}; |
| 17 | + |
| 18 | +sys.inherits(Pool, events.EventEmitter); |
| 19 | + |
| 20 | +Pool.prototype.getClient = function (cb) { |
| 21 | + for (var i=0;i<this.clients.length;i+=1) { |
| 22 | + if (!this.clients[i].busy) { |
| 23 | + if (this.clients.length > this.maxClients) { |
| 24 | + this.clients[i].end(); |
| 25 | + this.clients.splice(i, 1); |
| 26 | + i-=1; |
| 27 | + } else { |
| 28 | + return cb(this.clients[i]); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + if (this.clients.length >= this.maxClients) { |
| 33 | + this.pending.push(cb); |
| 34 | + } else { |
| 35 | + var client = http.createClient(this.port, this.host, this.https, this.credentials); |
| 36 | + this.clients.push(client); |
| 37 | + cb(client); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +Pool.prototype.request = function () { |
| 42 | + // Argument parsing. This gets a little dicey with the |
| 43 | + // differences in defaults |
| 44 | + var method, url, headers, callback, args; |
| 45 | + var self = this; |
| 46 | + args = Array.prototype.slice.call(arguments); |
| 47 | + |
| 48 | + if (typeof args[args.length - 1] === 'function') { |
| 49 | + callback = args.pop(); |
| 50 | + } |
| 51 | + if (args[0]) method = args[0]; |
| 52 | + if (args[1]) url = args[1]; |
| 53 | + if (args[2]) headers = args[2]; |
| 54 | + |
| 55 | + if (!headers) headers = {}; |
| 56 | + if (!headers.Connection) headers.Connection = 'keep-alive'; |
| 57 | + |
| 58 | + self.getClient(function (client) { |
| 59 | + var errorListener = function (error) { |
| 60 | + client.removeListener("error", errorListener); |
| 61 | + |
| 62 | + // Remove the client from the available clients since it has errored |
| 63 | + self.clients.splice(self.clients.indexOf(client), 1); |
| 64 | + |
| 65 | + self.emit("error", error); |
| 66 | + request.emit("error", error); |
| 67 | + }; |
| 68 | + |
| 69 | + var request = client.request(method, url, headers); |
| 70 | + client.on("error", errorListener); |
| 71 | + request.on("response", function (response) { |
| 72 | + response.on("end", function () { |
| 73 | + client.removeListener("error", errorListener); |
| 74 | + client.busy = false; |
| 75 | + self.onFree(client); |
| 76 | + }) |
| 77 | + }) |
| 78 | + client.busy = true; |
| 79 | + callback(request); |
| 80 | + }); |
| 81 | +}; |
| 82 | + |
| 83 | +Pool.prototype.onFree = function (client) { |
| 84 | + if (this.pending.length > 0) this.pending.shift()(client); |
| 85 | +}; |
| 86 | + |
| 87 | +Pool.prototype.setMinClients = function (num) { |
| 88 | + this.minClients = num; |
| 89 | + if (this.clients.length < num) { |
| 90 | + for (var i=this.clients.length;i<num;i+=1) { |
| 91 | + var client = http.createClient(this.port, this.host, this.https, this.credentials); |
| 92 | + this.clients.push(client); |
| 93 | + this.emit('free', client); |
| 94 | + } |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +Pool.prototype.setMaxClients = function (num) { |
| 99 | + this.maxClients = num; |
| 100 | +}; |
| 101 | +Pool.prototype.end = function () { |
| 102 | + this.clients.forEach(function (c) {c.end()}); |
| 103 | +}; |
| 104 | + |
| 105 | +function PoolManager () { |
| 106 | + this.pools = {}; |
| 107 | + this.pending = []; |
| 108 | + this.minClients = 0; |
| 109 | + this.maxClients = 8; |
| 110 | +}; |
| 111 | + |
| 112 | +PoolManager.prototype.setMaxClients = function (num) { |
| 113 | + this.maxClients = num; |
| 114 | + for (i in this.pools) { |
| 115 | + this.pools[i].setMaxClients(num); |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +PoolManager.prototype.setMinClients = function (num) { |
| 120 | + this.minClients = num; |
| 121 | + for (i in this.pools) { |
| 122 | + this.pools[i].setMinClients(num); |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | +PoolManager.prototype.getPool = function (port, host, https, credentials) { |
| 127 | + var k = (port+host+https+credentials); |
| 128 | + if (!this.pools[k]) { |
| 129 | + this.pools[k] = exports.createPool(port, host, https, credentials); |
| 130 | + this.pools[k].setMinClients(this.minClients); |
| 131 | + this.pools[k].setMaxClients(this.maxClients); |
| 132 | + } |
| 133 | + return this.pools[k]; |
| 134 | +}; |
| 135 | + |
| 136 | +exports.createPool = function (port, host, https, credentials) { |
| 137 | + return new Pool(port, host, https, credentials); |
| 138 | +}; |
| 139 | + |
| 140 | +exports.createPoolManager = function () { |
| 141 | + return new PoolManager(); |
| 142 | +}; |
0 commit comments