From 5000f1b3249b7160716ed0eafee000ce787cdf7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E6=98=9F=E7=B9=81?= Date: Wed, 18 Oct 2023 17:59:23 +0800 Subject: [PATCH] feat: auto retry if lock not required --- ....Architecture.Ddd.Cqrs.ServiceAgent.csproj | 1 + .../InjectExtensions.cs | 34 ++++++++++++++----- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent.csproj b/src/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent.csproj index 8115e6f..c5f601b 100644 --- a/src/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent.csproj +++ b/src/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent.csproj @@ -9,5 +9,6 @@ + diff --git a/src/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent/InjectExtensions.cs b/src/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent/InjectExtensions.cs index a950eb4..53bb635 100644 --- a/src/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent/InjectExtensions.cs +++ b/src/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent/InjectExtensions.cs @@ -1,5 +1,8 @@ +using System.Net; using System.Net.Http.Headers; using Microsoft.Extensions.DependencyInjection; +using Polly; +using Polly.Extensions.Http; namespace Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent; @@ -13,17 +16,22 @@ public static class InjectExtensions /// /// The . /// The base uri for api. - /// The type of service agent + /// The polly policy for underlying httpclient. + /// The type of service agent /// - public static IHttpClientBuilder AddServiceAgent(this IServiceCollection services, string baseUri) - where T : CqrsServiceAgent + public static IHttpClientBuilder AddServiceAgent( + this IServiceCollection services, + string baseUri, + IAsyncPolicy? policy = null) + where TClient : class { - return services.AddHttpClient( + policy ??= GetDefaultPolicy(); + return services.AddHttpClient( h => { h.BaseAddress = new Uri(baseUri); h.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/cqrs")); - }); + }).AddPolicyHandler(policy); } /// @@ -31,20 +39,30 @@ public static IHttpClientBuilder AddServiceAgent(this IServiceCollection serv /// /// The . /// The base uri for api. + /// The polly policy for underlying httpclient. /// The type of api client. /// The type of service agent /// public static IHttpClientBuilder AddServiceAgent( this IServiceCollection services, - string baseUri) + string baseUri, + IAsyncPolicy? policy = null) where TClient : class - where TImplementation : CqrsServiceAgent, TClient + where TImplementation : class, TClient { + policy ??= GetDefaultPolicy(); return services.AddHttpClient( h => { h.BaseAddress = new Uri(baseUri); h.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/cqrs")); - }); + }).AddPolicyHandler(policy); + } + + private static IAsyncPolicy GetDefaultPolicy() + { + return HttpPolicyExtensions.HandleTransientHttpError() + .OrResult(msg => msg.StatusCode == HttpStatusCode.TooManyRequests) + .WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(1500)); } }