Skip to content

Commit 4c58542

Browse files
committed
* Hold the _channelsSemaphore for a shorter period of time.
1 parent 96fdab7 commit 4c58542

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ internal sealed partial class AutorecoveringConnection
4646
private readonly Dictionary<string, RecordedQueue> _recordedQueues = new Dictionary<string, RecordedQueue>();
4747
private readonly HashSet<RecordedBinding> _recordedBindings = new HashSet<RecordedBinding>();
4848
private readonly Dictionary<string, RecordedConsumer> _recordedConsumers = new Dictionary<string, RecordedConsumer>();
49-
private List<AutorecoveringChannel> _channels = new List<AutorecoveringChannel>();
49+
private readonly List<AutorecoveringChannel> _channels = new List<AutorecoveringChannel>();
5050

5151
internal int RecordedExchangesCount => _recordedExchanges.Count;
5252

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -584,28 +584,43 @@ private async ValueTask RecoverChannelsAndItsConsumersAsync(bool recordedEntitie
584584
throw new InvalidOperationException("recordedEntitiesSemaphore must be held");
585585
}
586586

587-
var recoveredChannels = new List<AutorecoveringChannel>();
587+
var channelsToRecover = new List<AutorecoveringChannel>();
588588
await _channelsSemaphore.WaitAsync(cancellationToken)
589589
.ConfigureAwait(false);
590590
try
591591
{
592-
foreach (AutorecoveringChannel channel in _channels)
592+
channelsToRecover.AddRange(_channels);
593+
}
594+
finally
595+
{
596+
_channelsSemaphore.Release();
597+
}
598+
599+
var notRecoveredChannels = new List<AutorecoveringChannel>();
600+
foreach (AutorecoveringChannel channel in channelsToRecover)
601+
{
602+
bool recovered = await channel.AutomaticallyRecoverAsync(this, _config.TopologyRecoveryEnabled,
603+
recordedEntitiesSemaphoreHeld: recordedEntitiesSemaphoreHeld,
604+
cancellationToken: cancellationToken)
605+
.ConfigureAwait(false);
606+
607+
if (false == recovered)
593608
{
594-
bool recovered = await channel.AutomaticallyRecoverAsync(this, _config.TopologyRecoveryEnabled,
595-
recordedEntitiesSemaphoreHeld: recordedEntitiesSemaphoreHeld,
596-
cancellationToken: cancellationToken)
597-
.ConfigureAwait(false);
609+
notRecoveredChannels.Add(channel);
610+
}
611+
}
598612

599-
if (recovered)
600-
{
601-
recoveredChannels.Add(channel);
602-
}
613+
await _channelsSemaphore.WaitAsync(cancellationToken)
614+
.ConfigureAwait(false);
615+
try
616+
{
617+
foreach (AutorecoveringChannel channel in notRecoveredChannels)
618+
{
619+
_channels.Remove(channel);
603620
}
604621
}
605622
finally
606623
{
607-
_channels.Clear();
608-
_channels = recoveredChannels;
609624
_channelsSemaphore.Release();
610625
}
611626
}

0 commit comments

Comments
 (0)