Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/DistributedLock.Azure/AzureBlobLeaseDistributedLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Medallion.Threading.Azure;
public sealed partial class AzureBlobLeaseDistributedLock : IInternalDistributedLock<AzureBlobLeaseDistributedLockHandle>
{
/// <summary>
/// Metadata marker used to indicate that a blob was created for distributed locking and therefore
/// Metadata marker used to indicate that a blob was created for distributed locking and therefore
/// should be destroyed upon release
/// </summary>
private static readonly string CreatedMetadataKey = $"__DistributedLock";
Expand Down Expand Up @@ -53,7 +53,7 @@ internal static string GetSafeName(string name, BlobContainerClient blobContaine

return DistributedLockHelpers.ToSafeName(name, maxLength, s => ConvertToValidName(s));

// check based on
// check based on
// https://docs.microsoft.com/en-us/azure/storage/common/storage-use-emulator#connect-to-the-emulator-account-using-the-well-known-account-name-and-key
bool IsStorageEmulator() => blobContainerClient.Uri.IsAbsoluteUri
&& blobContainerClient.Uri.AbsoluteUri.StartsWith("http://127.0.0.1:10000/devstoreaccount1", StringComparison.Ordinal);
Expand Down Expand Up @@ -109,9 +109,11 @@ static string ConvertToValidName(string name)
CancellationToken cancellationToken,
bool isRetryAfterCreate)
{
try { await leaseClient.AcquireAsync(this._options.duration, cancellationToken).ConfigureAwait(false); }
catch (RequestFailedException acquireException)
var response = await leaseClient.AcquireAsync(this._options.duration, cancellationToken).ConfigureAwait(false);
if (response.IsError)
{
var acquireException = new RequestFailedException(response);

if (acquireException.ErrorCode == AzureErrors.LeaseAlreadyPresent) { return null; }

if (acquireException.ErrorCode == AzureErrors.BlobNotFound)
Expand Down Expand Up @@ -149,7 +151,7 @@ static string ConvertToValidName(string name)
}
}

throw;
throw acquireException;
}

var shouldDeleteBlob = isRetryAfterCreate
Expand Down
18 changes: 12 additions & 6 deletions src/DistributedLock.Azure/BlobLeaseClientWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Azure.Storage.Blobs.Specialized;
using Azure;
using Azure.Storage.Blobs.Specialized;
using Medallion.Threading.Internal;

namespace Medallion.Threading.Azure;
Expand All @@ -17,14 +18,19 @@ public BlobLeaseClientWrapper(BlobLeaseClient blobLeaseClient)

public string LeaseId => this._blobLeaseClient.LeaseId;

public ValueTask AcquireAsync(TimeoutValue duration, CancellationToken cancellationToken)
public ValueTask<Response> AcquireAsync(TimeoutValue duration, CancellationToken cancellationToken)
{
var requestContext = new RequestContext
{
CancellationToken = cancellationToken,
ErrorOptions = ErrorOptions.NoThrow
};

if (SyncViaAsync.IsSynchronous)
{
this._blobLeaseClient.Acquire(duration.TimeSpan, cancellationToken: cancellationToken);
return default;
return new ValueTask<Response>(this._blobLeaseClient.Acquire(duration.TimeSpan, conditions: null, requestContext));
}
return new ValueTask(this._blobLeaseClient.AcquireAsync(duration.TimeSpan, cancellationToken: cancellationToken));
return new ValueTask<Response>(this._blobLeaseClient.AcquireAsync(duration.TimeSpan, conditions: null, requestContext));
}

public ValueTask RenewAsync(CancellationToken cancellationToken)
Expand All @@ -46,4 +52,4 @@ public ValueTask ReleaseAsync()
}
return new ValueTask(this._blobLeaseClient.ReleaseAsync());
}
}
}