/// <inheritdoc />
 public Task <IDisposable> TryAcquireAsync(string resource, LockOptions options,
                                           CancellationToken cancellationToken = default)
 {
     return(options.WaitTime.HasValue
                         ? TryRepeatAsync(resource, options, cancellationToken)
                         : _innerLock.TryAcquireAsync(resource, options.ExpiryTime, cancellationToken));
 }
예제 #2
0
        [MethodImpl(MethodImplOptions.NoInlining)] // need to isolate for GC
        private WeakReference TestCleanupHelper(IDistributedLock lock1, IDistributedLock lock2)
        {
            var handle = lock1.Acquire();

            Assert.IsNull(lock2.TryAcquireAsync().Result);

            return(new WeakReference(handle));
        }
        public static IDisposable TryAcquireWithAsyncCancellation(IDistributedLock @lock, TimeSpan timeout, CancellationToken cancellationToken)
        {
            var tryAcquireTask = @lock.TryAcquireAsync(timeout, cancellationToken);

            try
            {
                return(tryAcquireTask.Result);
            }
            catch (AggregateException ex)
            {
                // attempt to prevent the throwing of aggregate exceptions
                if (ex.InnerExceptions.Count == 1)
                {
                    // rethrow the inner exception without losing stack trace
                    ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                }
                throw; // otherwise just rethrow
            }
        }
 public static IDisposable?TryAcquireWithAsyncCancellation(IDistributedLock @lock, TimeSpan timeout, CancellationToken cancellationToken)
 {
     return(@lock.TryAcquireAsync(timeout, cancellationToken).GetAwaiter().GetResult());
 }
        public static Task <IDisposable> AcquireAsync(IDistributedLock @lock, TimeSpan?timeout, CancellationToken cancellationToken)
        {
            var tryAcquireTask = @lock.TryAcquireAsync(timeout ?? Timeout.InfiniteTimeSpan, cancellationToken);

            return(ValidateTryAcquireResultAsync(tryAcquireTask, timeout));
        }