feat: execute post-baseline jetstream parity plan
This commit is contained in:
@@ -4,15 +4,26 @@ namespace NATS.Server.JetStream.Publish;
|
||||
|
||||
public sealed class PublishPreconditions
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, ulong> _dedupe = new(StringComparer.Ordinal);
|
||||
private readonly ConcurrentDictionary<string, DedupeEntry> _dedupe = new(StringComparer.Ordinal);
|
||||
|
||||
public bool IsDuplicate(string? msgId, out ulong existingSequence)
|
||||
public bool IsDuplicate(string? msgId, int duplicateWindowMs, out ulong existingSequence)
|
||||
{
|
||||
existingSequence = 0;
|
||||
if (string.IsNullOrEmpty(msgId))
|
||||
return false;
|
||||
|
||||
return _dedupe.TryGetValue(msgId, out existingSequence);
|
||||
if (!_dedupe.TryGetValue(msgId, out var entry))
|
||||
return false;
|
||||
|
||||
if (duplicateWindowMs > 0
|
||||
&& DateTime.UtcNow - entry.TimestampUtc > TimeSpan.FromMilliseconds(duplicateWindowMs))
|
||||
{
|
||||
_dedupe.TryRemove(msgId, out _);
|
||||
return false;
|
||||
}
|
||||
|
||||
existingSequence = entry.Sequence;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Record(string? msgId, ulong sequence)
|
||||
@@ -20,9 +31,24 @@ public sealed class PublishPreconditions
|
||||
if (string.IsNullOrEmpty(msgId))
|
||||
return;
|
||||
|
||||
_dedupe[msgId] = sequence;
|
||||
_dedupe[msgId] = new DedupeEntry(sequence, DateTime.UtcNow);
|
||||
}
|
||||
|
||||
public void TrimOlderThan(int duplicateWindowMs)
|
||||
{
|
||||
if (duplicateWindowMs <= 0)
|
||||
return;
|
||||
|
||||
var cutoff = DateTime.UtcNow.AddMilliseconds(-duplicateWindowMs);
|
||||
foreach (var (key, entry) in _dedupe)
|
||||
{
|
||||
if (entry.TimestampUtc < cutoff)
|
||||
_dedupe.TryRemove(key, out _);
|
||||
}
|
||||
}
|
||||
|
||||
public bool CheckExpectedLastSeq(ulong expectedLastSeq, ulong actualLastSeq)
|
||||
=> expectedLastSeq == 0 || expectedLastSeq == actualLastSeq;
|
||||
|
||||
private readonly record struct DedupeEntry(ulong Sequence, DateTime TimestampUtc);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user