-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDistributedLeaseBlobStorage.cs
116 lines (98 loc) · 3.48 KB
/
DistributedLeaseBlobStorage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System.Net;
using System.Text.Json;
using Azure;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using DistributedLeaseManager.Core;
using Microsoft.Extensions.Options;
namespace DistributedLeaseManager.AzureBlobStorage;
public class DistributedLeaseBlobStorage : IDistributedLeaseRepository
{
private readonly BlobServiceClient _blobServiceClient;
private readonly DistributedLeaseBlobStorageOptions _options;
public DistributedLeaseBlobStorage(
BlobServiceClient blobServiceClient,
IOptions<DistributedLeaseBlobStorageOptions> options)
{
_blobServiceClient = blobServiceClient;
_options = options.Value;
}
public Task EnsureCreated()
=> _blobServiceClient
.GetBlobContainerClient(_options.ContainerName)
.CreateIfNotExistsAsync();
public async Task<bool> Add(DistributedLease lease)
{
using var jsonStream = new MemoryStream();
JsonSerializer.Serialize(jsonStream, lease);
jsonStream.Position = 0;
try
{
await _blobServiceClient
.GetBlobContainerClient(_options.ContainerName)
.UploadBlobAsync(GetBlobPath(lease), jsonStream);
return true;
}
catch (RequestFailedException ex)
when (ex.Status == (int)HttpStatusCode.Conflict)
{
return false;
}
}
public async Task<DistributedLease?> Find(string resourceCategory, string resourceId)
{
using var blobStream = new MemoryStream();
try
{
var response = await _blobServiceClient
.GetBlobContainerClient(_options.ContainerName)
.GetBlobClient(GetBlobPath(resourceCategory, resourceId))
.DownloadToAsync(blobStream);
blobStream.Position = 0;
var lease = JsonSerializer.Deserialize<DistributedLease>(blobStream)!;
lease.ETag = response.Headers.ETag.ToString()!;
return lease;
}
catch (RequestFailedException ex)
when (ex.Status == (int)HttpStatusCode.NotFound)
{
return null;
}
}
public async Task<bool> Update(DistributedLease lease)
{
using var jsonStream = new MemoryStream();
JsonSerializer.Serialize(jsonStream, lease);
jsonStream.Position = 0;
try
{
await _blobServiceClient
.GetBlobContainerClient(_options.ContainerName)
.GetBlobClient(GetBlobPath(lease))
.UploadAsync(jsonStream, new BlobUploadOptions
{
Conditions = new BlobRequestConditions
{
IfMatch = new ETag(lease.ETag)
}
});
return true;
}
catch (RequestFailedException ex)
when (ex.Status == (int)HttpStatusCode.PreconditionFailed)
{
return false;
}
}
public async Task<bool> Remove(DistributedLease lease)
{
var response = await _blobServiceClient
.GetBlobContainerClient(_options.ContainerName)
.DeleteBlobIfExistsAsync(GetBlobPath(lease));
return response.Value;
}
private static string GetBlobPath(DistributedLease lease)
=> GetBlobPath(lease.ResourceCategory, lease.ResourceId);
private static string GetBlobPath(string resourceCategory, string resourceId)
=> $"{resourceCategory}/{resourceId}.json";
}