From bcb0455d1ae744ef1ae940cb0306e50cd4a4eed6 Mon Sep 17 00:00:00 2001 From: ericuss Date: Wed, 4 Sep 2019 17:27:45 +0200 Subject: [PATCH] add polly for grpc calls --- .../aggregator/Services/GrpcCallerService.cs | 13 +++++++++++-- .../aggregator/Services/GrpcCallerService.cs | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/GrpcCallerService.cs b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/GrpcCallerService.cs index e730be327..0209dffb1 100644 --- a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/GrpcCallerService.cs +++ b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/GrpcCallerService.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using System; using Grpc.Core; using Serilog; +using Polly; namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services { @@ -27,7 +28,11 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services try { - return await func(httpClient); + return await Policy + .Handle(ex => true) + .WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (e, t) => Log.Warning("Retrying the call to urlGrpc ={@urlGrpc}, BaseAddress={@BaseAddress}, errorMessage={@message}", urlGrpc, httpClient.BaseAddress,e.Message)) + .ExecuteAsync(() => func(httpClient)) + ; } catch (RpcException e) { @@ -59,7 +64,11 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services try { - await func(httpClient); + await Policy + .Handle(ex => true) + .WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (e, t) => Log.Warning("Retrying the call to urlGrpc ={@urlGrpc}, BaseAddress={@BaseAddress}, errorMessage={@message}", urlGrpc, httpClient.BaseAddress, e.Message)) + .ExecuteAsync(() => func(httpClient)) + ; } catch (RpcException e) { diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/GrpcCallerService.cs b/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/GrpcCallerService.cs index 6400c9797..d69a30f88 100644 --- a/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/GrpcCallerService.cs +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/GrpcCallerService.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using System; using Grpc.Core; using Serilog; +using Polly; namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services { @@ -27,7 +28,11 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services try { - return await func(httpClient); + return await Policy + .Handle(ex => true) + .WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (e, t) => Log.Warning("Retrying the call to urlGrpc ={@urlGrpc}, BaseAddress={@BaseAddress}, errorMessage={@message}", urlGrpc, httpClient.BaseAddress, e.Message)) + .ExecuteAsync(() => func(httpClient)) + ; } catch (RpcException e) { @@ -59,7 +64,11 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services try { - await func(httpClient); + await Policy + .Handle(ex => true) + .WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (e, t) => Log.Warning("Retrying the call to urlGrpc ={@urlGrpc}, BaseAddress={@BaseAddress}, errorMessage={@message}", urlGrpc, httpClient.BaseAddress, e.Message)) + .ExecuteAsync(() => func(httpClient)) + ; } catch (RpcException e) {