retry policy defined via config
This commit is contained in:
		
							parent
							
								
									8d033f0951
								
							
						
					
					
						commit
						1b38589ff0
					
				| @ -15,16 +15,17 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ | ||||
|     { | ||||
|         private readonly IConnectionFactory _connectionFactory; | ||||
|         private readonly ILogger<DefaultRabbitMQPersistentConnection> _logger; | ||||
| 
 | ||||
|         private readonly int _retryCount; | ||||
|         IConnection _connection; | ||||
|         bool _disposed; | ||||
| 
 | ||||
|         object sync_root = new object(); | ||||
| 
 | ||||
|         public DefaultRabbitMQPersistentConnection(IConnectionFactory connectionFactory,ILogger<DefaultRabbitMQPersistentConnection> logger) | ||||
|         public DefaultRabbitMQPersistentConnection(IConnectionFactory connectionFactory, ILogger<DefaultRabbitMQPersistentConnection> logger, int retryCount = 5) | ||||
|         { | ||||
|             _connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory)); | ||||
|             _logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||||
|             _retryCount = retryCount; | ||||
|         } | ||||
| 
 | ||||
|         public bool IsConnected | ||||
| @ -69,7 +70,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ | ||||
|             { | ||||
|                 var policy = RetryPolicy.Handle<SocketException>() | ||||
|                     .Or<BrokerUnreachableException>() | ||||
|                     .WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => | ||||
|                     .WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => | ||||
|                     { | ||||
|                         _logger.LogWarning(ex.ToString()); | ||||
|                     } | ||||
|  | ||||
| @ -26,19 +26,20 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ | ||||
|         private readonly IEventBusSubscriptionsManager _subsManager; | ||||
|         private readonly ILifetimeScope _autofac; | ||||
|         private readonly string AUTOFAC_SCOPE_NAME = "eshop_event_bus"; | ||||
|         private readonly int _retryCount; | ||||
| 
 | ||||
|         private IModel _consumerChannel; | ||||
|         private string _queueName; | ||||
| 
 | ||||
|         public EventBusRabbitMQ(IRabbitMQPersistentConnection persistentConnection, ILogger<EventBusRabbitMQ> logger, | ||||
|             ILifetimeScope autofac, IEventBusSubscriptionsManager subsManager) | ||||
|             ILifetimeScope autofac, IEventBusSubscriptionsManager subsManager, int retryCount = 5) | ||||
|         { | ||||
|             _persistentConnection = persistentConnection ?? throw new ArgumentNullException(nameof(persistentConnection)); | ||||
|             _logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||||
|             _subsManager = subsManager ?? new InMemoryEventBusSubscriptionsManager(); | ||||
|             _consumerChannel = CreateConsumerChannel(); | ||||
|             _autofac = autofac; | ||||
| 
 | ||||
|             _retryCount = retryCount; | ||||
|             _subsManager.OnEventRemoved += SubsManager_OnEventRemoved; | ||||
|         } | ||||
| 
 | ||||
| @ -72,7 +73,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ | ||||
| 
 | ||||
|             var policy = RetryPolicy.Handle<BrokerUnreachableException>() | ||||
|                 .Or<SocketException>() | ||||
|                 .WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => | ||||
|                 .WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => | ||||
|                 { | ||||
|                     _logger.LogWarning(ex.ToString()); | ||||
|                 }); | ||||
|  | ||||
| @ -120,7 +120,13 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API | ||||
|                         factory.Password = Configuration["EventBusPassword"]; | ||||
|                     } | ||||
| 
 | ||||
|                     return new DefaultRabbitMQPersistentConnection(factory, logger); | ||||
|                     var retryCount = 5; | ||||
|                     if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) | ||||
|                     { | ||||
|                         retryCount = int.Parse(Configuration["EventBusRetryCount"]); | ||||
|                     } | ||||
| 
 | ||||
|                     return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); | ||||
|                 }); | ||||
|             } | ||||
| 
 | ||||
| @ -247,7 +253,21 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 services.AddSingleton<IEventBus, EventBusRabbitMQ>(); | ||||
|                 services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp => | ||||
|                 { | ||||
|                     var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>(); | ||||
|                     var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); | ||||
|                     var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>(); | ||||
|                     var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); | ||||
| 
 | ||||
|                     var retryCount = 5; | ||||
|                     if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) | ||||
|                     { | ||||
|                         retryCount = int.Parse(Configuration["EventBusRetryCount"]); | ||||
|                     } | ||||
| 
 | ||||
