|
| 1 | +using Azure; |
| 2 | +using Azure.Storage.Blobs; |
| 3 | +using Azure.Storage.Blobs.Specialized; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using Paramore.Brighter.Logging; |
| 6 | + |
| 7 | +namespace Paramore.Brighter.Locking.Azure; |
| 8 | + |
| 9 | +public class AzureBlobLockingProvider(AzureBlobLockingProviderOptions options) : IDistributedLock |
| 10 | +{ |
| 11 | + private readonly BlobContainerClient _containerClient = new BlobContainerClient(options.BlobContainerUri, options.TokenCredential); |
| 12 | + private readonly ILogger _logger = ApplicationLogging.CreateLogger<AzureBlobLockingProviderOptions>(); |
| 13 | + |
| 14 | + private readonly Dictionary<string, string> _leases = new Dictionary<string, string>(); |
| 15 | + |
| 16 | + public async Task<bool> ObtainLockAsync(string resource, CancellationToken cancellationToken) |
| 17 | + { |
| 18 | + var client = GetBlobClient(resource); |
| 19 | + |
| 20 | + // Write if does not exist |
| 21 | + if (!await client.ExistsAsync(cancellationToken)) |
| 22 | + { |
| 23 | + await using var emptyStream = new MemoryStream(); |
| 24 | + await using var writer = new StreamWriter(emptyStream); |
| 25 | + await writer.WriteAsync(string.Empty); |
| 26 | + await writer.FlushAsync(cancellationToken); |
| 27 | + emptyStream.Position = 0; |
| 28 | + await client.UploadAsync(emptyStream, cancellationToken: cancellationToken); |
| 29 | + } |
| 30 | + |
| 31 | + try |
| 32 | + { |
| 33 | + var response = await client.GetBlobLeaseClient().AcquireAsync(options.LeaseValidity, cancellationToken: cancellationToken); |
| 34 | + _leases.Add(NormaliseResourceName(resource), response.Value.LeaseId); |
| 35 | + return true; |
| 36 | + } |
| 37 | + catch (RequestFailedException e) |
| 38 | + { |
| 39 | + _logger.LogInformation("Could not Acquire Lease on Blob {LockResourceName}", resource); |
| 40 | + return false; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public bool ObtainLock(string resource) |
| 45 | + { |
| 46 | + var client = GetBlobClient(resource); |
| 47 | + |
| 48 | + // Write if does not exist |
| 49 | + if (!client.Exists()) |
| 50 | + { |
| 51 | + using var emptyStream = new MemoryStream(); |
| 52 | + using var writer = new StreamWriter(emptyStream); |
| 53 | + writer.Write(string.Empty); |
| 54 | + writer.Flush(); |
| 55 | + emptyStream.Position = 0; |
| 56 | + client.Upload(emptyStream); |
| 57 | + } |
| 58 | + |
| 59 | + try |
| 60 | + { |
| 61 | + var response = client.GetBlobLeaseClient().Acquire(options.LeaseValidity); |
| 62 | + _leases.Add(NormaliseResourceName(resource), response.Value.LeaseId); |
| 63 | + return true; |
| 64 | + } |
| 65 | + catch (RequestFailedException e) |
| 66 | + { |
| 67 | + _logger.LogInformation("Could not Acquire Lease on Blob {LockResourceName}", resource); |
| 68 | + return false; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public async Task ReleaseLockAsync(string resource, CancellationToken cancellationToken) |
| 73 | + { |
| 74 | + var client = GetBlobLeaseClientForResource(resource); |
| 75 | + if(client == null) |
| 76 | + return; |
| 77 | + await client.ReleaseAsync(cancellationToken: cancellationToken); |
| 78 | + _leases.Remove(NormaliseResourceName(resource)); |
| 79 | + } |
| 80 | + |
| 81 | + public void ReleaseLock(string resource) |
| 82 | + { |
| 83 | + var client = GetBlobLeaseClientForResource(resource); |
| 84 | + if(client == null) |
| 85 | + return; |
| 86 | + client.Release(); |
| 87 | + _leases.Remove(NormaliseResourceName(resource)); |
| 88 | + } |
| 89 | + |
| 90 | + private BlobLeaseClient? GetBlobLeaseClientForResource(string resource) |
| 91 | + { |
| 92 | + if (_leases.ContainsKey(NormaliseResourceName(resource))) |
| 93 | + return GetBlobClient(resource).GetBlobLeaseClient(_leases[NormaliseResourceName(resource)]); |
| 94 | + |
| 95 | + _logger.LogInformation("No lock found for {LockResourceName}", resource); |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + private BlobClient GetBlobClient(string resource) |
| 100 | + { |
| 101 | + var storageLocation = options.StorageLocationFunc.Invoke(NormaliseResourceName(resource)); |
| 102 | + return _containerClient.GetBlobClient(storageLocation); |
| 103 | + } |
| 104 | + |
| 105 | + private static string NormaliseResourceName(string resourceName) => resourceName.ToLower(); |
| 106 | +} |
0 commit comments