Skip to content

Commit 2b85e7e

Browse files
committed
Refactor other use of ConcurrentDictionary GetOrAdd
1 parent f821abb commit 2b85e7e

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

projects/RabbitMQ.Client/client/impl/AsyncConsumerWorkService.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,19 @@ namespace RabbitMQ.Client.Impl
88
{
99
internal sealed class AsyncConsumerWorkService : ConsumerWorkService
1010
{
11-
private readonly ConcurrentDictionary<IModel, WorkPool> _workPools;
12-
private readonly Func<IModel, WorkPool> _startNewWorkPoolFunc;
13-
14-
public AsyncConsumerWorkService()
15-
{
16-
_workPools = new ConcurrentDictionary<IModel, WorkPool>();
17-
_startNewWorkPoolFunc = model => StartNewWorkPool(model);
18-
}
11+
private readonly ConcurrentDictionary<IModel, WorkPool> _workPools = new ConcurrentDictionary<IModel, WorkPool>();
12+
private readonly Func<IModel, WorkPool> _startNewWorkPoolFunc = model => StartNewWorkPool(model);
1913

2014
public void Schedule<TWork>(ModelBase model, TWork work) where TWork : Work
2115
{
2216
/*
2317
* rabbitmq/rabbitmq-dotnet-client#841
2418
* https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2.getoradd
25-
*
26-
* The lock is necessary because calling the value delegate is not atomic.
19+
* Note that the value delegate is not atomic but instances of this class are not meant to be used by
20+
* multiple threads.
2721
*/
28-
lock (_workPools)
29-
{
30-
_workPools.GetOrAdd(model, _startNewWorkPoolFunc).Enqueue(work);
31-
}
22+
WorkPool workPool = _workPools.GetOrAdd(model, _startNewWorkPoolFunc);
23+
workPool.Enqueue(work);
3224
}
3325

3426
private static WorkPool StartNewWorkPool(IModel model)

projects/RabbitMQ.Client/client/impl/ConsumerWorkService.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@
55

66
namespace RabbitMQ.Client.Impl
77
{
8-
class ConsumerWorkService
8+
internal class ConsumerWorkService
99
{
1010
private readonly ConcurrentDictionary<IModel, WorkPool> _workPools = new ConcurrentDictionary<IModel, WorkPool>();
11+
private readonly Func<IModel, WorkPool> _startNewWorkPoolFunc = model => StartNewWorkPool(model);
1112

1213
public void AddWork(IModel model, Action fn)
1314
{
14-
_workPools.GetOrAdd(model, StartNewWorkPool).Enqueue(fn);
15+
/*
16+
* rabbitmq/rabbitmq-dotnet-client#841
17+
* https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2.getoradd
18+
* Note that the value delegate is not atomic but instances of this class are not meant to be used by
19+
* multiple threads.
20+
*/
21+
WorkPool workPool = _workPools.GetOrAdd(model, _startNewWorkPoolFunc);
22+
workPool.Enqueue(fn);
1523
}
1624

17-
private WorkPool StartNewWorkPool(IModel model)
25+
private static WorkPool StartNewWorkPool(IModel model)
1826
{
1927
var newWorkPool = new WorkPool();
2028
newWorkPool.Start();

0 commit comments

Comments
 (0)