Skip to content

Commit 43bb0df

Browse files
committed
Wrapped concurrentqueue + semaphore pair together for readability, and unblocked recovery main loop during retry delay.
1 parent 384b1c3 commit 43bb0df

File tree

2 files changed

+116
-33
lines changed

2 files changed

+116
-33
lines changed

projects/client/RabbitMQ.Client/src/client/impl/AutorecoveringConnection.cs

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
using RabbitMQ.Client.Events;
4949
using RabbitMQ.Client.Impl;
50+
using RabbitMQ.Util;
5051

5152
namespace RabbitMQ.Client.Framing.Impl
5253
{
@@ -477,7 +478,6 @@ private void Init(IFrameHandler fh)
477478
if (ShouldTriggerConnectionRecovery(args))
478479
{
479480
_recoveryLoopCommandQueue.Enqueue(RecoveryCommand.BeginAutomaticRecovery);
480-
_semaphore.Release();
481481
}
482482
};
483483
lock (_eventLock)
@@ -961,8 +961,7 @@ private enum RecoveryConnectionState
961961
private Task _recoveryTask;
962962
private RecoveryConnectionState _recoveryLoopState = RecoveryConnectionState.Connected;
963963

964-
private readonly ConcurrentQueue<RecoveryCommand> _recoveryLoopCommandQueue = new ConcurrentQueue<RecoveryCommand>();
965-
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);
964+
private readonly AsyncConcurrentQueue<RecoveryCommand> _recoveryLoopCommandQueue = new AsyncConcurrentQueue<RecoveryCommand>();
966965
private readonly CancellationTokenSource _recoveryCancellationToken = new CancellationTokenSource();
967966
private readonly TaskCompletionSource<int> _recoveryLoopComplete = new TaskCompletionSource<int>();
968967

