Skip to content

Commit

Permalink
Apply wrapping in case of two policies or more
Browse files Browse the repository at this point in the history
  • Loading branch information
raman-m committed May 24, 2023
1 parent 91eed16 commit 4eb7f3e
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/Ocelot.Provider.Polly/PollyCircuitBreakingDelegatingHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

using Ocelot.Logging;

using Polly;
using Polly.CircuitBreaker;

using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Ocelot.Provider.Polly
{
public class PollyCircuitBreakingDelegatingHandler : DelegatingHandler
Expand All @@ -26,18 +27,17 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
{
try
{
IAsyncPolicy policy;

if (_qoSProvider.CircuitBreaker.Policies.Length == 1)
var policies = _qoSProvider.CircuitBreaker.Policies;
if (policies.Any())
{
policy = _qoSProvider.CircuitBreaker.Policies[0];
}
else
{
policy = Policy.WrapAsync(_qoSProvider.CircuitBreaker.Policies);
IAsyncPolicy policy = policies.Length >= 2
? Policy.WrapAsync(policies)
: policies[0];

return await policy.ExecuteAsync(() => base.SendAsync(request, cancellationToken));
}

return await policy.ExecuteAsync(() => base.SendAsync(request, cancellationToken));
return await base.SendAsync(request, cancellationToken);
}
catch (BrokenCircuitException ex)
{
Expand All @@ -46,7 +46,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
}
catch (HttpRequestException ex)
{
_logger.LogError("Error in CircuitBreakingDelegatingHandler.SendAync", ex);
_logger.LogError($"Error in {nameof(PollyCircuitBreakingDelegatingHandler)}.{nameof(SendAsync)}", ex);
throw;
}
}
Expand Down

0 comments on commit 4eb7f3e

Please sign in to comment.