Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JetStream metadata to msg parsed from reply string #139

Merged
merged 5 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ generated_code = true

[*.cs]
indent_size = 4
max_line_length = 300

# changes from VS2019 defaults
csharp_style_namespace_declarations = file_scoped
Expand Down
28 changes: 28 additions & 0 deletions src/NATS.Client.JetStream/Internal/ReplyToDateTimeAndSeq.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace NATS.Client.JetStream.Internal;

internal static class ReplyToDateTimeAndSeq
{
internal static (DateTimeOffset? DateTime, long? Seq) Parse(string? reply)
simonhoss marked this conversation as resolved.
Show resolved Hide resolved
{
if (reply == null)
{
return (null, null);
}

var ackSeperated = reply.Split(".");

// It must be seperated by 9 dots as defined in
// https://docs.nats.io/reference/reference-protocols/nats_api_reference#acknowledging-messages
if (ackSeperated.Length != 9)
simonhoss marked this conversation as resolved.
Show resolved Hide resolved
{
return (null, null);
}

var timestamp = long.Parse(ackSeperated[^2]);
var offset = DateTimeOffset.FromUnixTimeMilliseconds(timestamp / 1000000);
var dateTime = new DateTimeOffset(offset.Ticks, TimeSpan.Zero);
var seq = long.Parse(ackSeperated[5]);

return (dateTime, seq);
}
}
17 changes: 13 additions & 4 deletions src/NATS.Client.JetStream/NatsJSMsg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
{
private readonly NatsJSContext _context;
private readonly NatsMsg<T> _msg;
private readonly Lazy<(DateTimeOffset? DateTime, long? Sequence)> _replyToDateTimeAndSeq;

internal NatsJSMsg(NatsMsg<T> msg, NatsJSContext context)
{
_msg = msg;
_context = context;
_replyToDateTimeAndSeq = new Lazy<(DateTimeOffset? DateTime, long? Sequnce)>(() => ReplyToDateTimeAndSeq.Parse(msg.ReplyTo));
}

/// <summary>
Expand Down Expand Up @@ -50,6 +52,16 @@
/// </summary>
public INatsConnection? Connection => _msg.Connection;

/// <summary>
/// The sequence number of the message.
/// </summary>
public long? Sequence => _replyToDateTimeAndSeq.Value.Sequence;

/// <summary>
/// The time of the message.
/// </summary>
public DateTimeOffset? DateTime => _replyToDateTimeAndSeq.Value.DateTime;

/// <summary>
/// Acknowledges the message was completely handled.
/// </summary>
Expand Down Expand Up @@ -102,10 +114,7 @@

return _msg.ReplyAsync(
payload: payload,
opts: new NatsPubOpts
{
WaitUntilSent = opts.WaitUntilSent ?? _context.Opts.AckOpts.WaitUntilSent,
},
opts: new NatsPubOpts {WaitUntilSent = opts.WaitUntilSent ?? _context.Opts.AckOpts.WaitUntilSent,},

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (release/v2.9.23)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (release/v2.9.23)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (release/v2.9.23)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (release/v2.9.23)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (release/v2.9.23)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (release/v2.9.23)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (latest)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (latest)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (latest)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (latest)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (latest)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (latest)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (release/v2.9.23)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (release/v2.9.23)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (release/v2.9.23)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (release/v2.9.23)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (release/v2.9.23)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (release/v2.9.23)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (main)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (main)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (main)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (main)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (main)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / memory test (main)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (latest)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (latest)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (latest)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (latest)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (latest)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (latest)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (main)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (main)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (main)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (main)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (main)

Check warning on line 117 in src/NATS.Client.JetStream/NatsJSMsg.cs

View workflow job for this annotation

GitHub Actions / dotnet (main)

cancellationToken: cancellationToken);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using NATS.Client.JetStream.Internal;

namespace NATS.Client.JetStream.Tests.Internal;

public class ReplyToDateTimeAndSeqTest
{
[Fact]
public void ShouldParseReplyToDateTimeAndSeq()
{
var (dateTime, seq) = ReplyToDateTimeAndSeq.Parse("$JS.ACK.UnitTest.GetEvents_0.1.100.1.1696023331771188000.0");

dateTime!.Value.ToString("O").Should().Be("2023-09-29T21:35:31.7710000+00:00");
seq.Should().Be(100);
}

[Fact]
public void ShouldSetNullForReturnWhenReplyToIsNull()
{
var (dateTime, seq) = ReplyToDateTimeAndSeq.Parse(null);

dateTime.Should().BeNull();
seq.Should().BeNull();
}

[Fact]
public void ShouldSetNullWhenReplyToIsASimpleReqResponse()
{
var (dateTime, seq) = ReplyToDateTimeAndSeq.Parse("_INBOX.1");

dateTime.Should().BeNull();
seq.Should().BeNull();
}
}
Loading