-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlobStorageConfigurationProvider.cs
More file actions
163 lines (140 loc) · 4.3 KB
/
BlobStorageConfigurationProvider.cs
File metadata and controls
163 lines (140 loc) · 4.3 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using Azure;
using Azure.Core;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
namespace BlobStorageConfigurationProviderSample;
public class BlobStorageConfigurationProvider(
string account, string container, string blobName,
TokenCredential credential, ILogger logger) : ConfigurationProvider, IDisposable
{
private static readonly TimeSpan DefaultRefreshInterval = TimeSpan.FromMinutes(1);
private readonly BlobClient _blob = new BlobContainerClient(
new Uri($"https://{account}.blob.core.windows.net/{container}"), credential)
.GetBlobClient(blobName);
private readonly CancellationTokenSource _cts = new();
private readonly SemaphoreSlim _semaphore = new(1, 1);
private PeriodicTimer? _timer;
private Task? _pollTask;
private ETag? _etag;
private int _pollingStarted;
private int _disposed;
public override void Load()
{
LoadAsync(reload: false, _cts.Token).GetAwaiter().GetResult();
StartPollingOnce();
}
private void StartPollingOnce()
{
if (Interlocked.Exchange(ref _pollingStarted, 1) != 0)
{
return;
}
_timer = new PeriodicTimer(DefaultRefreshInterval);
_pollTask = PollAsync();
}
private async Task PollAsync()
{
try
{
while (_timer is not null &&
await _timer.WaitForNextTickAsync(_cts.Token))
{
try
{
await LoadAsync(reload: true, _cts.Token);
}
catch (OperationCanceledException) when (_cts.IsCancellationRequested)
{
break;
}
catch (Exception ex)
{
logger.LogWarning(
ex,
"Failed to refresh configuration from blob '{BlobUri}'. " +
"Keeping last known good configuration.",
_blob.Uri);
}
}
}
catch (OperationCanceledException) when (_cts.IsCancellationRequested)
{
// Expected during shutdown.
}
}
private async Task LoadAsync(bool reload, CancellationToken ct)
{
var entered = false;
var shouldReload = false;
try
{
await _semaphore.WaitAsync(ct);
entered = true;
var options = new BlobDownloadOptions();
if (_etag is not null)
{
options.Conditions = new BlobRequestConditions
{
IfNoneMatch = _etag.Value
};
}
var response = await _blob.DownloadContentAsync(options, ct);
if (response.GetRawResponse().Status == 304)
{
return; // Blob unchanged
}
var result = response.Value;
await using var stream = result.Content.ToStream();
var config = new ConfigurationBuilder()
.AddJsonStream(stream)
.Build();
try
{
var newData = config
.AsEnumerable()
.Where(x => x.Value is not null)
.ToDictionary(x => x.Key, x => x.Value!, StringComparer.OrdinalIgnoreCase);
Data = newData;
_etag = result.Details.ETag;
shouldReload = reload;
}
finally
{
(config as IDisposable)?.Dispose();
}
}
finally
{
if (entered)
{
_semaphore.Release();
}
}
if (shouldReload)
{
OnReload();
}
}
public void Dispose()
{
if (Interlocked.Exchange(ref _disposed, 1) != 0)
{
return;
}
_cts.Cancel();
_timer?.Dispose();
if (_pollTask is not null)
{
try
{
_pollTask.GetAwaiter().GetResult();
}
catch (OperationCanceledException) when (_cts.IsCancellationRequested)
{
// Expected during shutdown.
}
}
_semaphore.Dispose();
_cts.Dispose();
}
}