Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ heartbeat vs connection drain events timeout


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.

like image 927
Mr T. Avatar asked Nov 18 '15 17:11

Mr T.


People also ask

What is connection timeout in RabbitMQ?

The heartbeat timeout value defines after what period of time the peer TCP connection should be considered unreachable (down) by RabbitMQ and client libraries. This value is negotiated between the client and RabbitMQ server at the time of connection. The client must be configured to request heartbeats.

How do I increase my connection timeout in RabbitMQ?

The default connection timeout for the RabbitMQ connection factory is 600 seconds (at least in the Java client API), hence your 10 minutes. You can change this by specifying to the connection factory your timeout of choice.

What is heartbeat in Pika?

Pika picks the highest heartbeat value between client and server (and this is the norm in AMQP clients). This means that if you set a heartbeat value lower than 60 seconds, Pika will always pick RabbitMQ's value because it's higher.

What is TCP heartbeat?

A heartbeat is a type of a communication packet that is sent between nodes. Heartbeats are used to monitor the health of the nodes, networks and network interfaces, and to prevent cluster partitioning.


1 Answers

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.

like image 152
odedfos Avatar answered Nov 15 '22 00:11

odedfos