Skip to content

Commit

Permalink
ThreeMammals#1712 Migrate to Polly 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray authored and raman-m committed Oct 13, 2023
1 parent b4265a4 commit a815a58
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 32 deletions.
9 changes: 2 additions & 7 deletions src/Ocelot.Provider.Polly/CircuitBreaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ namespace Ocelot.Provider.Polly
{
public class CircuitBreaker
{
private readonly List<IAsyncPolicy> _policies = new();

public CircuitBreaker(params IAsyncPolicy[] policies)
{
foreach (var policy in policies.Where(p => p != null))
{
_policies.Add(policy);
}
Policies = policies.Where(p => p != null).ToArray();
}

public IAsyncPolicy[] Policies => _policies.ToArray();
public IAsyncPolicy[] Policies { get; }
}
}
2 changes: 1 addition & 1 deletion src/Ocelot.Provider.Polly/Ocelot.Provider.Polly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="Polly" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.0.0" />
Expand Down
13 changes: 10 additions & 3 deletions src/Ocelot.Provider.Polly/OcelotBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

using Polly.CircuitBreaker;
using Polly.Timeout;

using Microsoft.Extensions.DependencyInjection;

using Ocelot.Configuration;
using Ocelot.DependencyInjection;
using Ocelot.Errors;
using Ocelot.Logging;
using Ocelot.Requester;
using Polly.CircuitBreaker;
using Polly.Timeout;

namespace Ocelot.Provider.Polly
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

using Ocelot.Logging;
using Ocelot.Provider.Polly.Interfaces;

using Polly;
using Polly.CircuitBreaker;

Expand Down Expand Up @@ -28,7 +34,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
return await base.SendAsync(request, cancellationToken);
}

IAsyncPolicy policy = policies.Length > 1
var policy = policies.Length > 1
? Policy.WrapAsync(policies)
: policies[0];

Expand Down
37 changes: 18 additions & 19 deletions src/Ocelot.Provider.Polly/PollyQoSProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Net.Http;

using Ocelot.Configuration;
using Ocelot.Logging;
using Ocelot.Provider.Polly.Interfaces;

using Polly;
using Polly.CircuitBreaker;
using Polly.Timeout;
Expand All @@ -9,28 +13,18 @@ namespace Ocelot.Provider.Polly
{
public class PollyQoSProvider : IPollyQoSProvider
{
private readonly AsyncCircuitBreakerPolicy _circuitBreakerPolicy;
private readonly AsyncTimeoutPolicy _timeoutPolicy;
private readonly IOcelotLogger _logger;

public PollyQoSProvider(AsyncCircuitBreakerPolicy circuitBreakerPolicy, AsyncTimeoutPolicy timeoutPolicy, IOcelotLogger logger)
{
_circuitBreakerPolicy = circuitBreakerPolicy;
_timeoutPolicy = timeoutPolicy;
_logger = logger;
}

public PollyQoSProvider(DownstreamRoute route, IOcelotLoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<PollyQoSProvider>();
AsyncCircuitBreakerPolicy circuitBreakerPolicy;
var logger = loggerFactory.CreateLogger<PollyQoSProvider>();

_ = Enum.TryParse(route.QosOptions.TimeoutStrategy, out TimeoutStrategy strategy);

_timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromMilliseconds(route.QosOptions.TimeoutValue), strategy);
var timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromMilliseconds(route.QosOptions.TimeoutValue), strategy);

if (route.QosOptions.ExceptionsAllowedBeforeBreaking > 0)
{
_circuitBreakerPolicy = Policy
circuitBreakerPolicy = Policy
.Handle<HttpRequestException>()
.Or<TimeoutRejectedException>()
.Or<TimeoutException>()
Expand All @@ -39,25 +33,30 @@ public PollyQoSProvider(DownstreamRoute route, IOcelotLoggerFactory loggerFactor
durationOfBreak: TimeSpan.FromMilliseconds(route.QosOptions.DurationOfBreak),
onBreak: (ex, breakDelay) =>
{
_logger.LogError(
logger.LogError(
".Breaker logging: Breaking the circuit for " + breakDelay.TotalMilliseconds + "ms!", ex);
},
onReset: () =>
{
_logger.LogDebug(".Breaker logging: Call ok! Closed the circuit again.");
logger.LogDebug(".Breaker logging: Call ok! Closed the circuit again.");
},
onHalfOpen: () =>
{
_logger.LogDebug(".Breaker logging: Half-open; next call is a trial.");
logger.LogDebug(".Breaker logging: Half-open; next call is a trial.");
}
);
}
else
{
_circuitBreakerPolicy = null;
circuitBreakerPolicy = null;
}

CircuitBreaker = new CircuitBreaker(_circuitBreakerPolicy, _timeoutPolicy);
CircuitBreaker = new CircuitBreaker(circuitBreakerPolicy, timeoutPolicy);
}

[Obsolete("do not use: it does nothing")]
public PollyQoSProvider(AsyncCircuitBreakerPolicy circuitBreakerPolicy, AsyncTimeoutPolicy timeoutPolicy, IOcelotLogger logger)
{
}

public CircuitBreaker CircuitBreaker { get; }
Expand Down
2 changes: 1 addition & 1 deletion test/Ocelot.UnitTests/Ocelot.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<PackageReference Include="CacheManager.Core" Version="2.0.0-beta-1629" />
<PackageReference Include="CacheManager.Microsoft.Extensions.Configuration" Version="2.0.0-beta-1629" />
<PackageReference Include="CacheManager.Microsoft.Extensions.Logging" Version="2.0.0-beta-1629" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="Polly" Version="8.0.0" />
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.0.0" />
</ItemGroup>
</Project>

0 comments on commit a815a58

Please sign in to comment.