Created
July 4, 2012 19:18
-
-
Save sdolard/3049080 to your computer and use it in GitHub Desktop.
nodejs 100% cpu load on https.request (node v0.8.1 Mac)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var | |
util = require('util'), | |
https = require('https'), | |
EventEmitter = require('events').EventEmitter, | |
//------------------------------------------------------------------------------ | |
// Declaration | |
session, | |
Session = function (config) { | |
config = config || {}; | |
this.host = config.host || ''; | |
this.port = config.port || 443; | |
EventEmitter.call(this); | |
}; | |
util.inherits(Session, EventEmitter); | |
Session.prototype.connect = function(cb) { | |
this._authenticate(cb); | |
}; | |
Session.prototype._authenticate = function (cb) { | |
var | |
me = this; | |
if (cb) { | |
this.once('connect', cb); | |
} | |
this._request({ | |
host: this.host | |
}, function (err, response){ | |
me.emit('connect', err, response); | |
}); | |
}; | |
Session.prototype._request = function (config, callback) { | |
config = config || {}; | |
var | |
me = this, | |
r; | |
this.once('_request', callback); | |
r = https.request(config); | |
r.on('response', function(response) { | |
console.log(response); | |
me.emit('_request', undefined, response); | |
}); | |
// Socket error event | |
r.on('error', function(e) { | |
me.emit('_request', e); | |
}); | |
r.end(); | |
}; | |
//------------------------------------------------------------------------------ | |
// Instanciation | |
session = new Session({ | |
host: '10.0.0.254' | |
}); | |
session.connect(function(err, response) { | |
console.log('in \'connect\' callback '); | |
if (err) { | |
console.log(err); | |
} else { | |
console.log(response); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment