1
- // Copyright 2020 New Relic, Inc. All rights reserved.
1
+ // Copyright 2020 New Relic, Inc. All rights reserved.
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
using System . Runtime . CompilerServices ;
@@ -14,6 +14,7 @@ public class ConnectWrapperV2 : IWrapper
14
14
public bool IsTransactionRequired => false ;
15
15
16
16
private const string WrapperName = "stackexchangeredis-connect" ;
17
+ private static readonly object _lock = new ( ) ;
17
18
18
19
public CanWrapResponse CanWrap ( InstrumentedMethodInfo methodInfo )
19
20
{
@@ -23,22 +24,26 @@ public CanWrapResponse CanWrap(InstrumentedMethodInfo methodInfo)
23
24
public AfterWrappedMethodDelegate BeforeWrappedMethod ( InstrumentedMethodCall instrumentedMethodCall , IAgent agent , Agent . Api . ITransaction transaction )
24
25
{
25
26
return Delegates . GetDelegateFor < ConnectionMultiplexer > (
26
- onSuccess : muxer =>
27
- {
28
- RegisterProfIler ( muxer ) ;
29
- } ) ;
27
+ onSuccess : RegisterProfiler ) ;
30
28
31
- void RegisterProfIler ( IConnectionMultiplexer multiplexer )
29
+ void RegisterProfiler ( IConnectionMultiplexer multiplexer )
32
30
{
33
31
var xAgent = ( IAgentExperimental ) agent ;
34
32
35
33
// The SessionCache is not connection-specific. This checks for an existing cache and creates one if there is none.
36
- if ( ( ( IAgentExperimental ) agent ) . StackExchangeRedisCache == null )
34
+ if ( xAgent . StackExchangeRedisCache == null )
37
35
{
38
- // We only need the hashcode since nothing will change for the methodCall
39
- var hashCode = RuntimeHelpers . GetHashCode ( multiplexer ) ;
40
- var sessionCache = new SessionCache ( agent , hashCode ) ;
41
- xAgent . StackExchangeRedisCache = sessionCache ;
36
+ lock ( _lock )
37
+ {
38
+ if ( xAgent . StackExchangeRedisCache == null )
39
+ {
40
+ // We only need the hashcode since nothing will change for the methodCall
41
+ var hashCode = RuntimeHelpers . GetHashCode ( multiplexer ) ;
42
+ var sessionCache = new SessionCache ( agent , hashCode ) ;
43
+
44
+ xAgent . StackExchangeRedisCache = sessionCache ;
45
+ }
46
+ }
42
47
}
43
48
44
49
// Registers the profiling function from the shared SessionCache.
0 commit comments