Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/NATS.Client.JetStream/Models/ConsumerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public ConsumerConfig(string name)
#endif

/// <summary>
/// Specifies the priority policy for consumer message selection, such as prioritizing <c>overflow</c> or <c>pinned_client</c>.
/// Specifies the priority policy for consumer message selection, such as prioritizing <c>prioritized</c>, <c>overflow</c>, or <c>pinned_client</c>.
/// </summary>
[System.Text.Json.Serialization.JsonPropertyName("priority_policy")]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
Expand Down
13 changes: 13 additions & 0 deletions src/NATS.Client.JetStream/Models/ConsumerGetnextRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,17 @@ public record ConsumerGetnextRequest
[System.Text.Json.Serialization.JsonPropertyName("id")]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
public string? Id { get; set; }

/// <summary>
/// Priority for message delivery when using prioritized priority policy.
/// </summary>
/// <remarks>
/// Lower values indicate higher priority (0 is the highest priority).
/// Maximum priority value is 9. This field is only used when the consumer
/// has PriorityPolicy set to "prioritized".
/// </remarks>
[System.Text.Json.Serialization.JsonPropertyName("priority")]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
[System.ComponentModel.DataAnnotations.Range(0, 9)]
public byte Priority { get; set; }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when these are serialized, are the names lowercased?
probably what this is for

 new JsonStringEnumConverter<ConsumerConfigPriorityPolicy (JsonNamingPolicy.SnakeCaseLower),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume None does not get written either.

}
3 changes: 3 additions & 0 deletions src/NATS.Client.JetStream/NatsJSConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ await sub.CallMsgNextAsync(
Group = opts.PriorityGroup?.Group,
MinPending = opts.PriorityGroup?.MinPending ?? 0,
MinAckPending = opts.PriorityGroup?.MinAckPending ?? 0,
Priority = opts.PriorityGroup?.Priority ?? 0,
},
cancellationToken).ConfigureAwait(false);

Expand Down Expand Up @@ -438,6 +439,7 @@ await sub.CallMsgNextAsync(
Group = opts.PriorityGroup?.Group,
MinPending = opts.PriorityGroup?.MinPending ?? 0,
MinAckPending = opts.PriorityGroup?.MinAckPending ?? 0,
Priority = opts.PriorityGroup?.Priority ?? 0,
}
: new ConsumerGetnextRequest
{
Expand All @@ -449,6 +451,7 @@ await sub.CallMsgNextAsync(
Group = opts.PriorityGroup?.Group,
MinPending = opts.PriorityGroup?.MinPending ?? 0,
MinAckPending = opts.PriorityGroup?.MinAckPending ?? 0,
Priority = opts.PriorityGroup?.Priority ?? 0,
},
cancellationToken).ConfigureAwait(false);

Expand Down
4 changes: 2 additions & 2 deletions src/NATS.Client.JetStream/NatsJSContext.Consumers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ private async ValueTask<NatsJSConsumer> CreateOrUpdateConsumerInternalAsync(
}

// TODO: enum these values?
if (config.PriorityPolicy != null && config.PriorityPolicy != "none" && config.PriorityPolicy != "overflow" && config.PriorityPolicy != "pinned_client")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need this check any more. it's an enum now.

if (config.PriorityPolicy != null && config.PriorityPolicy != "none" && config.PriorityPolicy != "overflow" && config.PriorityPolicy != "pinned_client" && config.PriorityPolicy != "prioritized")
{
throw new NatsJSException("Cannot create consumers with priority policy other than 'overflow', 'pinned_client', or 'none'.");
throw new NatsJSException("Cannot create consumers with priority policy other than 'overflow', 'pinned_client', 'prioritized', or 'none'.");
}

var response = await JSRequestResponseAsync<ConsumerCreateRequest, ConsumerInfo>(
Expand Down
10 changes: 10 additions & 0 deletions src/NATS.Client.JetStream/NatsJSOpts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,14 @@ public record NatsJSPriorityGroupOpts
/// When specified, this Pull request will only receive messages when the consumer has at least this many ack pending messages.
/// </summary>
public long MinAckPending { get; set; }

/// <summary>
/// Priority for message delivery when using prioritized priority policy.
/// </summary>
/// <remarks>
/// Lower values indicate higher priority (0 is the highest priority).
/// Maximum priority value is 9. This field is only used when the consumer
/// has PriorityPolicy set to "prioritized".
/// </remarks>
public byte Priority { get; init; }
}
83 changes: 83 additions & 0 deletions tests/NATS.Client.JetStream.Tests/PriorityGroupTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,87 @@ public async Task Consume_from_overflow_group()
}
}
}

[SkipIfNatsServer(versionEarlierThan: "2.12")]
public async Task Fetch_from_prioritized_group_with_priority()
{
await using var nats = new NatsConnection(new NatsOpts { Url = _server.Url });
var js = new NatsJSContext(nats);
var prefix = _server.GetNextId();

var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));

await js.CreateStreamAsync($"{prefix}s1", [$"{prefix}s1.>"], cts.Token);

for (var i = 0; i < 10; i++)
{
var ack = await js.PublishAsync($"{prefix}s1.{i}", i, cancellationToken: cts.Token);
ack.EnsureSuccess();
}

var consumerConfig = new ConsumerConfig($"{prefix}c1")
{
PriorityGroups = ["jobs"],
PriorityPolicy = "prioritized",
};
var consumer = await js.CreateOrUpdateConsumerAsync($"{prefix}s1", consumerConfig, cancellationToken: cts.Token);

// Test with priority 5
{
var opts = new NatsJSFetchOpts
{
MaxMsgs = 3,
PriorityGroup = new NatsJSPriorityGroupOpts { Group = "jobs", Priority = 5 },
};
var count = 0;
await foreach (var msg in consumer.FetchAsync<int>(opts, cancellationToken: cts.Token))
{
Assert.Equal(count++, msg.Data);
if (count == 3) break;
}

Assert.Equal(3, count);
}

// Test with priority 0 (highest priority)
{
var opts = new NatsJSFetchOpts
{
MaxMsgs = 2,
PriorityGroup = new NatsJSPriorityGroupOpts { Group = "jobs", Priority = 0 },
};
var count = 0;
await foreach (var msg in consumer.FetchAsync<int>(opts, cancellationToken: cts.Token))
{
Assert.Equal(count + 3, msg.Data); // Should continue from where we left off
count++;
if (count == 2) break;
}

Assert.Equal(2, count);
}
}

[SkipIfNatsServer(versionEarlierThan: "2.12")]
public async Task Consumer_with_prioritized_policy_validation()
{
await using var nats = new NatsConnection(new NatsOpts { Url = _server.Url });
var js = new NatsJSContext(nats);
var prefix = _server.GetNextId();

var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));

await js.CreateStreamAsync($"{prefix}s1", [$"{prefix}s1.>"], cts.Token);

// Test that prioritized policy is accepted
var consumerConfig = new ConsumerConfig($"{prefix}c1")
{
PriorityGroups = ["jobs"],
PriorityPolicy = "prioritized",
};

var consumer = await js.CreateOrUpdateConsumerAsync($"{prefix}s1", consumerConfig, cancellationToken: cts.Token);
Assert.NotNull(consumer);
Assert.Equal("prioritized", consumer.Info.Config.PriorityPolicy);
}
}
Loading