nats-io / nats.net

Full Async C# / .NET client for NATS
https://nats-io.github.io/nats.net
Apache License 2.0
264 stars 54 forks source link

Single consumer routing issues using FilterSubjects configuration #630

Open StKolev opened 2 months ago

StKolev commented 2 months ago

Observed behavior

When a single consumer is configured over multiple filter subjects using FilterSubjects configuration option, the messages are mixed up and wrong messages are delivered when received from nats-server. I have created a single console app that reproduce this problem. In my use case I have batch of orders. First I publish BatchCreatedEvent, then each order that is part of the batch and then BatchCompleted event.

When BatchCreatedEvent is about to be consumed, it receives message payload with OrderCreatedEvent. When OrderCreatedEvent is about to be consumed, it receives message payload with either BatchCreatedEvent or BatchCompletedEvent.

Logging produced:

[21:38:44 INF] Received message from orders.batch.dc7a540c-2f5f-46d0-9d4b-c308c33cd497.created subject for OrderCreated with payload {"Id": 0, "BatchId": "dc7a540c-2f5f-46d0-9d4b-c308c33cd497", "OrderDate": "0001-01-01T00:00:00.0000000+00:00", "Total": 0, "$type": "OrderCreated"} [21:38:44 INF] Received message from orders.batch.order.1.created subject for OrderBatchCompleted with payload {"BatchId": "dc7a540c-2f5f-46d0-9d4b-c308c33cd497", "$type": "OrderBatchCompleted"} [21:38:44 INF] Received message from orders.batch.order.2.created subject for OrderBatchCreated with payload {"BatchId": "dc7a540c-2f5f-46d0-9d4b-c308c33cd497", "OrdersCount": 0, "$type": "OrderBatchCreated"} [21:38:44 INF] Received message from orders.batch.order.3.created subject for OrderCreated with payload {"Id": 3, "BatchId": "dc7a540c-2f5f-46d0-9d4b-c308c33cd497", "OrderDate": "2024-09-14T18:38:44.7932320+00:00", "Total": 73, "$type": "OrderCreated"} [21:38:44 INF] Received message from orders.batch.order.4.created subject for OrderBatchCompleted with payload {"BatchId": "dc7a540c-2f5f-46d0-9d4b-c308c33cd497", "$type": "OrderBatchCompleted"} [21:38:44 INF] Received message from orders.batch.order.5.created subject for OrderBatchCreated with payload {"BatchId": "dc7a540c-2f5f-46d0-9d4b-c308c33cd497", "OrdersCount": 0, "$type": "OrderBatchCreated"}

Expected behavior

When I configure consumer to receive messages from multiple subjects, I expect messages to be routed to right "handlers" given the message subject.

Server and client version

Server version: 2.10.18 Client version: 2.3.3

Host environment

No response

Steps to reproduce

https://github.com/StKolev/Nats.ConsumerRoutingTest

mtmk commented 2 months ago

serialization on the consumers has no effect on filtering. in your example you are creating one consumer and three consume tasks using NextAsync<T>() methods. they would pick up the next message available to that one consumer regardless of the generic type being used in the Next call. so essentially serialization is a pre-agreed notion and has no effect on filtering or message routing.