Skip to content

Commit 7c2eb5d

Browse files
committed
[api] pseduo-vendor pool until pull request is finalized
1 parent 3bb458e commit 7c2eb5d

File tree

4 files changed

+200
-8
lines changed

4 files changed

+200
-8
lines changed

lib/node-http-proxy.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
node-http-proxy.js: http proxy for node.js
33
4-
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, & Marak Squires
4+
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Marak Squires, Fedor Indutny
55
66
Permission is hereby granted, free of charge, to any person obtaining
77
a copy of this software and associated documentation files (the
@@ -27,7 +27,7 @@
2727
var sys = require('sys'),
2828
http = require('http'),
2929
events = require('events'),
30-
pool = require('pool'),
30+
pool = require('./../vendor/pool/main'),
3131
eyes = require('eyes'),
3232
min = 0,
3333
max = 100;
@@ -90,6 +90,7 @@ var HttpProxy = function (req, res, head) {
9090
this.emitter = new(events.EventEmitter);
9191
this.events = {};
9292
this.req = req;
93+
9394
// If this request is upgrade request
9495
// No response will be passed
9596
if (!req.headers.upgrade) {
@@ -218,10 +219,6 @@ HttpProxy.prototype = {
218219
});
219220
},
220221

221-
/**
222-
* WebSocket Tunnel realization
223-
* Copyright (c) 2010 Fedor Indutny : http://github.com/donnerjack13589
224-
*/
225222
proxyWebSocketRequest: function (port, server, host) {
226223
var self = this, req = self.req, socket = self.sock, head = self.head,
227224
headers = new _headers(req.headers), CRLF = '\r\n';
@@ -409,9 +406,7 @@ HttpProxy.prototype = {
409406
reverse_proxy.end();
410407
detach();
411408
});
412-
413409
};
414-
415410
}
416411
};
417412

vendor/pool/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Pool -- Simple HTTP client pooling
2+
3+
## Install
4+
5+
<pre>
6+
npm install pool
7+
</pre>
8+
9+
## Super simple to use
10+
11+
Pool has two core usage scenarios: creating a pool and creating a set of pools. Creating a pool is easy:
12+
13+
<pre>
14+
var pool = require('pool'),
15+
sys = require('sys'),
16+
local = pool.createPool('80', 'localhost');
17+
18+
client = local.request('GET', '/', function (request) {
19+
// You can work with the request here just as you would as if it
20+
// was returned from http.createClient
21+
request.on('end', function () {
22+
sys.puts('Request ended');
23+
});
24+
});
25+
</pre>
26+
27+
Creating a set of pools can be accomplished using a PoolManager:
28+
29+
<pre>
30+
var pool = require('pool'),
31+
manager = pool.createPoolManager(),
32+
local = manager.getPool('80', 'localhost');
33+
34+
client = local.request('GET', '/', function (request) {
35+
// You can work with the request here just as you would as if it
36+
// was returned from http.createClient
37+
request.on('end', function () {
38+
sys.puts('Request ended');
39+
});
40+
});
41+
</pre>

vendor/pool/main.js

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
};

vendor/pool/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{ "name" : "pool"
2+
, "description" : "HTTP client pools."
3+
, "tags" : ["http", "simple", "util", "utility"]
4+
, "version" : "0.4.1"
5+
, "author" : "Mikeal Rogers <[email protected]>"
6+
, "repository" :
7+
{ "type" : "git"
8+
, "url" : "http://github.com/mikeal/node-utils.git"
9+
}
10+
, "bugs" :
11+
{ "web" : "http://github.com/mikeal/node-utils/issues" }
12+
, "engines" : ["node >=0.1.90"]
13+
, "main" : "./main"
14+
}

0 commit comments

Comments
 (0)