nats-io/nats.net.v2

Queue group always adds client to subject name queue group

MerzMax opened this issue · 4 comments

Observed behavior

When adding a queue group to a subscribtion, the queue group the client is added to always equals the subject name of the subscription.

Expected behavior

When applying a queue group to a subscription the client will be added to the configured queue group instead of the subject the subscribtion is for.

Server and client version

Nats-Server: 2.9.20-alpine (Docker Image)
NATS.Client: 2.1.1 (Nuget-Package)

Host environment

No response

Steps to reproduce

No response

mtmk commented

is this about services? could you give an example code snippet?

The bug is in this line:

written = _subjectEncoding.GetBytes(subject, span);

The subject is concatinated to the request instead of the queue group.

In this example both clients (both queue groups) should receive the event. Istead, only one client receives the message. (the example is based on the official one from the docs)

using NATS.Client.Core;

await using var nats = new NatsConnection();

var replyTasks = new List<Task>();
var cts = new CancellationTokenSource();


for (var i = 0; i < 3; i++)
{
    // Create three subscriptions all on the same queue group
    // Create a background message loop for every subscription
    var replyTaskId = i;
    replyTasks.Add(Task.Run(async () =>
    {
        var name = "maths-service" + replyTaskId;
        // Retrieve messages until unsubscribed
        await foreach (var msg in nats.SubscribeAsync<int>("math.double", queueGroup: name, cancellationToken: cts.Token))
        {
            Console.WriteLine($"[{replyTaskId}] Received request: {msg.Data}");
        }

        Console.WriteLine($"[{replyTaskId}] Done");
    }));
}

// Give subscriptions time to start
await Task.Delay(1000);

// Send a few requests
for (var i = 0; i < 10; i++)
{
    await nats.PublishAsync("math.double", i);
    Console.WriteLine("pub {0}", i);
}

Console.WriteLine("Stopping...");

// Cancellation token will unsubscribe and complete the message loops
//cts.Cancel();

// Make sure all tasks finished cleanly
await Task.WhenAll(replyTasks);

Console.WriteLine("All done");
mtmk commented

Wow this is a pretty big bug actually. thanks for the report 🥇