Skip to content

Commit 6cee915

Browse files
committed
Fix connection recovery logs
1 parent 5919a44 commit 6cee915

2 files changed

Lines changed: 21 additions & 25 deletions

File tree

src/ArtemisNetClient/AutoRecovering/AutoRecoveringConnection.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,11 @@ private AsyncRetryPolicy<IConnection> CreateConnectionRetryPolicy(IRecoveryPolic
6666
}, (result, _, context) =>
6767
{
6868
var retryCount = context.GetRetryCount();
69-
var endpoint = GetCurrentEndpoint(context);
69+
var endpoint =context.GetEndpoint();
7070
if (result.Exception != null)
7171
{
7272
Log.FailedToEstablishConnection(_logger, endpoint, retryCount, result.Exception);
7373
}
74-
else
75-
{
76-
Log.ConnectionEstablished(_logger, endpoint, retryCount);
77-
}
7874
});
7975
}
8076

@@ -168,11 +164,20 @@ private Task<IConnection> CreateConnection(CancellationToken cancellationToken)
168164
{
169165
var ctx = new Context();
170166
ctx.SetRetryCount(0);
171-
return _connectionRetryPolicy.ExecuteAsync((context, ct) =>
167+
return _connectionRetryPolicy.ExecuteAsync(async (context, ct) =>
172168
{
173-
var endpoint = GetCurrentEndpoint(context);
169+
int retryCount = context.GetRetryCount();
170+
var endpoint = GetNextEndpoint(retryCount);
171+
context.SetEndpoint(endpoint);
174172
var connectionBuilder = new ConnectionBuilder(_loggerFactory, _messageIdPolicyFactory);
175-
return connectionBuilder.CreateAsync(endpoint, ct);
173+
var connection = await connectionBuilder.CreateAsync(endpoint, ct);
174+
175+
if (retryCount > 0)
176+
{
177+
Log.ConnectionEstablished(_logger, endpoint, retryCount);
178+
}
179+
180+
return connection;
176181
}, ctx, cancellationToken);
177182
}
178183

@@ -248,9 +253,8 @@ private async Task DisposeInnerConnection()
248253
}
249254
}
250255

251-
private Endpoint GetCurrentEndpoint(Context context)
256+
private Endpoint GetNextEndpoint(int retryCount)
252257
{
253-
int retryCount = context.GetRetryCount();
254258
return _endpoints[retryCount % _endpoints.Length];
255259
}
256260

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using System;
2-
using Polly;
1+
using Polly;
32

43
namespace ActiveMQ.Artemis.Client
54
{
65
internal static class PollyContextExtensions
76
{
87
private static string _retryCountKey = "retryCount";
9-
private static string _reconnectDelayKey = "reconnectDelay";
8+
private static string _endpointKey = "endpoint";
109

1110
public static void SetRetryCount(this Context context, int retryCount)
1211
{
@@ -17,22 +16,15 @@ public static int GetRetryCount(this Context context)
1716
{
1817
return (int) context[_retryCountKey];
1918
}
20-
21-
public static TimeSpan GetReconnectDelay(this Context context, TimeSpan initialReconnectDelay)
19+
20+
public static void SetEndpoint(this Context context, Endpoint endpoint)
2221
{
23-
if (context.TryGetValue(_reconnectDelayKey, out var reconnectDelay))
24-
{
25-
return (TimeSpan) reconnectDelay;
26-
}
27-
else
28-
{
29-
return initialReconnectDelay;
30-
}
22+
context[_endpointKey] = endpoint;
3123
}
3224

33-
public static void SetReconnectDelay(this Context context, TimeSpan reconnectDelay)
25+
public static Endpoint GetEndpoint(this Context context)
3426
{
35-
context[_reconnectDelayKey] = reconnectDelay;
27+
return (Endpoint) context[_endpointKey];
3628
}
3729
}
3830
}

0 commit comments

Comments
 (0)