[英]RabbitMQ heartbeat vs connection drain events timeout
I have a rabbitmq server and a amqp consumer (python) using kombu. 我有一个rabbitmq服务器和一个使用kombu的amqp使用者(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.