[api] pseduo-vendor pool until pull request is finalized

This commit is contained in:
indexzero 2010-09-17 01:30:52 -04:00
parent 3bb458e115
commit 7c2eb5de35
4 changed files with 200 additions and 8 deletions

View File

@ -1,7 +1,7 @@
/* /*
node-http-proxy.js: http proxy for node.js node-http-proxy.js: http proxy for node.js
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, & Marak Squires Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Marak Squires, Fedor Indutny
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the
@ -27,7 +27,7 @@
var sys = require('sys'), var sys = require('sys'),
http = require('http'), http = require('http'),
events = require('events'), events = require('events'),
pool = require('pool'), pool = require('./../vendor/pool/main'),
eyes = require('eyes'), eyes = require('eyes'),
min = 0, min = 0,
max = 100; max = 100;
@ -90,6 +90,7 @@ var HttpProxy = function (req, res, head) {
this.emitter = new(events.EventEmitter); this.emitter = new(events.EventEmitter);
this.events = {}; this.events = {};
this.req = req; this.req = req;
// If this request is upgrade request // If this request is upgrade request
// No response will be passed // No response will be passed
if (!req.headers.upgrade) { if (!req.headers.upgrade) {
@ -218,10 +219,6 @@ HttpProxy.prototype = {
}); });
}, },
/**
* WebSocket Tunnel realization
* Copyright (c) 2010 Fedor Indutny : http://github.com/donnerjack13589
*/
proxyWebSocketRequest: function (port, server, host) { proxyWebSocketRequest: function (port, server, host) {
var self = this, req = self.req, socket = self.sock, head = self.head, var self = this, req = self.req, socket = self.sock, head = self.head,
headers = new _headers(req.headers), CRLF = '\r\n'; headers = new _headers(req.headers), CRLF = '\r\n';
@ -409,9 +406,7 @@ HttpProxy.prototype = {
reverse_proxy.end(); reverse_proxy.end();
detach(); detach();
}); });
}; };
} }
}; };

41
vendor/pool/README.md vendored Normal file
View File

@ -0,0 +1,41 @@
# Pool -- Simple HTTP client pooling
## Install
<pre>
npm install pool
</pre>
## Super simple to use
Pool has two core usage scenarios: creating a pool and creating a set of pools. Creating a pool is easy:
<pre>
var pool = require('pool'),
sys = require('sys'),
local = pool.createPool('80', 'localhost');
client = local.request('GET', '/', function (request) {
// You can work with the request here just as you would as if it
// was returned from http.createClient
request.on('end', function () {
sys.puts('Request ended');
});
});
</pre>
Creating a set of pools can be accomplished using a PoolManager:
<pre>
var pool = require('pool'),
manager = pool.createPoolManager(),
local = manager.getPool('80', 'localhost');
client = local.request('GET', '/', function (request) {
// You can work with the request here just as you would as if it
// was returned from http.createClient
request.on('end', function () {
sys.puts('Request ended');
});
});
</pre>

142
vendor/pool/main.js vendored Normal file
View File

@ -0,0 +1,142 @@
var sys = require('sys')
, eyes = require('eyes')
, http = require('http')
, events = require('events')
;
function Pool (port, host, https, credentials) {
this.port = port;
this.host = host;
this.https = https;
this.credentials = credentials;
this.clients = [];
this.pending = [];
this.minClients = 0;
this.maxClients = 8;
};
sys.inherits(Pool, events.EventEmitter);
Pool.prototype.getClient = function (cb) {
for (var i=0;i<this.clients.length;i+=1) {
if (!this.clients[i].busy) {
if (this.clients.length > this.maxClients) {
this.clients[i].end();
this.clients.splice(i, 1);
i-=1;
} else {
return cb(this.clients[i]);
}
}
}
if (this.clients.length >= this.maxClients) {
this.pending.push(cb);
} else {
var client = http.createClient(this.port, this.host, this.https, this.credentials);
this.clients.push(client);
cb(client);
}
};
Pool.prototype.request = function () {
// Argument parsing. This gets a little dicey with the
// differences in defaults
var method, url, headers, callback, args;
var self = this;
args = Array.prototype.slice.call(arguments);
if (typeof args[args.length - 1] === 'function') {
callback = args.pop();
}
if (args[0]) method = args[0];
if (args[1]) url = args[1];
if (args[2]) headers = args[2];
if (!headers) headers = {};
if (!headers.Connection) headers.Connection = 'keep-alive';
self.getClient(function (client) {
var errorListener = function (error) {
client.removeListener("error", errorListener);
// Remove the client from the available clients since it has errored
self.clients.splice(self.clients.indexOf(client), 1);
self.emit("error", error);
request.emit("error", error);
};
var request = client.request(method, url, headers);
client.on("error", errorListener);
request.on("response", function (response) {
response.on("end", function () {
client.removeListener("error", errorListener);
client.busy = false;
self.onFree(client);
})
})
client.busy = true;
callback(request);
});
};
Pool.prototype.onFree = function (client) {
if (this.pending.length > 0) this.pending.shift()(client);
};
Pool.prototype.setMinClients = function (num) {
this.minClients = num;
if (this.clients.length < num) {
for (var i=this.clients.length;i<num;i+=1) {
var client = http.createClient(this.port, this.host, this.https, this.credentials);
this.clients.push(client);
this.emit('free', client);
}
}
};
Pool.prototype.setMaxClients = function (num) {
this.maxClients = num;
};
Pool.prototype.end = function () {
this.clients.forEach(function (c) {c.end()});
};
function PoolManager () {
this.pools = {};
this.pending = [];
this.minClients = 0;
this.maxClients = 8;
};
PoolManager.prototype.setMaxClients = function (num) {
this.maxClients = num;
for (i in this.pools) {
this.pools[i].setMaxClients(num);
}
};
PoolManager.prototype.setMinClients = function (num) {
this.minClients = num;
for (i in this.pools) {
this.pools[i].setMinClients(num);
}
};
PoolManager.prototype.getPool = function (port, host, https, credentials) {
var k = (port+host+https+credentials);
if (!this.pools[k]) {
this.pools[k] = exports.createPool(port, host, https, credentials);
this.pools[k].setMinClients(this.minClients);
this.pools[k].setMaxClients(this.maxClients);
}
return this.pools[k];
};
exports.createPool = function (port, host, https, credentials) {
return new Pool(port, host, https, credentials);
};
exports.createPoolManager = function () {
return new PoolManager();
};

14
vendor/pool/package.json vendored Normal file
View File

@ -0,0 +1,14 @@
{ "name" : "pool"
, "description" : "HTTP client pools."
, "tags" : ["http", "simple", "util", "utility"]
, "version" : "0.4.1"
, "author" : "Mikeal Rogers <mikeal.rogers@gmail.com>"
, "repository" :
{ "type" : "git"
, "url" : "http://github.com/mikeal/node-utils.git"
}
, "bugs" :
{ "web" : "http://github.com/mikeal/node-utils/issues" }
, "engines" : ["node >=0.1.90"]
, "main" : "./main"
}