mosquito / aio-pika

AMQP 0.9 client designed for asyncio and humans.
https://aio-pika.readthedocs.org/
Apache License 2.0
1.24k stars 189 forks source link

How to handle disconnects ? #152

Open mlmarius opened 6 years ago

mlmarius commented 6 years ago

I am running the "Simple consumer" example from the quick start. My consumer connects to the MQ but when I force disconnect it from the MQ I see the following logging output:

Future exception was never retrieved future: <Future finished exception=ConnectionError('Auto Reconnect Error')> ConnectionError: Auto Reconnect Error

How can I handle this cleanly and how can I control the reconnection attempt logic? (number of retries and time waiting between retries)

mosquito commented 6 years ago

ConnectionError('Auto Reconnect Error') will be set to all undone futures. In the case when connect_robust using, the connection state will be repared.

mlmarius commented 6 years ago

Thanks! Is there a way for me to retrieve that error and set the reconnect interval ?

mosquito commented 6 years ago

The Connect.add_close_callback adds callback which will be called when connection lost. The RobustConnection() class accept the reconnect_interval= keyword argument, this might be passed like this: connect_robust(..., reconnect_interval=0.1)

komarovf commented 4 years ago

@mosquito Can you help please with a case, when a server drops the connection? In that case RobustConnection() tries to reconnect to the same server and don't call close_callback. In that case, I want to connect to another server, but I can't raise some Exceptions in close/reconnect callbacks because they didn't call.

It seems to be a good idea to add some parameter like reconnect=True to RobustConnection() for cases when I don't want to reconnect to the same rmq server.