Exemplo n.º 1
0
 /// <summary>
 /// Creates a new instance of the DistributedLock class.
 /// </summary>
 /// <param name="store">The object store where the object exists.</param>
 /// <param name="staleLockMultiplier">A multiplier used to determine if a previously locked object is stale. Setting this value too short will result in one lock overwriting another.</param>
 public DistributedLock(ISharpLockDataStore <TLockableObject, TId> store, int staleLockMultiplier = 10)
 {
     _store               = store ?? throw new ArgumentNullException(nameof(store));
     _sharpLockLogger     = _store.GetLogger();
     LockTime             = _store.GetLockTime();
     _staleLockMultiplier = staleLockMultiplier;
 }
Exemplo n.º 2
0
 internal DistributedLock(ILogger logger, ISharpLockDataStore <TBaseObject, TLockableObject, TId> store, int staleLockMultiplier)
 {
     _store               = store ?? throw new ArgumentNullException(nameof(store));
     _logger              = logger;
     LockTime             = _store.GetLockTime();
     _staleLockMultiplier = staleLockMultiplier;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Asynchronously acquire a lock on the specified TLockableObject.
        /// This will wait <see cref="ISharpLockDataStore{TLockableObject,TId}.GetLockTime"/> * <see cref="_staleLockMultiplier"/> to acquire the lock.
        /// To specified a timeout, use the appropriate overload.
        /// </summary>
        /// <param name="obj">The object to take a lock on.</param>
        /// <param name="throwOnFailure">Throw an exception if the acquisition fails.</param>
        /// <returns>A <see cref="bool"/> indicating if the lock attempt was successful.</returns>
        public Task <TLockableObject> AcquireLockAsync(TLockableObject obj, bool throwOnFailure = false)
        {
            var timeout = TimeSpan.FromMilliseconds(_store.GetLockTime().TotalMilliseconds *(_staleLockMultiplier + 1));

            return(AcquireLockAsync(obj, timeout, throwOnFailure));
        }