add polly for grpc calls

This commit is contained in:
ericuss 2019-09-04 17:27:45 +02:00
parent adb061cc27
commit bcb0455d1a
2 changed files with 22 additions and 4 deletions

View File

@ -3,6 +3,7 @@ using System.Threading.Tasks;
using System; using System;
using Grpc.Core; using Grpc.Core;
using Serilog; using Serilog;
using Polly;
namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services
{ {
@ -27,7 +28,11 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services
try try
{ {
return await func(httpClient); return await Policy
.Handle<Exception>(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) catch (RpcException e)
{ {
@ -59,7 +64,11 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services
try try
{ {
await func(httpClient); await Policy
.Handle<Exception>(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) catch (RpcException e)
{ {

View File

@ -3,6 +3,7 @@ using System.Threading.Tasks;
using System; using System;
using Grpc.Core; using Grpc.Core;
using Serilog; using Serilog;
using Polly;
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
{ {
@ -27,7 +28,11 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
try try
{ {
return await func(httpClient); return await Policy
.Handle<Exception>(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) catch (RpcException e)
{ {
@ -59,7 +64,11 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
try try
{ {
await func(httpClient); await Policy
.Handle<Exception>(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) catch (RpcException e)
{ {