简体   繁体   English

RabbitMQ心跳与连接排除事件超时

[英]RabbitMQ heartbeat vs connection drain events timeout


I have a rabbitmq server and a amqp consumer (python) using kombu. 我有一个rabbitmq服务器和一个使用kombu的am​​qp使用者(python)。
I have installed my app in a system that has a firewall that closes idle connections after 1 hour. 我已将我的应用程序安装在具有防火墙的系统中,该防火墙在1小时后关闭空闲连接。
This is my amqp_consumer.py: 这是我的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? 我应该使用heartbeat参数还是应该将超时参数(3600秒)传递给drain_events()函数?
What are the differences between both options? 两种选择有什么区别? (seems to do the same). (似乎做同样的事)。
Thanks. 谢谢。

The drain_events on it's own would not produce any heartbeats, unless there are messages to consume and acknowledge. 除非有消息和确认消息,否则它自己的drain_events不会产生任何心跳。 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. 除此之外,您可能希望使用重试策略包装整个事物,以防连接断开或出现其他网络错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rabbitmq阻塞连接从RabbitMQ接收消息与使用阻塞连接从RabbitMQ获取消息 - Rabbitmq Blocking Connection to consume messages from RabbitMQ vs Using the Blocking Connection to get a message from RabbitMQ Pika 心跳终止连接 - Pika heartbeat terminates connection 皮卡blocking_connection.py随机超时连接到RabbitMQ - Pika blocking_connection.py random timeout connecting to RabbitMQ 适用于Rabbitmq的python amqp 1.3.3库心跳示例 - python amqp 1.3.3 library heartbeat example for rabbitmq VS Code Python超时等待调试器连接 - VS Code Python Timeout waiting for debugger connection py.test超时/ keepalive /心跳? - py.test timeout/keepalive/heartbeat? 如何使用 pika 1.2.0 发送 RabbitMQ 心跳帧? - How to send RabbitMQ heartbeat frame using pika 1.2.0? Docker Swarm 无法通过服务名称解析 DNS,Python Celery Workers 连接到 RabbitMQ Broker 导致连接超时 - Docker Swarm Failing to Resolve DNS by Service Name With Python Celery Workers Connecting to RabbitMQ Broker Resulting in Connection Timeout RabbitMQ在处理长时间运行的任务时关闭连接,超时设置产生错误 - RabbitMQ closes connection when processing long running tasks and timeout settings produce errors 消费者取消后,RabbitMQ pika异步消费者心跳问题 - RabbitMQ pika async consumer heartbeat issue after consumer cancellation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM