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

Bug: [RabbitBroker] When defining two handlers with different routing keys only the first is registered #2029

Open
amir-dropit opened this issue Jan 9, 2025 · 2 comments
Labels
bug Something isn't working RabbitMQ Issues related to `faststream.rabbit` module and RabbitMQ broker features

Comments

@amir-dropit
Copy link

amir-dropit commented Jan 9, 2025

When defining two different handlers for messages on the same queue with different routing keys only the first is registered.

import asyncio

from faststream import FastStream
from faststream.rabbit import RabbitBroker, RabbitQueue

broker = RabbitBroker("amqp://guest:guest@localhost:5672/")

async def main():
    app = FastStream(broker)
    await app.run()


@broker.subscriber(RabbitQueue("queue1",routing_key="*.CheckStatusCommand"),"xchng")
async def check_status_handler(body):
    print(f"check_status_handler: {body}")


@broker.subscriber(RabbitQueue("queue1",routing_key="*.GetUserCommand"),"xchng")
async def get_user_handler(body):
    print(f"get_user_handler: {body}")

if __name__ == "__main__":
    asyncio.run(main())

Expected behavior
One queue with two bindings, one for each routing key will be created

Observed behavior
Only one binding is created

Screenshots
image

Additional context
faststream = {extras = ["rabbit"], version = "^0.5.33"}

@amir-dropit amir-dropit added the bug Something isn't working label Jan 9, 2025
@Lancetnik
Copy link
Member

The problem is in queue hash function - https://github.com/airtai/faststream/blob/main/faststream/rabbit/schemas/queue.py#L49

Anyway, it should works fine in 0.6.0, so I don't want to fix it in main if it is not strongly required

Also, you can take a look at #1308 to dig into the problem

@Lancetnik Lancetnik moved this to Quick wins in FastStream Jan 10, 2025
@Lancetnik Lancetnik added the RabbitMQ Issues related to `faststream.rabbit` module and RabbitMQ broker features label Jan 10, 2025
@amir-dropit
Copy link
Author

amir-dropit commented Jan 10, 2025

So I overridden the hash method and it did solve the binding issue on startup

class DistinctQueue(RabbitQueue):
    def __hash__(self):
        return hash(self.routing_key) + super.__hash__(self)

But the thing is when I receive any message to the queue with one of those routing keys in the example it bounces between the handlers on every receive.
For example I have two handlers
handlerA for routeA
handlerB for routeB
both on the same queue

When I send two messages with routeA, the first one is received by handlerA and the second by handlerB
Same if I send with routeB, the first one is received by handlerA and the second by handlerB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working RabbitMQ Issues related to `faststream.rabbit` module and RabbitMQ broker features
Projects
Status: Quick wins
Development

No branches or pull requests

2 participants