pardahlman / RawRabbit

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

Add support for fault handling with Polly #174

Closed pardahlman closed 7 years ago

pardahlman commented 7 years ago

This will be done in a separate NuGet package, probably RawRabbit.Enrichers.Polly. The plugin is loaded as other plugins, and provides a fluent interface to register multiple policies for different situations

// define default policy
var defaultPolicy = Policy
    .Handle<Exception>()
    .FallbackAsync(ct =>
    {
        defaultCalled = true;
        return Task.FromResult(0);
    });

// define specific policy
var declareQueuePolicy = Policy
    .Handle<OperationInterruptedException>()
    .RetryAsync(async (e, retryCount, ctx) =>
    {
        // retry by declaring queue
        var defaultQueueCfg = ctx.GetPipeContext().GetClientConfiguration().Queue;
        var topology = ctx.GetTopologyProvider();
        var queue = new QueueDeclaration(defaultQueueCfg) { Name = ctx.GetQueueName() };
        await topology.DeclareQueueAsync(queue);
    });

// create client
var client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
{
    Plugins = p => p.UsePolly(c => c
        .UsePolicy(defaultPolicy) // fallback policy
        .UsePolicy(declareQueuePolicy, PolicyKeys.QueueBind)) // used for queue binds
});
ritasker commented 7 years ago

Looks great. Currently at NDC spoke to a guy last night who works a lot with polly. I will show him and see what he thinks too.

pardahlman commented 7 years ago

I think we have enough for now - will accept PR and suggestions to changes once it is in use :)