Skip to content

Commit 1b38589

Browse files
committed
retry policy defined via config
1 parent 8d033f0 commit 1b38589

19 files changed

Lines changed: 191 additions & 54 deletions

File tree

src/BuildingBlocks/EventBus/EventBusRabbitMQ/DefaultRabbitMQPersisterConnection.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@ public class DefaultRabbitMQPersistentConnection
1515
{
1616
private readonly IConnectionFactory _connectionFactory;
1717
private readonly ILogger<DefaultRabbitMQPersistentConnection> _logger;
18-
18+
private readonly int _retryCount;
1919
IConnection _connection;
2020
bool _disposed;
2121

2222
object sync_root = new object();
2323

24-
public DefaultRabbitMQPersistentConnection(IConnectionFactory connectionFactory,ILogger<DefaultRabbitMQPersistentConnection> logger)
24+
public DefaultRabbitMQPersistentConnection(IConnectionFactory connectionFactory, ILogger<DefaultRabbitMQPersistentConnection> logger, int retryCount = 5)
2525
{
2626
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
2727
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
28+
_retryCount = retryCount;
2829
}
2930

3031
public bool IsConnected
@@ -69,7 +70,7 @@ public bool TryConnect()
6970
{
7071
var policy = RetryPolicy.Handle<SocketException>()
7172
.Or<BrokerUnreachableException>()
72-
.WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) =>
73+
.WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) =>
7374
{
7475
_logger.LogWarning(ex.ToString());
7576
}
@@ -88,7 +89,7 @@ public bool TryConnect()
8889
_connection.ConnectionBlocked += OnConnectionBlocked;
8990

9091
_logger.LogInformation($"RabbitMQ persistent connection acquired a connection {_connection.Endpoint.HostName} and is subscribed to failure events");
91-
92+
9293
return true;
9394
}
9495
else

src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,20 @@ public class EventBusRabbitMQ : IEventBus, IDisposable
2626
private readonly IEventBusSubscriptionsManager _subsManager;
2727
private readonly ILifetimeScope _autofac;
2828
private readonly string AUTOFAC_SCOPE_NAME = "eshop_event_bus";
29+
private readonly int _retryCount;
2930

3031
private IModel _consumerChannel;
3132
private string _queueName;
3233

3334
public EventBusRabbitMQ(IRabbitMQPersistentConnection persistentConnection, ILogger<EventBusRabbitMQ> logger,
34-
ILifetimeScope autofac, IEventBusSubscriptionsManager subsManager)
35+
ILifetimeScope autofac, IEventBusSubscriptionsManager subsManager, int retryCount = 5)
3536
{
3637
_persistentConnection = persistentConnection ?? throw new ArgumentNullException(nameof(persistentConnection));
3738
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
3839
_subsManager = subsManager ?? new InMemoryEventBusSubscriptionsManager();
3940
_consumerChannel = CreateConsumerChannel();
4041
_autofac = autofac;
41-
42+
_retryCount = retryCount;
4243
_subsManager.OnEventRemoved += SubsManager_OnEventRemoved;
4344
}
4445

@@ -72,7 +73,7 @@ public void Publish(IntegrationEvent @event)
7273

7374
var policy = RetryPolicy.Handle<BrokerUnreachableException>()
7475
.Or<SocketException>()
75-
.WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) =>
76+
.WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) =>
7677
{
7778
_logger.LogWarning(ex.ToString());
7879
});

src/Services/Basket/Basket.API/Startup.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
120120
factory.Password = Configuration["EventBusPassword"];
121121
}
122122

123-
return new DefaultRabbitMQPersistentConnection(factory, logger);
123+
var retryCount = 5;
124+
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
125+
{
126+
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
127+
}
128+
129+
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
124130
});
125131
}
126132

