RabbitMQ heartbeat vs connection drain events timeout
Asked Answered
C

1

7


I have a rabbitmq server and a amqp consumer (python) using kombu.
I have installed my app in a system that has 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

what i want is that if the firewall closed the connection, then i want to reconnect. should i use the heartbeat argument or should i pass a timeout argument (of 3600 sec) to the drain_events() function?
What are the differences between both options? (seems to do the same).
Thanks.

Caloric answered 18/11, 2015 at 17:29 Comment(0)
D
1

The drain_events on it's own would not produce any heartbeats, unless there are messages to consume and acknowledge. If the queue is idle then eventually the connection would be closed (by rabbit server or by your firewall).

What you should do is use both the heartbeat and the timeout like so:

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

This way even if the queue is idle the connection won't be closed.

Besides that you might want to wrap the whole thing with a retry policy in case the connection does get closed or some other network error.

Delectable answered 14/1, 2018 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.