Receive notification of connection loss and recovery

66 views
Skip to first unread message

Adam Lett

unread,
Jan 25, 2021, 3:22:02 AM1/25/21
to Ruby RabbitMQ libraries
Hi

I am new to RabbitMQ and Bunny, so please excuse me if I've fundamentally misunderstood something, or if my assumptions are bad. My scenario is this: I have RabbitMQ server which is used for broadcasting events from a legacy line-of-business application, when certain entities get updated. And I have a Rails application which consumes these events in order to bust caches of these entities. I more or less have this working. But I foresee an issue if the consumer loses its connection, for example if the RabbitMQ server needs to be restarted: No events will be received for any updates that happen while the connection is down. This does not necessarily need to pose a big problem if the Rails app can somehow be made aware that it is currently not receiving events. As long as it is aware that it currently potentially missing events, it can bypass caches until the connection is re-established. When the connection is regained, it can empty the caches and start repopulating them. If it is unaware however, I risk stale caches. My concrete problem is how to become aware of the lost connection. Looking through the source code of Bunny, it seems like my only option is to disable automatic connection recovery and handle it myself. Which is ultimately not the world's biggest problem, but I would still prefer to Bunny's auto recovery mechanism, as I have more confidence it its ability to not leak resources and re-establish the topology correctly as it recovers. What I had hoped existed was something like the Java RabbitMQ library's RecoveryListener which does exactly what I want: It receives notification when the connection is lost, and again when the connection is regained.

I would be very curious to know how other people solve this issue, as I can't imagine I'm the only one who has it.

M K

unread,
Jan 25, 2021, 3:25:03 AM1/25/21
to Ruby RabbitMQ libraries
Apologies that this has gone under my radar and had to spend a while in moderation.

Take a look at Bunny::Session#after_recovery_completed which accepts a callable.

Adam Lett

unread,
Jan 25, 2021, 4:19:27 PM1/25/21
to ruby...@googlegroups.com
On 25 Jan 2021, 09.25 +0100, M K <michael....@gmail.com>, wrote:
Apologies that this has gone under my radar and had to spend a while in moderation.

Take a look at Bunny::Session#after_recovery_completed which accepts a callable.


Yes, I discovered this after posting, and it does indeed solve half of my problem :)

It seems to me, though, that getting a notification when the connection is lost is *at least* as valuable as getting one, after it has been recovered. I would love to see a #recovery_started to complement the already existing #after_recovery_completed. What would you think about this?

Michael Klishin

unread,
Jan 25, 2021, 4:39:44 PM1/25/21
to Ruby RabbitMQ libraries
It can be trivially added to Bunny::Session#recover_from_network_failure. You are welcome to contribute it :)

I'm not sure why would anyone who uses automatic recovery to care, though.
> --
> Bunny: http://rubybunny.info
> March Hare: http://rubymarchhare.info
>
> IRC: #rabbitmq on irc.freenode.net
>
> Post to the group: ruby...@googlegroups.com | unsubscribe: ruby-amqp+...@googlegroups.com
> ---
> You received this message because you are subscribed to the Google Groups "Ruby RabbitMQ libraries" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ruby-amqp+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-amqp/ea74e93f-f574-417c-865d-f0515ec4d3bd%40Spark.

Adam Lett

unread,
Jan 26, 2021, 8:13:20 AM1/26/21
to Ruby RabbitMQ libraries
On Monday, January 25, 2021 at 10:39:44 PM UTC+1 Michael Klishin wrote:
It can be trivially added to Bunny::Session#recover_from_network_failure. You are welcome to contribute it :)

Great! I will look into it when I can find the time.
 

I'm not sure why would anyone who uses automatic recovery to care, though.

If there's a way to solve the problem I described in my original post that doesn't require me to get a pull request accepted and wait for the next release, I would love to hear about it! As I wrote, I'm new to Bunny and RabbitMQ, and it's quite possible I'm misunderstanding something, or am making bad assumptions, or just thinking about it wrong. But as I also wrote, the Java client does have this feature, so it would appear I'm not *entirely* alone in finding it useful ;)

Anyway, thank you both for taking the time to respond.

Michael Klishin

unread,
Feb 5, 2021, 2:01:27 PM2/5/21
to ruby...@googlegroups.com
Your solution seems reasonable. However, the idea that you should treat the connection as "broken" if there were no messages
consumed in a while can backfire when you upgrade consumers or there is no traffic for any other reason.

Connection recovery and management in Web apps is painful compared to standalone consumers, so if this option
works for you (you can try the version in master, there is effectively no delta with GA releases up on rubygems.org),
I cannot argue with that :)
> --
> Bunny: http://rubybunny.info
> March Hare: http://rubymarchhare.info
>
> IRC: #rabbitmq on irc.freenode.net
>
> Post to the group: ruby...@googlegroups.com | unsubscribe: ruby-amqp+...@googlegroups.com
> ---
> You received this message because you are subscribed to the Google Groups "Ruby RabbitMQ libraries" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ruby-amqp+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-amqp/2f6d2932-7504-48e4-aa6d-76a81f77a830n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages