pardahlman / RawRabbit

A modern .NET framework for communication over RabbitMq
MIT License
747 stars 144 forks source link

Recover Broker connection if ChannelFactory instantiation fails #177

Closed pedrodbsa closed 7 years ago

pedrodbsa commented 7 years ago

I'm trying to make my application resilient to broker failures. If the broker goes down while the app is running it's possible to recover from network failures just fine.

However, if the broker unreachable at application start, the instantiation of ChannelFactory fails since the ConnectToBroker call from within the ctor throws an exception. Given that ChannelFactory is registered as a singleton, it's not possible to recover from it even if the broker is brought back up and the only way to recover from it is restarting the application.

Is there any way to circumvent this? Thanks in advance.

btw, I'm using 2.0.0-beta1

pardahlman commented 7 years ago

Hello @pedrodbsa - thanks for reporting this!

It is assumed that the broker is reachable at application startup, and it is by design that the exception is propagated all the way to the client - we use that in our deploy scripts as an indication that the deploy was unsuccessful.

However, ConnectToBroker is a protected virtual method. That means that you could override it and implement any retry strategy you like. I suggest you create a CustomChannelFactory that inherits from the default one and implement your own retry strategy (constructor removed for clarity).

public class CustomChannelFactory : ChannelFactory
{
    protected override void ConnectToBroker()
    {
        // Your code here
    }
}

The custom implementation can be registered when creating the client

var client = RawRabbitFactory.CreateTestClient(new RawRabbitOptions
{
    DependencyInjection = ioc => ioc.AddSingleton<IChannelFactory, CustomChannelFactory>()
})

As a side note, there is a beta package for [https://github.com/pardahlman/RawRabbit/tree/2.0/src/RawRabbit.Enrichers.Polly](policy strategies with Polly), where I'm planning to add a custom ChannelFactory where the connection is established within a execute delegate.

pardahlman commented 7 years ago

Closing this for now