Skip to content

Commit

Permalink
Merge branch 'dev' into BG/227832/Glob
Browse files Browse the repository at this point in the history
  • Loading branch information
BarryGibney authored Sep 18, 2024
2 parents 92dd513 + d9f1273 commit 0977e3c
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 20 deletions.
19 changes: 17 additions & 2 deletions Web/Edubase.Services/Edubase.Services.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
<Reference Include="Microsoft.Azure.KeyVault.Core, Version=3.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Azure.KeyVault.Core.3.0.5\lib\net461\Microsoft.Azure.KeyVault.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.6.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Bcl.TimeProvider, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.TimeProvider.8.0.0\lib\net462\Microsoft.Bcl.TimeProvider.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.WindowsAzure.Storage, Version=9.3.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<HintPath>..\packages\WindowsAzure.Storage.9.3.3\lib\net45\Microsoft.WindowsAzure.Storage.dll</HintPath>
Expand All @@ -97,13 +103,19 @@
<Reference Include="Newtonsoft.Json.Bson, Version=1.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.Bson.1.0.2\lib\net45\Newtonsoft.Json.Bson.dll</HintPath>
</Reference>
<Reference Include="Polly, Version=6.0.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc, processorArchitecture=MSIL">
<HintPath>..\packages\Polly.6.1.2\lib\netstandard2.0\Polly.dll</HintPath>
<Reference Include="Polly, Version=8.0.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc, processorArchitecture=MSIL">
<HintPath>..\packages\Polly.8.4.1\lib\net472\Polly.dll</HintPath>
</Reference>
<Reference Include="Polly.Core, Version=8.0.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc, processorArchitecture=MSIL">
<HintPath>..\packages\Polly.Core.8.4.1\lib\net472\Polly.Core.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.Annotations, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.ComponentModel.Annotations.4.5.0\lib\net461\System.ComponentModel.Annotations.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -138,6 +150,9 @@
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Approvals\IApprovalService.cs" />
Expand Down
9 changes: 7 additions & 2 deletions Web/Edubase.Services/ExternalLookup/FBService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net;
using System.Net.Http;
using System.Runtime.Caching;
using System.Threading;
using System.Threading.Tasks;
using Edubase.Common;
using Edubase.Services.IntegrationEndPoints;
Expand All @@ -27,7 +28,7 @@ public class FBService : IFBService

private const string FBServiceTimeoutKey = "FBService_Timeout";

