From 9851ab798d1a90411b364c3f594ef8786a504af3 Mon Sep 17 00:00:00 2001 From: devinleighsmith Date: Wed, 29 Jan 2025 23:56:59 -0800 Subject: [PATCH] code review corrections. --- source/backend/api/Startup.cs | 2 +- .../RestCommon/BaseRestRepository.cs | 3 ++- source/backend/core/Http/HttpRequestClient.cs | 4 ++-- source/backend/scheduler/Startup.cs | 17 +++++++++++++++++ source/backend/scheduler/appsettings.json | 6 +++++- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/source/backend/api/Startup.cs b/source/backend/api/Startup.cs index b5e9049505..18f178987e 100644 --- a/source/backend/api/Startup.cs +++ b/source/backend/api/Startup.cs @@ -240,7 +240,7 @@ public void ConfigureServices(IServiceCollection services) PollyOptions pollyOptions = new(); this.Configuration.GetSection("Polly").Bind(pollyOptions); - services.AddResiliencePipeline("retry-network-policy", (builder) => + services.AddResiliencePipeline(HttpRequestClient.NetworkPolicyName, (builder) => { builder.AddRetry(new() { diff --git a/source/backend/core.api/Repositories/RestCommon/BaseRestRepository.cs b/source/backend/core.api/Repositories/RestCommon/BaseRestRepository.cs index 9bb6d0a213..28bb734483 100644 --- a/source/backend/core.api/Repositories/RestCommon/BaseRestRepository.cs +++ b/source/backend/core.api/Repositories/RestCommon/BaseRestRepository.cs @@ -14,6 +14,7 @@ using Pims.Api.Models; using Pims.Api.Models.CodeTypes; using Pims.Api.Models.Requests.Http; +using Pims.Core.Http; using Polly; using Polly.Registry; @@ -45,7 +46,7 @@ protected BaseRestRepository( _logger = logger; _httpClientFactory = httpClientFactory; _jsonOptions = jsonOptions; - _resiliencePipeline = pollyPipelineProvider.GetPipeline("retry-network-policy"); + _resiliencePipeline = pollyPipelineProvider.GetPipeline(HttpRequestClient.NetworkPolicyName); } public abstract void AddAuthentication(HttpClient client, string authenticationToken = null); diff --git a/source/backend/core/Http/HttpRequestClient.cs b/source/backend/core/Http/HttpRequestClient.cs index 67495351a6..be9c89652a 100644 --- a/source/backend/core/Http/HttpRequestClient.cs +++ b/source/backend/core/Http/HttpRequestClient.cs @@ -4,7 +4,6 @@ using System.Text; using System.Text.Json; using System.Text.Json.Serialization; -using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -20,6 +19,7 @@ namespace Pims.Core.Http public class HttpRequestClient : IHttpRequestClient { #region Variables + public static string NetworkPolicyName = "retry-network-policy"; protected readonly ResiliencePipeline _resiliencePipeline; private readonly JsonSerializerOptions _serializeOptions; private readonly ILogger _logger; @@ -52,7 +52,7 @@ public HttpRequestClient(IHttpClientFactory clientFactory, IOptionsMonitor("retry-network-policy"); + _resiliencePipeline = pollyPipelineProvider.GetPipeline(HttpRequestClient.NetworkPolicyName); } #endregion diff --git a/source/backend/scheduler/Startup.cs b/source/backend/scheduler/Startup.cs index 698b516cf3..8dab6abee4 100644 --- a/source/backend/scheduler/Startup.cs +++ b/source/backend/scheduler/Startup.cs @@ -4,6 +4,7 @@ using System.IdentityModel.Tokens.Jwt; using System.IO; using System.Linq; +using System.Net.Http; using System.Reflection; using System.Security.Claims; using System.Text; @@ -32,6 +33,7 @@ using Pims.Core.Api.Handlers; using Pims.Core.Api.Helpers; using Pims.Core.Api.Middleware; +using Pims.Core.Configuration; using Pims.Core.Converters; using Pims.Core.Http; using Pims.Core.Json; @@ -42,6 +44,7 @@ using Pims.Scheduler.Repositories; using Pims.Scheduler.Rescheduler; using Pims.Scheduler.Services; +using Polly; using Prometheus; using StackExchange.Redis; @@ -198,6 +201,20 @@ public void ConfigureServices(IServiceCollection services) services.AddResponseCaching(); services.AddMemoryCache(); + PollyOptions pollyOptions = new(); + this.Configuration.GetSection("Polly").Bind(pollyOptions); + + services.AddResiliencePipeline(HttpRequestClient.NetworkPolicyName, (builder) => + { + builder.AddRetry(new() + { + BackoffType = DelayBackoffType.Exponential, + Delay = TimeSpan.FromSeconds(pollyOptions.DelayInSeconds), + MaxRetryAttempts = pollyOptions.MaxRetries, + ShouldHandle = new PredicateBuilder().Handle().HandleResult(response => (int)response.StatusCode >= 500 && (int)response.StatusCode <= 599), + }); + }); + // Export metrics from all HTTP clients registered in services services.UseHttpClientMetrics(); diff --git a/source/backend/scheduler/appsettings.json b/source/backend/scheduler/appsettings.json index a2db478a12..cbd0a79344 100644 --- a/source/backend/scheduler/appsettings.json +++ b/source/backend/scheduler/appsettings.json @@ -47,7 +47,7 @@ }, "Server": { "HeartbeatInterval": "00:00:30", - "Queues": ["scheduler"], + "Queues": [ "scheduler" ], "SchedulePollingInterval": "00:00:15", "ServerCheckInterval": "00:05:00", "ServerName": "scheduler", @@ -150,5 +150,9 @@ }, "ConnectionStrings": { "Redis": "scheduler-redis:6379" + }, + "Polly": { + "MaxRetries": 3, + "DelayInSeconds": 1 } }