/// <inheritdoc cref="IDistributedLockManager.AcquireAsync" /> public async Task <DistributedLock?> AcquireAsync( DistributedLockSettings settings, CancellationToken cancellationToken = default) { Check.NotNull(settings, nameof(settings)); if (string.IsNullOrEmpty(settings.ResourceName)) { throw new InvalidOperationException( "ResourceName cannot be null. Please provide a valid resource name in the settings."); } if (settings is NullLockSettings) { return(await NullLockManager.AcquireAsync(settings, cancellationToken).ConfigureAwait(false)); } _logger.LogAcquiringLock(settings); var stopwatch = Stopwatch.StartNew(); while (settings.AcquireTimeout == null || stopwatch.Elapsed < settings.AcquireTimeout) { if (await TryAcquireLockAsync(settings).ConfigureAwait(false)) { _logger.LogLockAcquired(settings); return(new DistributedLock(settings, this)); } await Task.Delay(settings.AcquireRetryInterval, cancellationToken).ConfigureAwait(false); if (cancellationToken.IsCancellationRequested) { break; } } throw new TimeoutException( $"Timeout waiting to get the required lock '{settings.ResourceName}'."); }