Skip to content

Commit 3859ddb

Browse files
committed
Add more info during the reconnection
Signed-off-by: Gabriele Santomaggio <[email protected]>
1 parent 445bca1 commit 3859ddb

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

RabbitMQ.AMQP.Client/IRecoveryConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ public interface IBackOffDelayPolicy
6060
/// or when the user wants to disable the backoff delay policy.
6161
/// </summary>
6262
bool IsActive();
63+
64+
int CurrentAttempt { get; }
6365
}

RabbitMQ.AMQP.Client/Impl/AbstractLifeCycle.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ internal async Task Reconnect()
5757
try
5858
{
5959
int randomWait = Random.Shared.Next(300, 900);
60-
Trace.WriteLine(TraceLevel.Information, $"{ToString()} is reconnecting in {randomWait} ms");
60+
Trace.WriteLine(TraceLevel.Information, $"{ToString()} is reconnecting in {randomWait} ms, " +
61+
$"attempt: {_backOffDelayPolicy.CurrentAttempt}");
6162
await Task.Delay(randomWait).ConfigureAwait(false);
6263
await OpenAsync().ConfigureAwait(false);
63-
Trace.WriteLine(TraceLevel.Information, $"{ToString()} is reconnected");
64+
Trace.WriteLine(TraceLevel.Information,
65+
$"{ToString()} is reconnected, attempt: {_backOffDelayPolicy.CurrentAttempt}");
6466
_backOffDelayPolicy.Reset();
6567
}
6668
catch (Exception e)

RabbitMQ.AMQP.Client/Impl/AmqpConnection.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,12 @@ await Task.Run(async () =>
289289
try
290290
{
291291
int next = _connectionSettings.RecoveryConfiguration.GetBackOffDelayPolicy().Delay();
292+
292293
Trace.WriteLine(TraceLevel.Information,
293-
$"Trying Recovering connection in {next} milliseconds. Info: {ToString()})");
294+
$"Trying Recovering connection in {next} milliseconds, " +
295+
$"attempt: {_connectionSettings.RecoveryConfiguration.GetBackOffDelayPolicy().CurrentAttempt}. " +
296+
$"Info: {ToString()})");
297+
294298
await Task.Delay(TimeSpan.FromMilliseconds(next))
295299
.ConfigureAwait(false);
296300

@@ -342,7 +346,7 @@ await _recordingTopologyListener.Accept(visitor)
342346
Trace.WriteLine(TraceLevel.Error, $"Error trying to reconnect entities {e}. Info: {this}");
343347
}
344348
}).ConfigureAwait(false);
345-
349+
346350
return;
347351
}
348352

RabbitMQ.AMQP.Client/Impl/ConnectionSettings.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ private BackOffDelayPolicy(int maxAttempt)
281281
private const int EndRandomMilliseconds = 1500;
282282

283283
private int _attempt = 1;
284-
private int _totalAttempt = 0;
285284
private readonly int _maxAttempt = 12;
286285

287286

@@ -296,25 +295,27 @@ private void ResetAfterMaxAttempt()
296295
public int Delay()
297296
{
298297
_attempt++;
299-
_totalAttempt++;
298+
CurrentAttempt++;
300299
ResetAfterMaxAttempt();
301300
return Random.Shared.Next(StartRandomMilliseconds, EndRandomMilliseconds) * _attempt;
302301
}
303302

304303
public void Reset()
305304
{
306305
_attempt = 1;
307-
_totalAttempt = 0;
306+
CurrentAttempt = 0;
308307
}
309308

310309
public bool IsActive()
311310
{
312-
return _totalAttempt < _maxAttempt;
311+
return CurrentAttempt < _maxAttempt;
313312
}
314313

314+
public int CurrentAttempt { get; private set; } = 0;
315+
315316

316317
public override string ToString()
317318
{
318-
return $"BackOffDelayPolicy{{ Attempt={_attempt}, TotalAttempt={_totalAttempt}, IsActive={IsActive} }}";
319+
return $"BackOffDelayPolicy{{ Attempt={_attempt}, TotalAttempt={CurrentAttempt}, IsActive={IsActive} }}";
319320
}
320321
}

Tests/ConnectionRecoveryTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public void Reset()
2222
}
2323

2424
public bool IsActive() => false;
25+
public int CurrentAttempt => 1;
2526
}
2627

2728
internal class FakeFastBackOffDelay : IBackOffDelayPolicy
@@ -36,6 +37,7 @@ public void Reset()
3637
}
3738

3839
public bool IsActive() => true;
40+
public int CurrentAttempt => 1;
3941
}
4042

4143
public class ConnectionRecoveryTests(ITestOutputHelper testOutputHelper)

0 commit comments

Comments
 (0)