Skip to content

NODEJS-375 Allow async creation of Uuid #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 11, 2017
Merged

Conversation

guzmo
Copy link
Contributor

@guzmo guzmo commented Sep 5, 2017

I was supposed to create a small benchmark of async vs sync but I've had no time :( I ran some small tests with https://www.npmjs.com/package/loadtest, but only on my computer, which are running a lot of other processes at the same time, so hard to tell.
Hope the code is not too messy ;)

@datastax-bot
Copy link

Hi @guzmo, thanks for your contribution!

In order for us to evaluate and accept your PR, we ask that you sign a contribution license agreement. It's all electronic and will take just minutes.

Sincerely,
DataStax Bot.

@datastax-bot
Copy link

Thank you @guzmo for signing the Contribution License Agreement.

Cheers,
DataStax Bot.

@guzmo
Copy link
Contributor Author

guzmo commented Sep 5, 2017

The Synchronous function might be faster for creating a small amount of bytes ( which Uuid is doing I think ), but it might be safer to use the async because you might block the node process.
nodejs/help#457

Well, your choice if you want to have it.

@jorgebay
Copy link
Contributor

jorgebay commented Sep 6, 2017

As discussed in the ticket, I think is a good idea to have an asynchronous overload of Uuid.random().

About TimeUuid, do you want to add support for callbacks for TimeUuid.now() and TimeUuid.fromDate() in this patch? Otherwise, we can create a separate ticket for it.

@guzmo
Copy link
Contributor Author

guzmo commented Sep 6, 2017

@jorgebay Can we create separate tickets for TimeUuid? :) One step at a time is usually a good thing and would be nice to at least get this fix in an upcoming release.
Does it look good otherwise? Some tests timed out, maybe I should look at them?

Cheers

@jorgebay
Copy link
Contributor

jorgebay commented Sep 7, 2017

ok, I've created a separate ticket for TimeUuid: NODEJS-387.

I'll talk with the rest of the team to see if we can include it for the upcoming v3.3.0 today and let you know.
The patch is looking good, I'll do a full review soon.

@jorgebay
Copy link
Contributor

jorgebay commented Sep 7, 2017

We will try to include it for v3.3.0 release.

Copy link
Contributor

@jorgebay jorgebay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!
I've added few minor nits.

@@ -108,7 +129,10 @@ function getHex (uuid) {
* @private
* @returns {Buffer}
*/
function getRandomBytes() {
function getRandomBytes (cb) {
if (cb) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified without the if block, into return crypto.randomBytes(16, cb)

it('should generate v4 uuids that do not collide', function (done) {
var values = {};
var length = 100000;
var count = length;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use built-in utils.times() (same signature as caolan's async.times()), ie:

var utils = require('../../lib/utils');

utils.times(length, function eachTime(n, next) {
  Uuid.random(function (err, val) {
    if (err) {
      return next(err);
   }
    values[val.toString()] = true;
    next();
  });
}, function finish(err) {
  assert.ifError(err);
  assert.strictEqual(Object.keys(values).length, length);
  done();
});

@guzmo
Copy link
Contributor Author

guzmo commented Sep 8, 2017

Good catches! Will fix the review comments tonight.

@jorgebay jorgebay merged commit cb89374 into datastax:master Sep 11, 2017
@jorgebay
Copy link
Contributor

Landed in cb89374.
Thanks @guzmo !

@guzmo
Copy link
Contributor Author

guzmo commented Sep 12, 2017

No problem, glad to help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants