Skip to content
Browse files

moved the timestamp generation inside the lock, this should help with…

… keeping the timestamps unique
  • Loading branch information...
1 parent c0c3d4d commit 4bf5b6fa3f82877da4e6723f4191ff3eb41d5fe9 @nberardi nberardi committed Sep 25, 2012
Showing with 6 additions and 6 deletions.
  1. +6 −6 src/GuidGenerator.cs
View
12 src/GuidGenerator.cs
@@ -13,7 +13,7 @@ public static class GuidGenerator
private static readonly Random Random;
private static readonly object Lock = new object();
- private static DateTimeOffset _lastTimestampGenerated = TimestampHelper.UtcNow();
+ private static DateTimeOffset _lastTimestampForNoDuplicatesGeneration = TimestampHelper.UtcNow();
// number of bytes in uuid
private const int ByteArraySize = 16;
@@ -144,21 +144,21 @@ public static DateTime GetUtcDateTime(Guid guid)
public static Guid GenerateTimeBasedGuid()
{
- var ts = TimestampHelper.UtcNow();
-
switch(GuidGeneration)
{
case GuidGeneration.Fast:
- return GenerateTimeBasedGuid(ts, ClockSequenceBytes, NodeBytes);
+ return GenerateTimeBasedGuid(TimestampHelper.UtcNow(), ClockSequenceBytes, NodeBytes);
case GuidGeneration.NoDuplicates:
default:
lock (Lock)
{
- if (ts <= _lastTimestampGenerated)
+ var ts = TimestampHelper.UtcNow();
+
+ if (ts <= _lastTimestampForNoDuplicatesGeneration)
ClockSequenceBytes = GenerateClockSequenceBytes();
- _lastTimestampGenerated = ts;
+ _lastTimestampForNoDuplicatesGeneration = ts;
return GenerateTimeBasedGuid(ts, ClockSequenceBytes, NodeBytes);
}

0 comments on commit 4bf5b6f

Please sign in to comment.
Something went wrong with that request. Please try again.