+6
-3
@@ -51,7 +51,7 @@ public sealed class SeqId(DateTime epoch)
|
||||
/// Thrown if more than 255 IDs generated in 1ms
|
||||
/// </exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static long NextId(RngEntropy entropy = RngEntropy.Strong) => Default.Next(DateTime.UtcNow, entropy);
|
||||
public static long NextId(RngEntropy entropy = RngEntropy.Strong) => Default.Next(entropy);
|
||||
|
||||
#if NET9_0_OR_GREATER
|
||||
private readonly Lock _lock = new();
|
||||
@@ -62,6 +62,7 @@ public sealed class SeqId(DateTime epoch)
|
||||
private readonly DateTime _epoch = epoch;
|
||||
private int _seqId = 0;
|
||||
private long _lastTimestamp = -1L;
|
||||
private Func<DateTime> _defaultTimeFactory = static () => DateTime.UtcNow;
|
||||
|
||||
/// <summary>
|
||||
/// Generates next ID using current UTC time
|
||||
@@ -72,7 +73,7 @@ public sealed class SeqId(DateTime epoch)
|
||||
/// Thrown if more than 255 IDs generated in 1ms
|
||||
/// </exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public long Next(RngEntropy entropy = RngEntropy.Strong) => Next(DateTime.UtcNow, entropy);
|
||||
public long Next(RngEntropy entropy = RngEntropy.Strong) => Next(_defaultTimeFactory(), entropy);
|
||||
|
||||
/// <summary>
|
||||
/// Generates next ID with explicit timestamp
|
||||
@@ -104,7 +105,7 @@ public sealed class SeqId(DateTime epoch)
|
||||
throw new InvalidOperationException("Refused to create new SeqId. Last timestamp is in the future.");
|
||||
}
|
||||
|
||||
if (_seqId == SeqMask)
|
||||
if (_seqId > SeqMask)
|
||||
{
|
||||
throw new IndexOutOfRangeException("Refused to create new SeqId. Sequence exhausted.");
|
||||
}
|
||||
@@ -118,4 +119,6 @@ public sealed class SeqId(DateTime epoch)
|
||||
|
||||
return timestamp | currentSeq | currentRand;
|
||||
}
|
||||
|
||||
internal void UnsafeReplaceDefaultTimeFactory(Func<DateTime> timeFactory) => _defaultTimeFactory = timeFactory;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user