feat: add jetstream publish preconditions and dedupe
This commit is contained in:
@@ -3,6 +3,7 @@ namespace NATS.Server.JetStream.Publish;
|
||||
public sealed class JetStreamPublisher
|
||||
{
|
||||
private readonly StreamManager _streamManager;
|
||||
private readonly PublishPreconditions _preconditions = new();
|
||||
|
||||
public JetStreamPublisher(StreamManager streamManager)
|
||||
{
|
||||
@@ -10,7 +11,20 @@ public sealed class JetStreamPublisher
|
||||
}
|
||||
|
||||
public bool TryCapture(string subject, ReadOnlyMemory<byte> payload, out PubAck ack)
|
||||
=> TryCapture(subject, payload, null, out ack);
|
||||
|
||||
public bool TryCapture(string subject, ReadOnlyMemory<byte> payload, string? msgId, out PubAck ack)
|
||||
{
|
||||
if (_preconditions.IsDuplicate(msgId, out var existingSequence))
|
||||
{
|
||||
ack = new PubAck
|
||||
{
|
||||
Seq = existingSequence,
|
||||
ErrorCode = 10071,
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
var captured = _streamManager.Capture(subject, payload);
|
||||
if (captured == null)
|
||||
{
|
||||
@@ -19,7 +33,7 @@ public sealed class JetStreamPublisher
|
||||
}
|
||||
|
||||
ack = captured;
|
||||
|
||||
_preconditions.Record(msgId, ack.Seq);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user