Skip to content

Commit 6a5d3be

Browse files
authored
ServiceBinder may end up being used concurrently; make sure the internal data supports this (#216)
* ServiceBinder may end up being used concurrently; make sure the internal data supports this fix #202 * make the interface map static
1 parent 5148278 commit 6a5d3be

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/protobuf-net.Grpc/Configuration/ServiceBinder.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System;
1+
using ProtoBuf.Grpc.Internal;
2+
using System;
3+
using System.Collections.Concurrent;
24
using System.Collections.Generic;
35
using System.Reflection;
4-
using ProtoBuf.Grpc.Internal;
56

67
namespace ProtoBuf.Grpc.Configuration
78
{
@@ -19,13 +20,14 @@ public class ServiceBinder
1920
/// </summary>
2021
protected ServiceBinder() { }
2122

22-
private readonly Dictionary<Type, InterfaceMapping> _map = new Dictionary<Type, InterfaceMapping>();
23-
private InterfaceMapping GetMap(Type contractType, Type serviceType)
23+
private static readonly ConcurrentDictionary<Type, InterfaceMapping> s_map = new ConcurrentDictionary<Type, InterfaceMapping>();
24+
private static InterfaceMapping GetMap(Type contractType, Type serviceType)
2425
{
25-
if (!_map.TryGetValue(contractType, out var interfaceMapping))
26-
{
26+
if (!s_map.TryGetValue(contractType, out var interfaceMapping))
27+
{ // note: it doesn't matter if this ends up getting called more than once
28+
// in a race condition - we don't need to block etc (the result will be compatible)
2729
interfaceMapping = serviceType.GetInterfaceMap(contractType);
28-
_map[contractType] = interfaceMapping;
30+
s_map[contractType] = interfaceMapping;
2931
}
3032
return interfaceMapping;
3133
}

0 commit comments

Comments
 (0)