mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Implemented locking using ReaderWriterLockSlim and simplified logging statements.
This commit is contained in:
@@ -6,7 +6,7 @@ using Orchard.Logging;
|
|||||||
|
|
||||||
namespace Orchard.Tasks.Locking {
|
namespace Orchard.Tasks.Locking {
|
||||||
public class DistributedLockService : IDistributedLockService {
|
public class DistributedLockService : IDistributedLockService {
|
||||||
private static readonly object _semaphore = new object();
|
private readonly ReaderWriterLockSlim _rwl = new ReaderWriterLockSlim();
|
||||||
private readonly Work<IDistributedLock> _lock;
|
private readonly Work<IDistributedLock> _lock;
|
||||||
private readonly IMachineNameProvider _machineNameProvider;
|
private readonly IMachineNameProvider _machineNameProvider;
|
||||||
|
|
||||||
@@ -22,7 +22,7 @@ namespace Orchard.Tasks.Locking {
|
|||||||
var machineName = _machineNameProvider.GetMachineName();
|
var machineName = _machineNameProvider.GetMachineName();
|
||||||
@lock = _lock.Value;
|
@lock = _lock.Value;
|
||||||
|
|
||||||
if (Monitor.TryEnter(_semaphore)) {
|
if (_rwl.TryEnterWriteLock(0)) {
|
||||||
try {
|
try {
|
||||||
var waitedTime = TimeSpan.Zero;
|
var waitedTime = TimeSpan.Zero;
|
||||||
var waitTime = TimeSpan.FromMilliseconds(timeout.TotalMilliseconds / 10);
|
var waitTime = TimeSpan.FromMilliseconds(timeout.TotalMilliseconds / 10);
|
||||||
@@ -35,20 +35,20 @@ namespace Orchard.Tasks.Locking {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (acquired) {
|
if (acquired) {
|
||||||
Logger.Debug(String.Format("Successfully acquired a lock named {0} on machine {1}.", name, machineName));
|
Logger.Debug("Successfully acquired a lock named {0} on machine {1}.", name, machineName);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
Logger.Error(ex, "Error during acquire lock.");
|
Logger.Error(ex, "Error while trying to acquire a lock named {0} on machine {1}.", name, machineName);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
Monitor.Exit(_semaphore);
|
_rwl.ExitWriteLock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.Debug(String.Format("Failed to acquire a lock named {0} on machine {1}.", name, machineName));
|
Logger.Debug("Could not acquire a lock named {0} on machine {1}.", name, machineName);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user