|                     return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, retryCount); | ||||
|                 }); | ||||
|             } | ||||
| 
 | ||||
|             services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); | ||||
|  | ||||
| @ -13,5 +13,6 @@ | ||||
|   "SubscriptionClientName": "Basket", | ||||
|   "ApplicationInsights": { | ||||
|     "InstrumentationKey": "" | ||||
|   } | ||||
|   }, | ||||
|   "EventBusRetryCount": 5 | ||||
| } | ||||
| @ -161,7 +161,13 @@ | ||||
|                         factory.Password = Configuration["EventBusPassword"]; | ||||
|                     } | ||||
| 
 | ||||
|                     return new DefaultRabbitMQPersistentConnection(factory, logger); | ||||
|                     var retryCount = 5; | ||||
|                     if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) | ||||
|                     { | ||||
|                         retryCount = int.Parse(Configuration["EventBusRetryCount"]); | ||||
|                     } | ||||
| 
 | ||||
|                     return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); | ||||
|                 }); | ||||
|             } | ||||
| 
 | ||||
| @ -221,7 +227,21 @@ | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 services.AddSingleton<IEventBus, EventBusRabbitMQ>(); | ||||
|                 services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp => | ||||
|                 { | ||||
|                     var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>(); | ||||
|                     var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); | ||||
|                     var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>(); | ||||
|                     var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); | ||||
| 
 | ||||
|                     var retryCount = 5; | ||||
|                     if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) | ||||
|                     { | ||||
|                         retryCount = int.Parse(Configuration["EventBusRetryCount"]); | ||||
|                     } | ||||
| 
 | ||||
|                     return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, retryCount); | ||||
|                 }); | ||||
|             } | ||||
| 
 | ||||
|             services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); | ||||
|  | ||||
| @ -15,5 +15,6 @@ | ||||
|   "SubscriptionClientName": "Catalog", | ||||
|   "ApplicationInsights": { | ||||
|     "InstrumentationKey": "" | ||||
|   } | ||||
|   }, | ||||
|   "EventBusRetryCount": 5 | ||||
| } | ||||
|  | ||||
| @ -87,7 +87,13 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API | ||||
|                         factory.Password = Configuration["EventBusPassword"]; | ||||
|                     } | ||||
| 
 | ||||
|                     return new DefaultRabbitMQPersistentConnection(factory, logger); | ||||
|                     var retryCount = 5; | ||||
|                     if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) | ||||
|                     { | ||||
|                         retryCount = int.Parse(Configuration["EventBusRetryCount"]); | ||||
|                     } | ||||
| 
 | ||||
|                     return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); | ||||
|                 }); | ||||
|             } | ||||
| 
 | ||||
| @ -217,7 +223,21 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 services.AddSingleton<IEventBus, EventBusRabbitMQ>(); | ||||
|                 services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp => | ||||
|                 { | ||||
|                     var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>(); | ||||
|                     var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); | ||||
|                     var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>(); | ||||
|                     var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); | ||||
| 
 | ||||
|                     var retryCount = 5; | ||||
|                     if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) | ||||
|                     { | ||||
|                         retryCount = int.Parse(Configuration["EventBusRetryCount"]); | ||||
|                     } | ||||
| 
 | ||||
|                     return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, retryCount); | ||||
|                 }); | ||||
|             } | ||||
| 
 | ||||
|             services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); | ||||
|  | ||||
| @ -1,10 +0,0 @@ | ||||
| { | ||||
|   "Logging": { | ||||
|     "IncludeScopes": false, | ||||
|     "LogLevel": { | ||||
|       "Default": "Debug", | ||||
|       "System": "Information", | ||||
|       "Microsoft": "Information" | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @ -14,5 +14,6 @@ | ||||
|   "SubscriptionClientName": "Locations", | ||||
|   "ApplicationInsights": { | ||||
|     "InstrumentationKey": "" | ||||
|   } | ||||
|   }, | ||||
|   "EventBusRetryCount": 5 | ||||
| } | ||||
| @ -122,7 +122,14 @@ | ||||
|                     { | ||||
|                         factory.Password = Configuration["EventBusPassword"]; | ||||
|                     } | ||||
|                     return new DefaultRabbitMQPersistentConnection(factory, logger); | ||||
| 
 | ||||
|                     var retryCount = 5; | ||||
|                     if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) | ||||
|                     { | ||||
|                         retryCount = int.Parse(Configuration["EventBusRetryCount"]); | ||||
|                     } | ||||
| 
 | ||||
