Implementing Resilient RabbitMQ Connections in a Layered Architecture

Hello, I am trying to provide a RabbitMQ connection to my program inside a Layer.scoped.

So far my code works great, but is not resilient to connection failures. When the connection is broken with Rabbit server, the connection object does not work anymore but is still provided to my program, which creates a bunch of errors with I want to publish messages or subscribe. I would like to have a way to always provide a working connection to my program.

One way is to detect when the connection fails and automatically refresh the connection, but how could I "refresh" a context / layer ?
Currently I am able to watch the connection errors inside a Stream, but I don't know what to trigger when I get an error. You can look at my code here: https://gist.github.com/wewelll/c30c3128cdcc43a02acd4eede73471c5. I don't know how to trigger a refresh of the layer.

I was also thinking to provide a SynchronizedRef of the connection, so I could just update the connection ref with a new instance of the connection, but it seems a bit over-engineered.

What would be the best way to do this ?
Was this page helpful?