@@ -975,29 +974,19 @@ private async Task MainRecoveryLoop()
975974
{
976975
while (!_recoveryCancellationToken.IsCancellationRequested)
977976
{
978-
try
979-
{
980-
await _semaphore.WaitAsync(_recoveryCancellationToken.Token).ConfigureAwait(false);
981-
}
982-
catch (TaskCanceledException)
983-
{
984-
// Swallowing the task cancellation in case we are stopping work.
985-
}
986-
987-
if (!_recoveryCancellationToken.IsCancellationRequested && _recoveryLoopCommandQueue.TryDequeue(out RecoveryCommand command))
977+
var command = await _recoveryLoopCommandQueue.DequeueAsync(_recoveryCancellationToken.Token).ConfigureAwait(false);
978+
979+
switch (_recoveryLoopState)
988980
{
989-
switch (_recoveryLoopState)
990-
{
991-
case RecoveryConnectionState.Connected:
992-
await RecoveryLoopConnectedHandler(command).ConfigureAwait(false);
993-
break;
994-
case RecoveryConnectionState.Recovering:
995-
await RecoveryLoopRecoveringHandler(command).ConfigureAwait(false);
996-
break;
997-
default:
998-
ESLog.Warn("RecoveryLoop state is out of range.");
999-
break;
1000-
}
981+
case RecoveryConnectionState.Connected:
982+
RecoveryLoopConnectedHandler(command);
983+
break;
984+
case RecoveryConnectionState.Recovering:
985+
RecoveryLoopRecoveringHandler(command);
986+
break;
987+
default:
988+
ESLog.Warn("RecoveryLoop state is out of range.");
989+
break;
1001990
}
1002991
}
1003992
}
@@ -1030,7 +1019,7 @@ private void StopRecoveryLoop()
10301019
/// Handles commands when in the Recovering state.
10311020
/// </summary>
10321021
/// <param name="command"></param>
1033-
private async Task RecoveryLoopRecoveringHandler(RecoveryCommand command)
1022+
private void RecoveryLoopRecoveringHandler(RecoveryCommand command)
10341023
{
10351024
switch (command)
10361025
{
@@ -1044,9 +1033,7 @@ private async Task RecoveryLoopRecoveringHandler(RecoveryCommand command)
10441033
}
10451034
else
10461035
{
1047-
await Task.Delay(_factory.NetworkRecoveryInterval);
1048-
_recoveryLoopCommandQueue.Enqueue(RecoveryCommand.PerformAutomaticRecovery);
1049-
_semaphore.Release();
1036+
ScheduleRecoveryRetry();
10501037
}
10511038

10521039
break;
@@ -1060,7 +1047,7 @@ private async Task RecoveryLoopRecoveringHandler(RecoveryCommand command)
10601047
/// Handles commands when in the Connected state.
10611048
/// </summary>
10621049
/// <param name="command"></param>
1063-
private async Task RecoveryLoopConnectedHandler(RecoveryCommand command)
1050+
private void RecoveryLoopConnectedHandler(RecoveryCommand command)
10641051
{
10651052
switch (command)
10661053
{
@@ -1069,14 +1056,24 @@ private async Task RecoveryLoopConnectedHandler(RecoveryCommand command)
10691056
break;
10701057
case RecoveryCommand.BeginAutomaticRecovery:
10711058
_recoveryLoopState = RecoveryConnectionState.Recovering;
1072-
await Task.Delay(_factory.NetworkRecoveryInterval).ConfigureAwait(false);
1073-
_recoveryLoopCommandQueue.Enqueue(RecoveryCommand.PerformAutomaticRecovery);
1074-
_semaphore.Release();
1059+
ScheduleRecoveryRetry();
10751060
break;
10761061
default:
10771062
ESLog.Warn($"RecoveryLoop command {command} is out of range.");
10781063
break;
10791064
}
10801065
}
1066+
1067+
/// <summary>
1068+
/// Schedule a background Task to signal the command queue when the retry duration has elapsed.
1069+
/// </summary>
1070+
private void ScheduleRecoveryRetry()
1071+
{
1072+
Task.Delay(_factory.NetworkRecoveryInterval)
1073+
.ContinueWith(t =>
1074+
{
1075+
_recoveryLoopCommandQueue.Enqueue(RecoveryCommand.PerformAutomaticRecovery);
1076+
});
1077+
}
10811078
}
10821079
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// This source code is dual-licensed under the Apache License, version
2+
// 2.0, and the Mozilla Public License, version 1.1.
3+
//
4+
// The APL v2.0:
5+
//
6+
//---------------------------------------------------------------------------
7+
// Copyright (c) 2007-2020 VMware, Inc.
8+
//
9+
// Licensed under the Apache License, Version 2.0 (the "License");
10+
// you may not use this file except in compliance with the License.
11+
// You may obtain a copy of the License at
12+
//
13+
// https://www.apache.org/licenses/LICENSE-2.0
14+
//
15+
// Unless required by applicable law or agreed to in writing, software
16+
// distributed under the License is distributed on an "AS IS" BASIS,
17+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
// See the License for the specific language governing permissions and
19+
// limitations under the License.
20+
//---------------------------------------------------------------------------
21+
//
22+
// The MPL v1.1:
23+
//
24+
//---------------------------------------------------------------------------
25+
// The contents of this file are subject to the Mozilla Public License
26+
// Version 1.1 (the "License"); you may not use this file except in
27+
// compliance with the License. You may obtain a copy of the License
28+
// at https://www.mozilla.org/MPL/
29+
//
30+
// Software distributed under the License is distributed on an "AS IS"
31+
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
32+
// the License for the specific language governing rights and
33+
// limitations under the License.
34+
//
35+
// The Original Code is RabbitMQ.
36+
//
37+
// The Initial Developer of the Original Code is Pivotal Software, Inc.
38+
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
39+
//---------------------------------------------------------------------------
40+
41+
using System;
42+
using System.Collections.Concurrent;
43+
using System.Threading;
44+
using System.Threading.Tasks;
45+
46+
namespace RabbitMQ.Util
47+
{
48+
/// <summary>
49+
/// A concurrent queue where Dequeue waits for something to be inserted if the queue is empty and Enqueue signals
50+
/// that something has been added. Similar in function to a BlockingCollection but with async/await
51+
/// support.
52+
/// </summary>
53+
/// <typeparam name="T"></typeparam>
54+
internal class AsyncConcurrentQueue<T>
55+
{
56+
private readonly ConcurrentQueue<T> _internalQueue = new ConcurrentQueue<T>();
57+
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);
58+
59+
/// <summary>
60+
/// Returns a Task that is completed when an object can be returned from the beginning of the queue.
61+
/// </summary>
62+
/// <param name="token"></param>
63+
/// <returns></returns>
64+
public async Task<T> DequeueAsync(CancellationToken token = default)
65+
{
66+
await _semaphore.WaitAsync(token).ConfigureAwait(false);
67+
68+
if (!_internalQueue.TryDequeue(out var command))
69+
{
70+
throw new InvalidOperationException("Internal queue empty despite signaled enqueue.");
71+
}
72+
73+
return command;
74+
}
75+
76+
/// <summary>
77+
/// Add an object to the end of the queue.
78+
/// </summary>
79+
/// <param name="item"></param>
80+
public void Enqueue(T item)
81+
{
82+
_internalQueue.Enqueue(item);
83+
_semaphore.Release();
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)