-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathFlagdProvider.cs
More file actions
254 lines (215 loc) · 10.1 KB
/
FlagdProvider.cs
File metadata and controls
254 lines (215 loc) · 10.1 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using OpenFeature.Constant;
using OpenFeature.Model;
using OpenFeature.Providers.Flagd.Resolver.InProcess;
using OpenFeature.Providers.Flagd.Resolver.Rpc;
using Metadata = OpenFeature.Model.Metadata;
using Value = OpenFeature.Model.Value;
namespace OpenFeature.Providers.Flagd;
/// <summary>
/// FlagdProvider is the OpenFeature provider for flagD.
/// </summary>
public sealed class FlagdProvider : FeatureProvider
{
const string ProviderName = "flagd Provider";
private readonly FlagdConfig _config;
private readonly Metadata _providerMetadata = new Metadata(ProviderName);
private readonly Resolver.Resolver _resolver;
private readonly List<Hook> _hooks = new List<Hook>();
/// <summary>
/// Constructor of the provider. This constructor uses the value of the following
/// environment variables to initialise its client:
/// FLAGD_HOST - The host name of the flagd server (default="localhost")
/// FLAGD_PORT - The port of the flagd server (default="8013")
/// FLAGD_TLS - Determines whether to use https or not (default="false")
/// FLAGD_FLAGD_SERVER_CERT_PATH - The path to the client certificate (default="")
/// FLAGD_SOCKET_PATH - Path to the unix socket (default="")
/// FLAGD_CACHE - Enable or disable the cache (default="false")
/// FLAGD_MAX_CACHE_SIZE - The maximum size of the cache (default="10")
/// FLAGD_MAX_EVENT_STREAM_RETRIES - The maximum amount of retries for establishing the EventStream
/// FLAGD_RESOLVER - The type of resolver (in-process, file or rpc) to be used for the provider
/// FLAGD_SOURCE_FILE_PATH - The path to the flag definition JSON file (used when FLAGD_RESOLVER="file")
/// FLAGD_HASH_FILE_CHANGE - Use content hashing for file change detection (default="false", used when FLAGD_RESOLVER="file")
/// </summary>
public FlagdProvider() : this(FlagdConfig.Builder().Build())
{
}
/// <summary>
/// Constructor of the provider. This constructor uses the value of the following
/// environment variables to initialise its client:
/// FLAGD_FLAGD_SERVER_CERT_PATH - The path to the client certificate (default="")
/// FLAGD_CACHE - Enable or disable the cache (default="false")
/// FLAGD_MAX_CACHE_SIZE - The maximum size of the cache (default="10")
/// FLAGD_MAX_EVENT_STREAM_RETRIES - The maximum amount of retries for establishing the EventStream
/// FLAGD_RESOLVER - The type of resolver (in-process, file or rpc) to be used for the provider
/// FLAGD_SOURCE_FILE_PATH - The path to the flag definition JSON file (used when FLAGD_RESOLVER="file")
/// FLAGD_HASH_FILE_CHANGE - Use content hashing for file change detection (default="false", used when FLAGD_RESOLVER="file")
/// <param name="url">The URL of the flagd server</param>
/// <exception cref="ArgumentNullException">if no url is provided.</exception>
/// </summary>
public FlagdProvider(Uri url) : this(FlagdConfig.Builder(url).Build())
{
}
/// <summary>
/// Constructor of the provider.
/// <param name="config">The FlagdConfig object</param>
/// <exception cref="ArgumentNullException">if no config object is provided.</exception>
/// </summary>
public FlagdProvider(FlagdConfig config)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
_config = config;
if (_config.ResolverType == ResolverType.IN_PROCESS)
{
var jsonSchemaValidator = new JsonSchemaValidator(_config.Logger);
_resolver = new InProcessResolver(_config, jsonSchemaValidator);
}
else if (_config.ResolverType == ResolverType.FILE)
{
if (string.IsNullOrWhiteSpace(_config.SourceFilePath))
throw new ArgumentException("SourceFilePath must be set when using ResolverType.FILE");
var jsonSchemaValidator = new JsonSchemaValidator(_config.Logger);
_resolver = new FileBasedResolver(
_config.Logger,
_config.SourceFilePath,
jsonSchemaValidator,
_config.SourceSelector,
_config.UseHashFileChangeDetection);
}
else
{
_resolver = new RpcResolver(config);
}
_hooks.Add(new SyncMetadataHook(() => this._enrichedContext));
this._resolver.ProviderEvent += this.OnProviderEvent;
}
// just for testing, internal but visible in tests
internal FlagdProvider(Resolver.Resolver resolver)
{
_resolver = resolver;
}
// just for testing, internal but visible in tests
internal FlagdConfig GetConfig() => _config;
/// <summary>
/// Get the provider name.
/// </summary>
public static string GetProviderName()
{
return ProviderName;
}
/// <summary>
/// Return the metadata associated to this provider.
/// </summary>
public override Metadata GetMetadata() => _providerMetadata;
/// <summary>
/// Return the resolver of the provider
/// </summary>
internal Resolver.Resolver GetResolver() => _resolver;
/// <inheritdoc/>
public override IImmutableList<Hook> GetProviderHooks()
{
return this._hooks.ToImmutableList();
}
internal EvaluationContext _enrichedContext = EvaluationContext.Empty;
private bool _connected;
internal void OnProviderEvent(object _, FlagdProviderEvent payload)
{
switch (payload.EventType)
{
case ProviderEventTypes.ProviderConfigurationChanged:
{
this.UpdateEnrichedContext(payload);
if (this._connected)
{
this.EventChannel.Writer.TryWrite(new ProviderEventPayload
{
Type = ProviderEventTypes.ProviderConfigurationChanged,
ProviderName = this._providerMetadata.Name
});
break;
}
this.EventChannel.Writer.TryWrite(new ProviderEventPayload
{
Type = ProviderEventTypes.ProviderReady,
ProviderName = this._providerMetadata.Name
});
this._connected = true;
break;
}
case ProviderEventTypes.ProviderReady:
{
this.UpdateEnrichedContext(payload);
this.EventChannel.Writer.TryWrite(new ProviderEventPayload
{
Type = ProviderEventTypes.ProviderReady,
ProviderName = this._providerMetadata.Name
});
this._connected = true;
break;
}
case ProviderEventTypes.ProviderError:
{
this.EventChannel.Writer.TryWrite(new ProviderEventPayload
{
Type = ProviderEventTypes.ProviderError,
ProviderName = this._providerMetadata.Name
});
break;
}
default:
break;
}
}
private void UpdateEnrichedContext(FlagdProviderEvent payload)
{
var context = EvaluationContext.Builder();
foreach (var item in payload.SyncMetadata.AsDictionary())
{
context.Set(item.Key, item.Value);
}
this._enrichedContext = context.Build();
}
/// <inheritdoc/>
public override async Task InitializeAsync(EvaluationContext context, CancellationToken cancellationToken = default)
{
await _resolver.Init().ConfigureAwait(false);
}
/// <inheritdoc/>
public override async Task ShutdownAsync(CancellationToken cancellationToken = default)
{
await _resolver.Shutdown().ConfigureAwait(false);
this._resolver.ProviderEvent -= this.OnProviderEvent;
}
/// <inheritdoc/>
public override async Task<ResolutionDetails<bool>> ResolveBooleanValueAsync(string flagKey, bool defaultValue, EvaluationContext context = null, CancellationToken cancellationToken = default)
{
return await _resolver.ResolveBooleanValueAsync(flagKey, defaultValue, context).ConfigureAwait(false);
}
/// <inheritdoc/>
public override async Task<ResolutionDetails<string>> ResolveStringValueAsync(string flagKey, string defaultValue, EvaluationContext context = null, CancellationToken cancellationToken = default)
{
return await _resolver.ResolveStringValueAsync(flagKey, defaultValue, context).ConfigureAwait(false);
}
/// <inheritdoc/>
public override async Task<ResolutionDetails<int>> ResolveIntegerValueAsync(string flagKey, int defaultValue, EvaluationContext context = null, CancellationToken cancellationToken = default)
{
return await _resolver.ResolveIntegerValueAsync(flagKey, defaultValue, context).ConfigureAwait(false);
}
/// <inheritdoc/>
public override async Task<ResolutionDetails<double>> ResolveDoubleValueAsync(string flagKey, double defaultValue, EvaluationContext context = null, CancellationToken cancellationToken = default)
{
return await _resolver.ResolveDoubleValueAsync(flagKey, defaultValue, context).ConfigureAwait(false);
}
/// <inheritdoc/>
public override async Task<ResolutionDetails<Value>> ResolveStructureValueAsync(string flagKey, Value defaultValue, EvaluationContext context = null, CancellationToken cancellationToken = default)
{
return await _resolver.ResolveStructureValueAsync(flagKey, defaultValue, context).ConfigureAwait(false);
}
}