|                     return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); | ||||
|                 }); | ||||
|             }             | ||||
| 
 | ||||
| @ -242,7 +249,21 @@ | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 services.AddSingleton<IEventBus, EventBusRabbitMQ>(); | ||||
|                 services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp => | ||||
|                 { | ||||
|                     var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>(); | ||||
|                     var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); | ||||
|                     var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>(); | ||||
|                     var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); | ||||
| 
 | ||||
|                     var retryCount = 5; | ||||
|                     if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) | ||||
|                     { | ||||
|                         retryCount = int.Parse(Configuration["EventBusRetryCount"]); | ||||
|                     } | ||||
| 
 | ||||
|                     return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, retryCount); | ||||
|                 }); | ||||
|             } | ||||
| 
 | ||||
|             services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); | ||||
|  | ||||
| @ -1,8 +0,0 @@ | ||||
| { | ||||
|   "Logging": { | ||||
|     "IncludeScopes": false, | ||||
|     "LogLevel": { | ||||
|       "Default": "Warning" | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @ -15,5 +15,6 @@ | ||||
|   "AzureStorageEnabled": false, | ||||
|   "ApplicationInsights": { | ||||
|     "InstrumentationKey": "" | ||||
|   } | ||||
|   }, | ||||
|   "EventBusRetryCount": 5 | ||||
| } | ||||
| @ -180,7 +180,13 @@ | ||||
|                         factory.Password = Configuration["EventBusPassword"]; | ||||
|                     } | ||||
| 
 | ||||
|                     return new DefaultRabbitMQPersistentConnection(factory, logger); | ||||
|                     var retryCount = 5; | ||||
|                     if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) | ||||
|                     { | ||||
|                         retryCount = int.Parse(Configuration["EventBusRetryCount"]); | ||||
|                     } | ||||
| 
 | ||||
|                     return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); | ||||
|                 }); | ||||
|             } | ||||
| 
 | ||||
| @ -284,7 +290,21 @@ | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 services.AddSingleton<IEventBus, EventBusRabbitMQ>(); | ||||
|                 services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp => | ||||
|                 { | ||||
|                     var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>(); | ||||
|                     var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); | ||||
|                     var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>(); | ||||
|                     var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); | ||||
| 
 | ||||
|                     var retryCount = 5; | ||||
|                     if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) | ||||
|                     { | ||||
|                         retryCount = int.Parse(Configuration["EventBusRetryCount"]); | ||||
|                     } | ||||
| 
 | ||||
|                     return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, retryCount); | ||||
|                 }); | ||||
|             } | ||||
| 
 | ||||
|             services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); | ||||
|  | ||||
| @ -16,5 +16,6 @@ | ||||
|   "CheckUpdateTime": "30000", | ||||
|   "ApplicationInsights": { | ||||
|     "InstrumentationKey": "" | ||||
|   } | ||||
|   }, | ||||
|   "EventBusRetryCount": 5 | ||||
| } | ||||
|  | ||||
| @ -74,7 +74,13 @@ namespace Payment.API | ||||
|                         factory.Password = Configuration["EventBusPassword"]; | ||||
|                     } | ||||
| 
 | ||||
|                     return new DefaultRabbitMQPersistentConnection(factory, logger); | ||||
|                     var retryCount = 5; | ||||
|                     if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) | ||||
|                     { | ||||
|                         retryCount = int.Parse(Configuration["EventBusRetryCount"]); | ||||
|                     } | ||||
| 
 | ||||
|                     return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); | ||||
|                 }); | ||||
|             } | ||||
| 
 | ||||
| @ -123,7 +129,21 @@ namespace Payment.API | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 services.AddSingleton<IEventBus, EventBusRabbitMQ>(); | ||||
|                 services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp => | ||||
|                 { | ||||
|                     var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>(); | ||||
|                     var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); | ||||
|                     var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>(); | ||||
|                     var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); | ||||
| 
 | ||||
|                     var retryCount = 5; | ||||
|                     if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) | ||||
|                     { | ||||
|                         retryCount = int.Parse(Configuration["EventBusRetryCount"]); | ||||
|                     } | ||||
| 
 | ||||
|                     return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, retryCount); | ||||
|                 }); | ||||
|             } | ||||
| 
 | ||||
