From 6db9917621375565404e4904fb4686b8fa0aab54 Mon Sep 17 00:00:00 2001 From: Miguel Veloso Date: Thu, 17 Oct 2019 13:59:28 +0100 Subject: [PATCH] Implement jitter with new algorith in Polly contrib code helper --- .../aggregator/Mobile.Shopping.HttpAggregator.csproj | 1 + src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs | 5 ++++- src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs | 5 ++++- .../aggregator/Web.Shopping.HttpAggregator.csproj | 1 + .../DefaultRabbitMQPersistentConnection.cs | 7 +++++-- .../EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs | 7 +++++-- .../EventBus/EventBusRabbitMQ/EventBusRabbitMQ.csproj | 1 + src/Web/WebMVC/Startup.cs | 5 ++++- src/Web/WebMVC/WebMVC.csproj | 1 + 9 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Mobile.Shopping.HttpAggregator.csproj b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Mobile.Shopping.HttpAggregator.csproj index 5fbc5ec63..f77674cb1 100644 --- a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Mobile.Shopping.HttpAggregator.csproj +++ b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Mobile.Shopping.HttpAggregator.csproj @@ -17,6 +17,7 @@ + diff --git a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs index eee0d9c6e..a9db0ad6f 100644 --- a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs +++ b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs @@ -21,6 +21,7 @@ using HealthChecks.UI.Client; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks; using Devspaces.Support; +using Polly.Contrib.WaitAndRetry; namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator { @@ -208,10 +209,12 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator private static IAsyncPolicy GetRetryPolicy() { + var delay = Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(2), retryCount: 6); + return HttpPolicyExtensions .HandleTransientHttpError() .OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound) - .WaitAndRetryAsync(6, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); + .WaitAndRetryAsync(delay); } private static IAsyncPolicy GetCircuitBreakerPolicy() diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs b/src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs index dd5e1cc8c..a35c2e7a0 100644 --- a/src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs @@ -22,6 +22,7 @@ using HealthChecks.UI.Client; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks; using Devspaces.Support; +using Polly.Contrib.WaitAndRetry; namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator { @@ -207,10 +208,12 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator static IAsyncPolicy GetRetryPolicy() { + var delay = Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(2), retryCount: 6); + return HttpPolicyExtensions .HandleTransientHttpError() .OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound) - .WaitAndRetryAsync(6, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); + .WaitAndRetryAsync(delay); } diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/Web.Shopping.HttpAggregator.csproj b/src/ApiGateways/Web.Bff.Shopping/aggregator/Web.Shopping.HttpAggregator.csproj index abfd4ab03..abef91a2b 100644 --- a/src/ApiGateways/Web.Bff.Shopping/aggregator/Web.Shopping.HttpAggregator.csproj +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/Web.Shopping.HttpAggregator.csproj @@ -16,6 +16,7 @@ + diff --git a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/DefaultRabbitMQPersistentConnection.cs b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/DefaultRabbitMQPersistentConnection.cs index 93e5b2917..04f518953 100644 --- a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/DefaultRabbitMQPersistentConnection.cs +++ b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/DefaultRabbitMQPersistentConnection.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Logging; using Polly; +using Polly.Contrib.WaitAndRetry; using Polly.Retry; using RabbitMQ.Client; using RabbitMQ.Client.Events; @@ -68,11 +69,13 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ lock (sync_root) { + var delay = Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(2), retryCount: _retryCount); + var policy = RetryPolicy.Handle() .Or() - .WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => + .WaitAndRetry(delay, (ex, time) => { - _logger.LogWarning(ex, "RabbitMQ Client could not connect after {TimeOut}s ({ExceptionMessage})", $"{time.TotalSeconds:n1}", ex.Message); + _logger.LogWarning(ex, "RabbitMQ Client could not connect after {TimeOut}s ({ExceptionMessage})", $"{time.TotalSeconds:n3}", ex.Message); } ); diff --git a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs index 5623549d5..345d62811 100644 --- a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs +++ b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Polly; +using Polly.Contrib.WaitAndRetry; using Polly.Retry; using RabbitMQ.Client; using RabbitMQ.Client.Events; @@ -73,11 +74,13 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ _persistentConnection.TryConnect(); } + var delay = Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(2), retryCount: _retryCount); + var policy = RetryPolicy.Handle() .Or() - .WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => + .WaitAndRetry(delay, (ex, time) => { - _logger.LogWarning(ex, "Could not publish event: {EventId} after {Timeout}s ({ExceptionMessage})", @event.Id, $"{time.TotalSeconds:n1}", ex.Message); + _logger.LogWarning(ex, "Could not publish event: {EventId} after {Timeout}s ({ExceptionMessage})", @event.Id, $"{time.TotalSeconds:n3}", ex.Message); }); var eventName = @event.GetType().Name; diff --git a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.csproj b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.csproj index 62373d1b3..a8d6e6806 100644 --- a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.csproj +++ b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.csproj @@ -11,6 +11,7 @@ + diff --git a/src/Web/WebMVC/Startup.cs b/src/Web/WebMVC/Startup.cs index 52a311369..dd3fddad6 100644 --- a/src/Web/WebMVC/Startup.cs +++ b/src/Web/WebMVC/Startup.cs @@ -17,6 +17,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Logging; using Polly; +using Polly.Contrib.WaitAndRetry; using Polly.Extensions.Http; using StackExchange.Redis; using System; @@ -274,10 +275,12 @@ namespace Microsoft.eShopOnContainers.WebMVC static IAsyncPolicy GetRetryPolicy() { + var delay = Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(2), retryCount: 6); + return HttpPolicyExtensions .HandleTransientHttpError() .OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound) - .WaitAndRetryAsync(6, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); + .WaitAndRetryAsync(delay); } static IAsyncPolicy GetCircuitBreakerPolicy() diff --git a/src/Web/WebMVC/WebMVC.csproj b/src/Web/WebMVC/WebMVC.csproj index ef204dfb6..f10782663 100644 --- a/src/Web/WebMVC/WebMVC.csproj +++ b/src/Web/WebMVC/WebMVC.csproj @@ -36,6 +36,7 @@ +