private readonly Policy RetryPolicy = PollyUtil.CreateRetryPolicy(
private readonly IAsyncPolicy<HttpResponseMessage> RetryPolicy = PollyUtil.CreateRetryPolicy(
PollyUtil.CsvSecondsToTimeSpans(
ConfigurationManager.AppSettings["FBService_RetryIntervals"]
), FBServiceTimeoutKey
Expand Down Expand Up @@ -103,7 +104,11 @@ public async Task<bool> CheckExists(int? lookupId, FbType lookupType)

try
{
using (var response = await RetryPolicy.ExecuteAsync(async () => await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)))
using (var response = await RetryPolicy.ExecuteAsync(async (context, cancellationToken) =>
{
return await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead,
cancellationToken);
}, new Context(), CancellationToken.None))
{
var isOk = response.StatusCode == HttpStatusCode.OK;
MemoryCache.Default.Set(new CacheItem(key, isOk), new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddHours(cacheTime) });
Expand Down
2 changes: 1 addition & 1 deletion Web/Edubase.Services/ExternalLookup/FSCPService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class FSCPDService : IFSCPDService

private const string FBServiceTimeoutKey = "FscpdClient_Timeout";

private readonly Policy RetryPolicy = PollyUtil.CreateRetryPolicy(
private readonly IAsyncPolicy<HttpResponseMessage> RetryPolicy = PollyUtil.CreateRetryPolicy(
PollyUtil.CsvSecondsToTimeSpans(
ConfigurationManager.AppSettings["FscpdClient_RetryIntervals"]
), FBServiceTimeoutKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class AzureMapsService : IAzureMapsService

private const string AzureMapServiceTimeoutKey = "AzureMapService_Timeout";

private readonly Policy RetryPolicy = PollyUtil.CreateRetryPolicy(
private readonly IAsyncPolicy<HttpResponseMessage> RetryPolicy = PollyUtil.CreateRetryPolicy(
PollyUtil.CsvSecondsToTimeSpans(
ConfigurationManager.AppSettings["AzureMapService_RetryIntervals"]
), AzureMapServiceTimeoutKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class OSPlacesApiService : IOSPlacesApiService

private const string OSPlacesApiServicesTimeoutKey = "OSPlacesApiServices_Timeout";

private readonly Policy RetryPolicy = PollyUtil.CreateRetryPolicy(
private readonly IAsyncPolicy<HttpResponseMessage> RetryPolicy = PollyUtil.CreateRetryPolicy(
PollyUtil.CsvSecondsToTimeSpans(
ConfigurationManager.AppSettings["OSPlacesApiServices_RetryIntervals"]
), OSPlacesApiServicesTimeoutKey
Expand Down
12 changes: 6 additions & 6 deletions Web/Edubase.Services/IntegrationEndPoints/PollyUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ public static class PollyUtil
/// A retry policy that handles <see cref="HttpRequestException"/> and waits for the specified retry intervals.
/// If <paramref name="retryIntervals"/> is null or empty, returns a no-op policy that doesn't perform any retries.
/// </returns>
public static Policy CreateRetryPolicy(TimeSpan[] retryIntervals, string settingsKey)
public static IAsyncPolicy<HttpResponseMessage> CreateRetryPolicy(TimeSpan[] retryIntervals, string settingsKey)
{
if(retryIntervals is null || retryIntervals.Length == 0)
{
return Policy.NoOp();
return Policy.NoOpAsync<HttpResponseMessage>();
}

var retryPolicy = Policy
var retryPolicy = Policy<HttpResponseMessage>
.Handle<HttpRequestException>()
.Or<TaskCanceledException>()
.WaitAndRetryAsync(retryIntervals);

var timeoutPolicy = CreateTimeoutPolicy(settingsKey);

return Policy.WrapAsync(retryPolicy, timeoutPolicy);
return Policy.WrapAsync<HttpResponseMessage>(retryPolicy, timeoutPolicy);
}

public static Policy CreateTimeoutPolicy(string settingsKey)
public static IAsyncPolicy<HttpResponseMessage> CreateTimeoutPolicy(string settingsKey)
{
if (!int.TryParse(ConfigurationManager.AppSettings[settingsKey], out var timeoutSettings))
{
timeoutSettings = 10;
}

return Policy.TimeoutAsync(TimeSpan.FromSeconds(timeoutSettings));
return Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(timeoutSettings));
}

/// <summary>
Expand Down
7 changes: 6 additions & 1 deletion Web/Edubase.Services/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
<package id="Humanizer.Core" version="2.14.1" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Client" version="6.0.0" targetFramework="net48" />
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net48" />
<package id="Polly" version="8.4.1" targetFramework="net48" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="6.0.0" targetFramework="net48" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net48" />
<package id="Microsoft.Bcl.TimeProvider" version="8.0.0" targetFramework="net48" />
<package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net48" />
<package id="Newtonsoft.Json.Bson" version="1.0.2" targetFramework="net48" />
<package id="Polly" version="6.1.2" targetFramework="net48" />
<package id="Polly.Core" version="8.4.1" targetFramework="net48" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.ComponentModel.Annotations" version="4.5.0" targetFramework="net48" />
<package id="System.Memory" version="4.5.5" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Polly">
<Version>6.1.2</Version>
<Version>8.4.1</Version>
</PackageReference>
<PackageReference Include="xunit">
<Version>2.9.0</Version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
using System.Configuration;
using Polly.Timeout;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Threading;
using Polly;

namespace Edubase.ServicesUnitTests.IntegrationEndPoints
{
Expand All @@ -17,14 +20,14 @@ public class PollyUtilTests
public void CreateRetryPolicy_ReturnsNoOpPolicy_WhenNullIntervalsPassedIn()
{
var policy = PollyUtil.CreateRetryPolicy(null,"");
Assert.IsType<NoOpPolicy>(policy);
Assert.IsAssignableFrom<IAsyncPolicy<HttpResponseMessage>>(policy);
}

[Fact]
public void CreateRetryPolicy_ReturnsNoOpPolicy_WhenEmptyIntervalsPassedIn()
{
var policy = PollyUtil.CreateRetryPolicy(new TimeSpan[0],"");
Assert.IsType<NoOpPolicy>(policy);
Assert.IsAssignableFrom<IAsyncPolicy<HttpResponseMessage>>(policy);
}

[Fact]
Expand All @@ -36,7 +39,7 @@ public void CreateRetryPolicy_ReturnsPolicyWrap_WhenIntervalsPassedIn()
var policy = PollyUtil.CreateRetryPolicy(retryIntervals, settingsKey);

Assert.NotNull(policy);
Assert.IsType<PolicyWrap>(policy);
Assert.IsAssignableFrom<IAsyncPolicy<HttpResponseMessage>>(policy);
}

//Note: Polly doesn't expose the timeout settings once the policy is created
Expand All @@ -49,12 +52,14 @@ public async Task CreateTimeoutPolicy_ShouldTriggerTimeout()

var policy = PollyUtil.CreateTimeoutPolicy(validKey);

Func<CancellationToken, Task> operation = async (ct) =>
Func<CancellationToken, Task<HttpResponseMessage>> operation = async (ct) =>
{
await Task.Delay(6000, ct);
return new HttpResponseMessage(HttpStatusCode.OK);
};

await Assert.ThrowsAsync<TimeoutRejectedException>(() => policy.ExecuteAsync(operation, CancellationToken.None));
await Assert.ThrowsAsync<TimeoutRejectedException>(async () =>
await policy.ExecuteAsync(operation, CancellationToken.None));
Assert.NotNull(policy);
}

Expand All @@ -74,6 +79,7 @@ public async Task CreateRetryPolicy_DefaultsTo10Seconds()
await policy.ExecuteAsync(async (ct) =>
{
await Task.Delay(11000, ct);
return new HttpResponseMessage(HttpStatusCode.OK);
}, CancellationToken.None);
}
catch (Exception ex)
Expand Down

0 comments on commit 0977e3c

Please sign in to comment.