-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathConfigurationClientExtensions.cs
More file actions
196 lines (175 loc) · 8.19 KB
/
ConfigurationClientExtensions.cs
File metadata and controls
196 lines (175 loc) · 8.19 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Azure;
using Azure.Data.AppConfiguration;
using Microsoft.Extensions.Configuration.AzureAppConfiguration.FeatureManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Extensions.Configuration.AzureAppConfiguration.Extensions
{
internal static class ConfigurationClientExtensions
{
public static async Task<KeyValueChange> GetKeyValueChange(this ConfigurationClient client, ConfigurationSetting setting, CancellationToken cancellationToken, bool makeConditionalRequest = true)
{
if (setting == null)
{
throw new ArgumentNullException(nameof(setting));
}
if (string.IsNullOrEmpty(setting.Key))
{
throw new ArgumentNullException($"{nameof(setting)}.{nameof(setting.Key)}");
}
try
{
Response<ConfigurationSetting> response = await client.GetConfigurationSettingAsync(setting, onlyIfChanged: makeConditionalRequest, cancellationToken).ConfigureAwait(false);
if (response.GetRawResponse().Status == (int)HttpStatusCode.OK &&
!response.Value.ETag.Equals(setting.ETag))
{
return new KeyValueChange
{
ChangeType = KeyValueChangeType.Modified,
Previous = setting,
Current = response.Value,
Key = setting.Key,
Label = setting.Label
};
}
}
catch (RequestFailedException e) when (e.Status == (int)HttpStatusCode.NotFound && setting.ETag != default)
{
return new KeyValueChange
{
ChangeType = KeyValueChangeType.Deleted,
Previous = setting,
Current = null,
Key = setting.Key,
Label = setting.Label
};
}
return new KeyValueChange
{
ChangeType = KeyValueChangeType.None,
Previous = setting,
Current = setting,
Key = setting.Key,
Label = setting.Label
};
}
public static async Task<IEnumerable<KeyValueChange>> GetKeyValueChangeCollection(
this ConfigurationClient client,
IEnumerable<ConfigurationSetting> keyValues,
GetKeyValueChangeCollectionOptions options,
StringBuilder logDebugBuilder,
StringBuilder logInfoBuilder,
Uri endpoint,
CancellationToken cancellationToken)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (keyValues == null)
{
keyValues = Enumerable.Empty<ConfigurationSetting>();
}
if (options.KeyFilter == null)
{
options.KeyFilter = string.Empty;
}
if (keyValues.Any(k => string.IsNullOrEmpty(k.Key)))
{
throw new ArgumentNullException($"{nameof(keyValues)}[].{nameof(ConfigurationSetting.Key)}");
}
if (keyValues.Any(k => !string.Equals(k.Label.NormalizeNull(), options.Label.NormalizeNull())))
{
throw new ArgumentException("All key-values registered for refresh must use the same label.", $"{nameof(keyValues)}[].{nameof(ConfigurationSetting.Label)}");
}
if (keyValues.Any(k => k.Label != null && k.Label.Contains("*")))
{
throw new ArgumentException("The label filter cannot contain '*'", $"{nameof(options)}.{nameof(options.Label)}");
}
var hasKeyValueCollectionChanged = false;
var selector = new SettingSelector
{
KeyFilter = options.KeyFilter,
LabelFilter = string.IsNullOrEmpty(options.Label) ? LabelFilter.Null : options.Label,
Fields = SettingFields.ETag | SettingFields.Key
};
// Dictionary of eTags that we write to and use for comparison
var eTagMap = keyValues.ToDictionary(kv => kv.Key, kv => kv.ETag);
// Fetch e-tags for prefixed key-values that can be used to detect changes
await TracingUtils.CallWithRequestTracing(options.RequestTracingEnabled, RequestType.Watch, options.RequestTracingOptions,
async () =>
{
await foreach (ConfigurationSetting setting in client.GetConfigurationSettingsAsync(selector, cancellationToken).ConfigureAwait(false))
{
if (!eTagMap.TryGetValue(setting.Key, out ETag etag) || !etag.Equals(setting.ETag))
{
hasKeyValueCollectionChanged = true;
break;
}
eTagMap.Remove(setting.Key);
}
}).ConfigureAwait(false);
// Check for any deletions
if (eTagMap.Any())
{
hasKeyValueCollectionChanged = true;
}
var changes = new List<KeyValueChange>();
// If changes have been observed, refresh prefixed key-values
if (hasKeyValueCollectionChanged)
{
selector = new SettingSelector
{
KeyFilter = options.KeyFilter,
LabelFilter = string.IsNullOrEmpty(options.Label) ? LabelFilter.Null : options.Label
};
eTagMap = keyValues.ToDictionary(kv => kv.Key, kv => kv.ETag);
await TracingUtils.CallWithRequestTracing(options.RequestTracingEnabled, RequestType.Watch, options.RequestTracingOptions,
async () =>
{
await foreach (ConfigurationSetting setting in client.GetConfigurationSettingsAsync(selector, cancellationToken).ConfigureAwait(false))
{
if (!eTagMap.TryGetValue(setting.Key, out ETag etag) || !etag.Equals(setting.ETag))
{
changes.Add(new KeyValueChange
{
ChangeType = KeyValueChangeType.Modified,
Key = setting.Key,
Label = options.Label.NormalizeNull(),
Previous = null,
Current = setting
});
string key = setting.Key.Substring(FeatureManagementConstants.FeatureFlagMarker.Length);
logDebugBuilder.AppendLine(LogHelper.BuildFeatureFlagReadMessage(key, options.Label.NormalizeNull(), endpoint.ToString()));
logInfoBuilder.AppendLine(LogHelper.BuildFeatureFlagUpdatedMessage(key));
}
eTagMap.Remove(setting.Key);
}
}).ConfigureAwait(false);
foreach (var kvp in eTagMap)
{
changes.Add(new KeyValueChange
{
ChangeType = KeyValueChangeType.Deleted,
Key = kvp.Key,
Label = options.Label.NormalizeNull(),
Previous = null,
Current = null
});
string key = kvp.Key.Substring(FeatureManagementConstants.FeatureFlagMarker.Length);
logDebugBuilder.AppendLine(LogHelper.BuildFeatureFlagReadMessage(key, options.Label.NormalizeNull(), endpoint.ToString()));
logInfoBuilder.AppendLine(LogHelper.BuildFeatureFlagUpdatedMessage(key));
}
}
return changes;
}
}
}