Skip to content

Commit

Permalink
#1844 More open customization of Polly use
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray committed Dec 7, 2023
1 parent d7754af commit b223f7c
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/Ocelot.Provider.Polly/OcelotBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

using Ocelot.Configuration;
using Ocelot.DependencyInjection;
using Ocelot.Errors;
using Ocelot.Logging;
using Ocelot.Provider.Polly.Interfaces;
using Ocelot.Requester;

using Polly.CircuitBreaker;
using Polly.Timeout;

namespace Ocelot.Provider.Polly;

public static class OcelotBuilderExtensions
{
private static readonly Dictionary<Type, Func<Exception, Error>> ErrorMapping = new Dictionary<Type, Func<Exception, Error>>
{
{typeof(TaskCanceledException), e => new RequestTimedOutError(e)},
{typeof(TimeoutRejectedException), e => new RequestTimedOutError(e)},
{typeof(BrokenCircuitException), e => new RequestTimedOutError(e)},
{typeof(BrokenCircuitException<HttpResponseMessage>), e => new RequestTimedOutError(e)}
};


public static IOcelotBuilder AddPolly<T>(this IOcelotBuilder builder,
QosDelegatingHandlerDelegate delegatingHandler,
Dictionary<Type, Func<Exception, Error>> errorMapping)
where T : class, IPollyQoSProvider<HttpResponseMessage>
QosDelegatingHandlerDelegate delegatingHandler,
Dictionary<Type, Func<Exception, Error>> errorMapping)
where T : class, IPollyQoSProvider<HttpResponseMessage>
{
builder.Services
.AddSingleton(errorMapping)
Expand All @@ -25,17 +36,22 @@ public static class OcelotBuilderExtensions

return builder;
}

public static IOcelotBuilder AddPolly<T>(this IOcelotBuilder builder, Dictionary<Type, Func<Exception, Error>> errorMapping)
where T : class, IPollyQoSProvider<HttpResponseMessage> =>
AddPolly<T>(builder, GetDelegatingHandler, errorMapping);

public static IOcelotBuilder AddPolly<T>(this IOcelotBuilder builder, QosDelegatingHandlerDelegate delegatingHandler)
where T : class, IPollyQoSProvider<HttpResponseMessage> =>
AddPolly<T>(builder, delegatingHandler, ErrorMapping);

public static IOcelotBuilder AddPolly<T>(this IOcelotBuilder builder)
where T : class, IPollyQoSProvider<HttpResponseMessage> =>
AddPolly<T>(builder, GetDelegatingHandler, ErrorMapping);

public static IOcelotBuilder AddPolly(this IOcelotBuilder builder)
{
var errorMapping = new Dictionary<Type, Func<Exception, Error>>
{
{ typeof(TaskCanceledException), e => new RequestTimedOutError(e) },
{ typeof(TimeoutRejectedException), e => new RequestTimedOutError(e) },
{ typeof(BrokenCircuitException), e => new RequestTimedOutError(e) },
{ typeof(BrokenCircuitException<HttpResponseMessage>), e => new RequestTimedOutError(e) },
};
return AddPolly<PollyQoSProvider>(builder, GetDelegatingHandler, errorMapping);
return AddPolly<PollyQoSProvider>(builder, GetDelegatingHandler, ErrorMapping);
}

private static DelegatingHandler GetDelegatingHandler(DownstreamRoute route, IHttpContextAccessor contextAccessor, IOcelotLoggerFactory loggerFactory)
Expand Down

0 comments on commit b223f7c

Please sign in to comment.