|             services.AddTransient<OrderStatusChangedToStockConfirmedIntegrationEventHandler>(); | ||||
|  | ||||
| @ -10,5 +10,6 @@ | ||||
|   "SubscriptionClientName": "Payment", | ||||
|   "ApplicationInsights": { | ||||
|     "InstrumentationKey": "" | ||||
|   } | ||||
|   }, | ||||
|   "EventBusRetryCount": 5 | ||||
| } | ||||
|  | ||||
| @ -9,9 +9,16 @@ namespace Microsoft.eShopOnContainers.WebMVC.Infrastructure | ||||
|     public class ResilientHttpClientFactory : IResilientHttpClientFactory | ||||
|     { | ||||
|         private readonly ILogger<ResilientHttpClient> _logger; | ||||
|         private readonly int _retryCount; | ||||
|         private readonly int _exceptionsAllowedBeforeBreaking; | ||||
| 
 | ||||
|         public ResilientHttpClientFactory(ILogger<ResilientHttpClient> logger, int exceptionsAllowedBeforeBreaking = 5, int retryCount = 6) | ||||
|         { | ||||
|             _logger = logger; | ||||
|             _exceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking; | ||||
|             _retryCount = retryCount; | ||||
|         } | ||||
| 
 | ||||
|         public ResilientHttpClientFactory(ILogger<ResilientHttpClient> logger)  | ||||
|             =>_logger = logger;         | ||||
| 
 | ||||
|         public ResilientHttpClient CreateResilientHttpClient() | ||||
|             => new ResilientHttpClient((origin) => CreatePolicies(), _logger); | ||||
| @ -22,7 +29,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Infrastructure | ||||
|                 Policy.Handle<HttpRequestException>() | ||||
|                 .WaitAndRetryAsync( | ||||
|                     // number of retries | ||||
|                     6, | ||||
|                     _retryCount, | ||||
|                     // exponential backofff | ||||
|                     retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), | ||||
|                     // on retry | ||||
| @ -38,7 +45,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Infrastructure | ||||
|                 Policy.Handle<HttpRequestException>() | ||||
|                 .CircuitBreakerAsync(  | ||||
|                    // number of exceptions before breaking circuit | ||||
|                    5, | ||||
|                    _exceptionsAllowedBeforeBreaking, | ||||
|                    // time circuit opened before retry | ||||
|                    TimeSpan.FromMinutes(1), | ||||
|                    (exception, duration) => | ||||
|  | ||||
| @ -79,7 +79,24 @@ namespace Microsoft.eShopOnContainers.WebMVC | ||||
| 
 | ||||
|             if (Configuration.GetValue<string>("UseResilientHttp") == bool.TrueString) | ||||
|             { | ||||
|                 services.AddSingleton<IResilientHttpClientFactory, ResilientHttpClientFactory>(); | ||||
|                 services.AddSingleton<IResilientHttpClientFactory, ResilientHttpClientFactory>(sp => | ||||
|                 { | ||||
|                     var logger = sp.GetRequiredService<ILogger<ResilientHttpClient>>(); | ||||
| 
 | ||||
|                     var retryCount = 6; | ||||
|                     if (!string.IsNullOrEmpty(Configuration["HttpClientRetryCount"])) | ||||
|                     { | ||||
|                         retryCount = int.Parse(Configuration["HttpClientRetryCount"]); | ||||
|                     } | ||||
| 
 | ||||
|                     var exceptionsAllowedBeforeBreaking = 5; | ||||
|                     if (!string.IsNullOrEmpty(Configuration["HttpClientExceptionsAllowedBeforeBreaking"])) | ||||
|                     { | ||||
|                         exceptionsAllowedBeforeBreaking = int.Parse(Configuration["HttpClientExceptionsAllowedBeforeBreaking"]); | ||||
|                     } | ||||
| 
 | ||||
|                     return new ResilientHttpClientFactory(logger, exceptionsAllowedBeforeBreaking, retryCount); | ||||
|                 }); | ||||
|                 services.AddSingleton<IHttpClient, ResilientHttpClient>(sp => sp.GetService<IResilientHttpClientFactory>().CreateResilientHttpClient()); | ||||
|             } | ||||
|             else | ||||
|  | ||||
| @ -21,5 +21,7 @@ | ||||
|   }, | ||||
|   "ApplicationInsights": { | ||||
|     "InstrumentationKey": "" | ||||
|   } | ||||
|   }, | ||||
|   "HttpClientRetryCount": 6, | ||||
|   "HttpClientExceptionsAllowedBeforeBreaking": 5 | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user