@@ -247,7 +253,21 @@ private void RegisterEventBus(IServiceCollection services)
247253
}
248254
else
249255
{
250-
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
256+
services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
257+
{
258+
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
259+
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
260+
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
261+
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
262+
263+
var retryCount = 5;
264+
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
265+
{
266+
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
267+
}
268+
269+
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, retryCount);
270+
});
251271
}
252272

253273
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();

src/Services/Basket/Basket.API/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
"SubscriptionClientName": "Basket",
1414
"ApplicationInsights": {
1515
"InstrumentationKey": ""
16-
}
16+
},
17+
"EventBusRetryCount": 5
1718
}

src/Services/Catalog/Catalog.API/Startup.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Startup(IConfiguration configuration)
3535
Configuration = configuration;
3636
}
3737

38-
public IConfiguration Configuration { get; }
38+
public IConfiguration Configuration { get; }
3939

4040
public IServiceProvider ConfigureServices(IServiceCollection services)
4141
{
@@ -161,7 +161,13 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
161161
factory.Password = Configuration["EventBusPassword"];
162162
}
163163

164-
return new DefaultRabbitMQPersistentConnection(factory, logger);
164+
var retryCount = 5;
165+
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
166+
{
167+
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
168+
}
169+
170+
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
165171
});
166172
}
167173

@@ -188,7 +194,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
188194
loggerFactory.CreateLogger("init").LogDebug($"Using PATH BASE '{pathBase}'");
189195
app.UsePathBase(pathBase);
190196
}
191-
197+
192198
app.UseCors("CorsPolicy");
193199

194200
app.UseMvcWithDefaultRoute();
@@ -221,7 +227,21 @@ private void RegisterEventBus(IServiceCollection services)
221227
}
222228
else
223229
{
224-
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
230+
services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
231+
{
232+
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
233+
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
234+
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
235+
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
236+
237+
var retryCount = 5;
238+
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
239+
{
240+
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
241+
}
242+
243+
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, retryCount);
244+
});
225245
}
226246

227247
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();

src/Services/Catalog/Catalog.API/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"SubscriptionClientName": "Catalog",
1616
"ApplicationInsights": {
1717
"InstrumentationKey": ""
18-
}
18+
},
19+
"EventBusRetryCount": 5
1920
}

src/Services/Location/Locations.API/Startup.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
8787
factory.Password = Configuration["EventBusPassword"];
8888
}
8989

90-
return new DefaultRabbitMQPersistentConnection(factory, logger);
90+
var retryCount = 5;
91+
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
92+
{
93+
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
94+
}
95+
96+
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
9197
});
9298
}
9399

@@ -217,7 +223,21 @@ private void RegisterEventBus(IServiceCollection services)
217223
}
218224
else
219225
{
220-
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
226+
services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
227+
{
228+
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
229+
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
230+
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
231+
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
232+
233+
var retryCount = 5;
234+
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
235+
{
236+
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
237+
}
238+
239+
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, retryCount);
240+
});
221241
}
222242

223243
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();

src/Services/Location/Locations.API/appsettings.Development.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Services/Location/Locations.API/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
"SubscriptionClientName": "Locations",
1515
"ApplicationInsights": {
1616
"InstrumentationKey": ""
17-
}
17+
},
18+
"EventBusRetryCount": 5
1819
}

src/Services/Marketing/Marketing.API/Startup.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,14 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
122122
{
123123
factory.Password = Configuration["EventBusPassword"];
124124
}
125-
return new DefaultRabbitMQPersistentConnection(factory, logger);
125+
126+
var retryCount = 5;
127+
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
128+
{
129+
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
130+
}
131+
132+
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
126133
});
127134
}
128135

@@ -242,7 +249,21 @@ private void RegisterEventBus(IServiceCollection services)
242249
}
243250
else
244251
{
245-
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
252+
services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
253+
{
254+
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
255+
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
256+
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
257+
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
258+
259+
var retryCount = 5;
260+
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
261+
{
262+
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
263+
}
264+
265+
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, retryCount);
266+
});
246267
}
247268

248269
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();

0 commit comments

Comments
 (0)