Skip to content

Commit

Permalink
[debug] Removed pool as a dependency for stress test
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Sep 22, 2010
1 parent 266e524 commit 73381cf
Showing 1 changed file with 60 additions and 55 deletions.
115 changes: 60 additions & 55 deletions lib/node-http-proxy.js
Expand Up @@ -145,78 +145,83 @@ HttpProxy.prototype = {
self.body = '';

// Open new HTTP request to internal resource with will act as a reverse proxy pass
var p = manager.getPool(port, server);
sys.puts('Current pool count for ' + req.headers.host + ":" + port + ' ' + p.clients.length + ', Busy: ' + p.getBusy() + ', Free: ' + p.getFree());
//var p = manager.getPool(port, server);
//sys.puts('Current pool count for ' + req.headers.host + ":" + port + ' ' + p.clients.length + ', Busy: ' + p.getBusy() + ', Free: ' + p.getFree());

p.on('error', function (err) {
//p.on('error', function (err) {
// Remark: We should probably do something here
// but this is a hot-fix because I don't think 'pool'
// should be emitting this event.
});
//});

var client = http.createClient(port, server);
var reverse_proxy = client.request(req.method, req.url, req.headers);

p.request(req.method, req.url, req.headers, function (reverse_proxy) {
// Create an error handler so we can use it temporarily
var error = function (err) {
res.writeHead(500, {'Content-Type': 'text/plain'});
//p.request(req.method, req.url, req.headers, function (reverse_proxy) {

if(req.method !== 'HEAD') {
res.write('An error has occurred: ' + JSON.stringify(err));
}

// Response end may never come so removeListener here
reverse_proxy.removeListener('error', error);
res.end();
};
// Create an error handler so we can use it temporarily
var error = function (err) {
res.writeHead(500, {'Content-Type': 'text/plain'});

// Add a listener for the connection timeout event
reverse_proxy.addListener('error', error);
if(req.method !== 'HEAD') {
res.write('An error has occurred: ' + JSON.stringify(err));
}

// Response end may never come so removeListener here
reverse_proxy.removeListener('error', error);
res.end();
};

// Add a listener for the reverse_proxy response event
reverse_proxy.addListener('response', function (response) {
if (response.headers.connection) {
if (req.headers.connection) response.headers.connection = req.headers.connection;
else response.headers.connection = 'close';
}
// Add a listener for the connection timeout event
reverse_proxy.addListener('error', error);

// Set the response headers of the client response
res.writeHead(response.statusCode, response.headers);
// Add a listener for the reverse_proxy response event
reverse_proxy.addListener('response', function (response) {
if (response.headers.connection) {
if (req.headers.connection) response.headers.connection = req.headers.connection;
else response.headers.connection = 'close';
}

// Status code = 304
// No 'data' event and no 'end'
if (response.statusCode === 304) {
res.end();
return;
}
// Set the response headers of the client response
res.writeHead(response.statusCode, response.headers);

// Add event handler for the proxied response in chunks
response.addListener('data', function (chunk) {
if(req.method !== 'HEAD') {
res.write(chunk, 'binary');
self.body += chunk;
}
});
// Status code = 304
// No 'data' event and no 'end'
if (response.statusCode === 304) {
res.end();
return;
}

// Add event listener for end of proxied response
response.addListener('end', function () {
// Remark: Emit the end event for testability
self.emitter.emit('proxy', null, self.body);
reverse_proxy.removeListener('error', error);
res.end();
});
// Add event handler for the proxied response in chunks
response.addListener('data', function (chunk) {
if(req.method !== 'HEAD') {
res.write(chunk, 'binary');
self.body += chunk;
}
});

// Chunk the client request body as chunks from the proxied request come in
req.addListener('data', function (chunk) {
reverse_proxy.write(chunk, 'binary');
})

// At the end of the client request, we are going to stop the proxied request
req.addListener('end', function () {
reverse_proxy.end();
// Add event listener for end of proxied response
response.addListener('end', function () {
// Remark: Emit the end event for testability
self.emitter.emit('proxy', null, self.body);
reverse_proxy.removeListener('error', error);
res.end();
});
});

self.unwatch(req);
// Chunk the client request body as chunks from the proxied request come in
req.addListener('data', function (chunk) {
reverse_proxy.write(chunk, 'binary');
})

// At the end of the client request, we are going to stop the proxied request
req.addListener('end', function () {
reverse_proxy.end();
});

self.unwatch(req);

//});
},

proxyWebSocketRequest: function (port, server, host) {
Expand Down

0 comments on commit 73381cf

Please sign in to comment.