Skip to content

Commit f335e2d

Browse files
Merge pull request #289 from rabbitmq/rabbitmq-dotnet-client-288
Use concurrent collections inside AutorecoveringConnection
2 parents 9674068 + 90a20d0 commit f335e2d

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

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

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
using RabbitMQ.Client.Impl;
4444
using System;
4545
using System.Collections.Generic;
46+
using System.Collections.Concurrent;
4647
using System.Linq;
47-
using System.Net;
4848
using System.Threading;
4949
using System.Threading.Tasks;
5050

@@ -73,19 +73,20 @@ public class AutorecoveringConnection : IConnection, IRecoverable
7373

7474
protected List<AutorecoveringModel> m_models = new List<AutorecoveringModel>();
7575

76-
protected HashSet<RecordedBinding> m_recordedBindings = new HashSet<RecordedBinding>();
76+
protected IDictionary<RecordedBinding, byte> m_recordedBindings =
77+
new ConcurrentDictionary<RecordedBinding, byte>();
7778

7879
protected List<EventHandler<ConnectionBlockedEventArgs>> m_recordedBlockedEventHandlers =
7980
new List<EventHandler<ConnectionBlockedEventArgs>>();
8081

8182
protected IDictionary<string, RecordedConsumer> m_recordedConsumers =
82-
new Dictionary<string, RecordedConsumer>();
83+
new ConcurrentDictionary<string, RecordedConsumer>();
8384

8485
protected IDictionary<string, RecordedExchange> m_recordedExchanges =
85-
new Dictionary<string, RecordedExchange>();
86+
new ConcurrentDictionary<string, RecordedExchange>();
8687

8788
protected IDictionary<string, RecordedQueue> m_recordedQueues =
88-
new Dictionary<string, RecordedQueue>();
89+
new ConcurrentDictionary<string, RecordedQueue>();
8990

9091
protected List<EventHandler<ShutdownEventArgs>> m_recordedShutdownEventHandlers =
9192
new List<EventHandler<ShutdownEventArgs>>();
@@ -398,7 +399,7 @@ public void DeleteRecordedBinding(RecordedBinding rb)
398399
{
399400
lock (m_recordedEntitiesLock)
400401
{
401-
m_recordedBindings.RemoveWhere(b => b.Equals(rb));
402+
m_recordedBindings.Remove(rb);
402403
}
403404
}
404405

@@ -425,11 +426,10 @@ public void DeleteRecordedExchange(string name)
425426

426427
// find bindings that need removal, check if some auto-delete exchanges
427428
// might need the same
428-
List<RecordedBinding> bs = m_recordedBindings.Where(b => name.Equals(b.Destination)).
429-
ToList();
430-
m_recordedBindings.RemoveWhere(b => name.Equals(b.Destination));
429+
var bs = m_recordedBindings.Keys.Where(b => name.Equals(b.Destination));
431430
foreach (RecordedBinding b in bs)
432431
{
432+
m_recordedBindings.Remove(b);
433433
MaybeDeleteRecordedAutoDeleteExchange(b.Source);
434434
}
435435
}
@@ -442,11 +442,10 @@ public void DeleteRecordedQueue(string name)
442442
m_recordedQueues.Remove(name);
443443
// find bindings that need removal, check if some auto-delete exchanges
444444
// might need the same
445-
List<RecordedBinding> bs = m_recordedBindings.Where(b => name.Equals(b.Destination)).
446-
ToList();
447-
m_recordedBindings.RemoveWhere(b => name.Equals(b.Destination));
445+
var bs = m_recordedBindings.Keys.Where(b => name.Equals(b.Destination));
448446
foreach (RecordedBinding b in bs)
449447
{
448+
m_recordedBindings.Remove(b);
450449
MaybeDeleteRecordedAutoDeleteExchange(b.Source);
451450
}
452451
}
@@ -468,7 +467,7 @@ public void MaybeDeleteRecordedAutoDeleteExchange(string exchange)
468467
{
469468
lock (m_recordedEntitiesLock)
470469
{
471-
if (!HasMoreDestinationsBoundToExchange(m_recordedBindings, exchange))
470+
if (!HasMoreDestinationsBoundToExchange(m_recordedBindings.Keys, exchange))
472471
{
473472
RecordedExchange rx;
474473
m_recordedExchanges.TryGetValue(exchange, out rx);
@@ -505,7 +504,7 @@ public void RecordBinding(RecordedBinding rb)
505504
{
506505
lock (m_recordedEntitiesLock)
507506
{
508-
m_recordedBindings.Add(rb);
507+
m_recordedBindings.Add(rb, 0);
509508
}
510509
}
511510

@@ -752,8 +751,7 @@ protected void PropagateQueueNameChangeToBindings(string oldName, string newName
752751
{
753752
lock (m_recordedBindings)
754753
{
755-
IEnumerable<RecordedBinding> bs = m_recordedBindings.
756-
Where(b => b.Destination.Equals(oldName));
754+
var bs = m_recordedBindings.Keys.Where(b => b.Destination.Equals(oldName));
757755
foreach (RecordedBinding b in bs)
758756
{
759757
b.Destination = newName;
@@ -776,7 +774,7 @@ protected void PropagateQueueNameChangeToConsumers(string oldName, string newNam
776774

777775
protected void RecoverBindings()
778776
{
779-
foreach (RecordedBinding b in m_recordedBindings)
777+
foreach (var b in m_recordedBindings.Keys)
780778
{
781779
try
782780
{
@@ -850,8 +848,7 @@ protected void RecoverConnectionUnblockedHandlers()
850848

851849
protected void RecoverConsumers()
852850
{
853-
var dict = new Dictionary<string, RecordedConsumer>(m_recordedConsumers);
854-
foreach (KeyValuePair<string, RecordedConsumer> pair in dict)
851+
foreach (KeyValuePair<string, RecordedConsumer> pair in m_recordedConsumers)
855852
{
856853
string tag = pair.Key;
857854
RecordedConsumer cons = pair.Value;
@@ -938,8 +935,7 @@ protected void RecoverQueues()
938935
{
939936
lock (m_recordedQueues)
940937
{
941-
var rqs = new Dictionary<string, RecordedQueue>(m_recordedQueues);
942-
foreach (KeyValuePair<string, RecordedQueue> pair in rqs)
938+
foreach (KeyValuePair<string, RecordedQueue> pair in m_recordedQueues)
943939
{
944940
string oldName = pair.Key;
945941
RecordedQueue rq = pair.Value;

0 commit comments

Comments
 (0)