Skip to content

Commit 499957e

Browse files
committed
Remove Lock from DiscoveryProxyCache in benefit of ReaderWriterLockSlim
castleproject#565
1 parent 7628eba commit 499957e

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

src/Castle.Facilities.WcfIntegration/Client/Discovery/DiscoveryProxyCache.cs

+36-13
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ namespace Castle.Facilities.WcfIntegration
1616
{
1717
using System;
1818
using System.ServiceModel.Discovery;
19+
using System.Threading;
20+
1921
using Castle.Core.Internal;
2022

2123
public class DiscoveryProxyCache : DiscoveryEndpointProvider, IDisposable
2224
{
2325
private readonly DiscoveryEndpointProvider inner;
2426
private volatile DiscoveryEndpoint endpoint;
25-
private readonly Lock @lock = Lock.Create();
27+
private readonly ReaderWriterLockSlim @lock = new ReaderWriterLockSlim();
2628

2729
public DiscoveryProxyCache(DiscoveryEndpointProvider inner)
2830
{
@@ -36,36 +38,57 @@ public DiscoveryProxyCache(DiscoveryEndpointProvider inner)
3638

3739
public override DiscoveryEndpoint GetDiscoveryEndpoint()
3840
{
39-
using (var locker = @lock.ForReadingUpgradeable())
41+
@lock.EnterUpgradeableReadLock();
42+
try
4043
{
4144
if (endpoint != null)
4245
return endpoint;
46+
@lock.EnterWriteLock();
47+
try
48+
{
49+
if (endpoint == null)
50+
endpoint = inner.GetDiscoveryEndpoint();
4351

44-
locker.Upgrade();
45-
46-
if (endpoint == null)
47-
endpoint = inner.GetDiscoveryEndpoint();
48-
49-
return endpoint;
52+
return endpoint;
53+
}
54+
finally
55+
{
56+
@lock.ExitWriteLock();
57+
}
58+
}
59+
finally
60+
{
61+
@lock.ExitUpgradeableReadLock();
5062
}
5163
}
5264

5365
private void DiscoveryEndpointFaulted(object sender, DiscoveryEndpointFaultEventArgs args)
5466
{
55-
using (var locker = @lock.ForReadingUpgradeable())
67+
@lock.EnterUpgradeableReadLock();
68+
try
5669
{
5770
if (args.Culprit != endpoint)
5871
return;
59-
60-
locker.Upgrade();
61-
62-
endpoint = null;
72+
@lock.EnterWriteLock();
73+
try
74+
{
75+
endpoint = null;
76+
}
77+
finally
78+
{
79+
@lock.ExitWriteLock();
80+
}
81+
}
82+
finally
83+
{
84+
@lock.ExitUpgradeableReadLock();
6385
}
6486
}
6587

6688
void IDisposable.Dispose()
6789
{
6890
AbstractChannelBuilder.DiscoveryEndpointFaulted -= DiscoveryEndpointFaulted;
91+
@lock.Dispose();
6992
}
7093
}
7194
}

0 commit comments

Comments
 (0)