How to disable heartbeats with pika and rabbitmq

2020-07-19 05:20发布

问题:

I am using rabbitmq to facilitate some tasks from my rabbit server to my respective consumers. I have noticed that when I run some rather lengthy tests, 20+ minutes, my consumer will lose contact with the producer after it completes it's task. In my rabbit logs, I have seen the error

closing AMQP connection <0.14009.27> (192.168.101.2:64855 -> 
192.168.101.3:5672):
missed heartbeats from client, timeout: 60s

Also, I receive this error from pika

pika.exceptions.ConnectionClosed: (-1, "error(10054, 'An existing connection was forcibly closed by the remote host')")

I'm assuming this is due to this code right here and the conflict of heartbeats with the lengthy blocking connection time.

 self.connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.101.2', 5672, 'user', credentials))
    self.channel = self.connection.channel()
    self.channel.queue_declare(queue=self.tool,
                               arguments={'x-message-ttl': 1000,
                                             "x-dead-letter-exchange": "dlx",
                                             "x-dead-letter-routing-key": "dl",
                                             'durable': True})

Is there a proper way to increase the heartbeat time or how would I turn it off(would it be wise to) completely? Like I said, tests that are 20+ min seem to lead to a closedconnection error but I've ran plenty of tests from the 1-15 minute mark where everything is fine and the consumer client continues to wait for a message to be delivered.

回答1:

Please don't disable heartbeats. Instead, use Pika correctly. This means:

  • Use Pika version 0.12.0
  • Do your long-running task in a separate thread
  • When your task completes, use the add_callback_threadsafe method to schedule the basic_ack call.

Example code can be found here: link

I'm a RabbitMQ core team member and Pika maintainer so if you have further questions or issues, I recommend following up on either the pika-python or rabbitmq-users mailing list. Thanks!


NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.



回答2:

You can set the minimum heartbeat interval when creating the connection.

You can see an example in the pika documentation.

I'd recommend against disabling the heartbeat as it might lead to hanging connections piling up on the broker. We experienced such issue in production.

Always make sure the connections have a minimum reasonable heartbeat. If the heartbeat interval needs to be long (hours for example), make sure you close the connection when the application crashes or exits. In this way you won't leave the connection open on the broker side.