Merge pull request #1008 from faeldt/master

Increase performance of generateId by avoiding sync calls to crypto.randomBytes
This commit is contained in:
Guillermo Rauch
2012-09-10 15:06:40 -07:00
2 changed files with 3 additions and 26 deletions

View File

@@ -12,6 +12,7 @@ var fs = require('fs')
, url = require('url')
, tty = require('tty')
, crypto = require('crypto')
, base64id = require('base64id')
, util = require('./util')
, store = require('./store')
, client = require('socket.io-client')
@@ -729,31 +730,6 @@ Manager.prototype.handleClient = function (data, req) {
}
};
/**
* Generates a session id.
*
* @api private
*/
Manager.prototype.generateId = function () {
var rand = new Buffer(15); // multiple of 3 for base64
if (!rand.writeInt32BE) {
return Math.abs(Math.random() * Math.random() * Date.now() | 0).toString()
+ Math.abs(Math.random() * Math.random() * Date.now() | 0).toString();
}
this.sequenceNumber = (this.sequenceNumber + 1) | 0;
rand.writeInt32BE(this.sequenceNumber, 11);
if (crypto.randomBytes) {
crypto.randomBytes(12).copy(rand);
} else {
// not secure for node 0.4
[0, 4, 8].forEach(function(i) {
rand.writeInt32BE(Math.random() * Math.pow(2, 32) | 0, i);
});
}
return rand.toString('base64').replace(/\//g, '_').replace(/\+/g, '-');
};
/**
* Handles a handshake request.
*
@@ -799,7 +775,7 @@ Manager.prototype.handleHandshake = function (data, req, res) {
if (err) return error(err);
if (authorized) {
var id = self.generateId()
var id = base64id.generateId()
, hs = [
id
, self.enabled('heartbeats') ? self.get('heartbeat timeout') || '' : ''