Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ConcurrentDictionary #1580

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace RabbitMQ
#if NETSTANDARD
internal static class DictionaryExtension
{
public static bool Remove<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, out TValue value)
public static bool Remove<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, out TValue value)
{
return dictionary.TryGetValue(key, out value) && dictionary.Remove(key);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

Expand All @@ -7,8 +9,8 @@ namespace RabbitMQ.Client.ConsumerDispatching
#nullable enable
internal abstract class ConsumerDispatcherBase
{
private static readonly FallbackConsumer fallbackConsumer = new FallbackConsumer();
private readonly Dictionary<string, IBasicConsumer> _consumers = new Dictionary<string, IBasicConsumer>();
private static readonly FallbackConsumer s_fallbackConsumer = new FallbackConsumer();
private readonly IDictionary<string, IBasicConsumer> _consumers = new ConcurrentDictionary<string, IBasicConsumer>();

public IBasicConsumer? DefaultConsumer { get; set; }

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

protected void AddConsumer(IBasicConsumer consumer, string tag)
{
lock (_consumers)
{
_consumers[tag] = consumer;
}
_consumers[tag] = consumer;
}

protected IBasicConsumer GetConsumerOrDefault(string tag)
{
lock (_consumers)
{
return _consumers.TryGetValue(tag, out IBasicConsumer? consumer) ? consumer : GetDefaultOrFallbackConsumer();
}
return _consumers.TryGetValue(tag, out IBasicConsumer? consumer) ? consumer : GetDefaultOrFallbackConsumer();
}

public IBasicConsumer GetAndRemoveConsumer(string tag)
{
lock (_consumers)
{
return _consumers.Remove(tag, out IBasicConsumer? consumer) ? consumer : GetDefaultOrFallbackConsumer();
}
return _consumers.Remove(tag, out IBasicConsumer? consumer) ? consumer : GetDefaultOrFallbackConsumer();
}

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

private void DoShutdownConsumers(ShutdownEventArgs reason)
{
lock (_consumers)
foreach (KeyValuePair<string, IBasicConsumer> pair in _consumers.ToArray())
{
foreach (KeyValuePair<string, IBasicConsumer> pair in _consumers)
{
ShutdownConsumer(pair.Value, reason);
}
_consumers.Clear();
ShutdownConsumer(pair.Value, reason);
}
_consumers.Clear();
}

protected abstract void ShutdownConsumer(IBasicConsumer consumer, ShutdownEventArgs reason);
Expand All @@ -74,7 +64,7 @@ private void DoShutdownConsumers(ShutdownEventArgs reason)
[MethodImpl(MethodImplOptions.NoInlining)]
private IBasicConsumer GetDefaultOrFallbackConsumer()
{
return DefaultConsumer ?? fallbackConsumer;
return DefaultConsumer ?? s_fallbackConsumer;
}
}
}