async static Task StartAttemptingAcquisition( LockingCoordinator lockingCoordinator, LockId lockId, ILogger logger, CancellationTokenSource lockingCancellationSource, IObserver<object> observer) { var holdingLock = false; var retryInterval = lockId.CalculateRetryInterval(); var randomGenerator = new Random(Guid.NewGuid().GetHashCode()); while (!lockingCancellationSource.IsCancellationRequested && !holdingLock) { try { holdingLock = await lockingCoordinator.TryAcquireLock(lockId).ConfigureAwait(false); if(holdingLock) { logger.ToInfoLog(string.Format("DISTRIBUTED LOCK ACQUIRED for {0}", lockId.Resource)); } else { logger.ToDebugLog("Unable to acquire the lock, retrying..."); await TaskUtils.SilentlyCanceledDelay( randomGenerator.Next(retryInterval.First(), retryInterval.Second()), lockingCancellationSource.Token) .ConfigureAwait(false); } } catch (Exception ex) { observer.OnError(ex); if(lockingCoordinator != null) await lockingCoordinator .TryReleaseTheLock(lockId) .ConfigureAwait(false); holdingLock = false; } } }