feat: add jetstream publish preconditions and dedupe
This commit is contained in:
25
src/NATS.Server/JetStream/Publish/PublishPreconditions.cs
Normal file
25
src/NATS.Server/JetStream/Publish/PublishPreconditions.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace NATS.Server.JetStream.Publish;
|
||||
|
||||
public sealed class PublishPreconditions
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, ulong> _dedupe = new(StringComparer.Ordinal);
|
||||
|
||||
public bool IsDuplicate(string? msgId, out ulong existingSequence)
|
||||
{
|
||||
existingSequence = 0;
|
||||
if (string.IsNullOrEmpty(msgId))
|
||||
return false;
|
||||
|
||||
return _dedupe.TryGetValue(msgId, out existingSequence);
|
||||
}
|
||||
|
||||
public void Record(string? msgId, ulong sequence)
|
||||
{
|
||||
if (string.IsNullOrEmpty(msgId))
|
||||
return;
|
||||
|
||||
_dedupe[msgId] = sequence;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user