RabbitMQ Network Connection Event Timeout


I have a rabbitmq server and amqp consumer (python) using combo.
I installed my application on a system with a firewall that closes idle connections after 1 hour.
This is my amqp_consumer.py:

try: # connections with Connection(self.broker_url, ssl=_ssl, heartbeat=self.heartbeat) as conn: chan = conn.channel() # more stuff here with conn.Consumer(queue, callbacks = [messageHandler], channel = chan): # Process messages and handle events on all channels while True: conn.drain_events() except Exception as e: # do stuff 

I want if the firewall closes the connection, I want to reconnect. Should I use the heartbeat argument or should I pass the timeout argument (3600 seconds) to the drain_events() function?
What are the differences between both options? (seems to do the same).
Thanks.

+7
python timeout rabbitmq heartbeat kombu
source share
1 answer

Money crashes alone will not cause any heartbeat unless there are messages to consume and acknowledge. If the queue is inactive, then the connection will be closed (by the rabbit server or your firewall).

What you need to do is use both the pulse and the timeout:

 while True: try: conn.drain_events(timeout=1) except socket.timeout: conn.heartbeat_check() 

Thus, even if the queue is in standby mode, the connection will not be closed.

In addition, you can wrap it all up with a retry policy in case of a connection closure or some other network error.

0
source share

All Articles