Permalink
Browse files

Added listen() host dns resolution. Closes #35

  • Loading branch information...
1 parent 92d37fc commit a452dbdde26aecdf90992aeee7fc51df368fb29b @tj tj committed Feb 28, 2011
Showing with 58 additions and 9 deletions.
  1. +29 −9 lib/master.js
  2. +29 −0 test/test.dns.js
View
@@ -213,6 +213,7 @@ Master.prototype.call = function(id, method){
*/
Master.prototype.listen = function(port, host, callback){
+ var self = this;
if (this.preventDefault) return;
if (this.environmentMatches) {
if (this.isWorker) {
@@ -229,8 +230,11 @@ Master.prototype.listen = function(port, host, callback){
if (this.isChild) {
this.acceptFd();
} else {
- this.fd = this.createSocket();
- this.start();
+ this.createSocket(function(err, fd){
+ if (err) throw err;
+ self.fd = fd;
+ self.start();
+ });
}
}
}
@@ -314,19 +318,35 @@ Master.prototype.use = function(plugin){
};
/**
- * Create listening socket.
+ * Create listening socket and callback `fn(err, fd)`.
*
- * @return {Number} fd
- * @api public
+ * @return {Function} fn
+ * @api private
*/
-Master.prototype.createSocket = function(){
+Master.prototype.createSocket = function(fn){
+ var self = this
+ , ipv;
+
+ // explicit host
if (this.host) {
- return binding.socket('tcp' + net.isIP(this.host));
+ // ip
+ if (ipv = net.isIP(this.host)) {
+ fn(null, binding.socket('tcp' + ipv));
+ // lookup
+ } else {
+ require('dns').lookup(this.host, function(err, ip, ipv){
+ if (err) return fn(err);
+ self.host = ip;
+ fn(null, binding.socket('tcp' + ipv));
+ });
+ }
+ // local socket
} else if ('string' == typeof this.port) {
- return binding.socket('unix');
+ fn(null, binding.socket('unix'));
+ // only port
} else if (this.port) {
- return binding.socket('tcp4');
+ fn(null, binding.socket('tcp4'));
}
};
View
@@ -0,0 +1,29 @@
+
+/**
+ * Module dependencies.
+ */
+
+var cluster = require('../')
+ , http = require('http');
+
+require('./common');
+
+var server = http.createServer(function(req, res){
+ res.writeHead(200);
+ res.end('Hello World');
+});
+
+cluster = cluster(server)
+ .listen(3000, 'localhost');
+
+cluster.on('listening', function(){
+ process.cwd().should.include.string('cluster/test');
+ http.get({ host: 'localhost', port: 3000 }, function(res){
+ res.on('data', function(chunk){
+ chunk.toString().should.equal('Hello World');
+ });
+ res.on('end', function(){
+ cluster.close();
+ });
+ });
+});

0 comments on commit a452dbd

Please sign in to comment.