Skip to content

Commit 5da0720

Browse files
authored
Merge pull request #1580 from rabbitmq/lukebakken/use-concurrent-dict
Use ConcurrentDictionary
2 parents bf89475 + e132c9b commit 5da0720

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

projects/RabbitMQ.Client/FrameworkExtension/DictionaryExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace RabbitMQ
3737
#if NETSTANDARD
3838
internal static class DictionaryExtension
3939
{
40-
public static bool Remove<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, out TValue value)
40+
public static bool Remove<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, out TValue value)
4141
{
4242
return dictionary.TryGetValue(key, out value) && dictionary.Remove(key);
4343
}
Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Concurrent;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Runtime.CompilerServices;
35
using System.Threading.Tasks;
46

@@ -7,8 +9,8 @@ namespace RabbitMQ.Client.ConsumerDispatching
79
#nullable enable
810
internal abstract class ConsumerDispatcherBase
911
{
10-
private static readonly FallbackConsumer fallbackConsumer = new FallbackConsumer();
11-
private readonly Dictionary<string, IBasicConsumer> _consumers = new Dictionary<string, IBasicConsumer>();
12+
private static readonly FallbackConsumer s_fallbackConsumer = new FallbackConsumer();
13+
private readonly IDictionary<string, IBasicConsumer> _consumers = new ConcurrentDictionary<string, IBasicConsumer>();
1214

1315
public IBasicConsumer? DefaultConsumer { get; set; }
1416

@@ -18,26 +20,17 @@ protected ConsumerDispatcherBase()
1820

1921
protected void AddConsumer(IBasicConsumer consumer, string tag)
2022
{
21-
lock (_consumers)
22-
{
23-
_consumers[tag] = consumer;
24-
}
23+
_consumers[tag] = consumer;
2524
}
2625

2726
protected IBasicConsumer GetConsumerOrDefault(string tag)
2827
{
29-
lock (_consumers)
30-
{
31-
return _consumers.TryGetValue(tag, out IBasicConsumer? consumer) ? consumer : GetDefaultOrFallbackConsumer();
32-
}
28+
return _consumers.TryGetValue(tag, out IBasicConsumer? consumer) ? consumer : GetDefaultOrFallbackConsumer();
3329
}
3430

3531
public IBasicConsumer GetAndRemoveConsumer(string tag)
3632
{
37-
lock (_consumers)
38-
{
39-
return _consumers.Remove(tag, out IBasicConsumer? consumer) ? consumer : GetDefaultOrFallbackConsumer();
40-
}
33+
return _consumers.Remove(tag, out IBasicConsumer? consumer) ? consumer : GetDefaultOrFallbackConsumer();
4134
}
4235

4336
public void Shutdown(ShutdownEventArgs reason)
@@ -54,14 +47,11 @@ public Task ShutdownAsync(ShutdownEventArgs reason)
5447

5548
private void DoShutdownConsumers(ShutdownEventArgs reason)
5649
{
57-
lock (_consumers)
50+
foreach (KeyValuePair<string, IBasicConsumer> pair in _consumers.ToArray())
5851
{
59-
foreach (KeyValuePair<string, IBasicConsumer> pair in _consumers)
60-
{
61-
ShutdownConsumer(pair.Value, reason);
62-
}
63-
_consumers.Clear();
52+
ShutdownConsumer(pair.Value, reason);
6453
}
54+
_consumers.Clear();
6555
}
6656

6757
protected abstract void ShutdownConsumer(IBasicConsumer consumer, ShutdownEventArgs reason);
@@ -74,7 +64,7 @@ private void DoShutdownConsumers(ShutdownEventArgs reason)
7464
[MethodImpl(MethodImplOptions.NoInlining)]
7565
private IBasicConsumer GetDefaultOrFallbackConsumer()
7666
{
77-
return DefaultConsumer ?? fallbackConsumer;
67+
return DefaultConsumer ?? s_fallbackConsumer;
7868
}
7969
}
8070
}

0 commit comments

Comments
 (0)