-
Notifications
You must be signed in to change notification settings - Fork 3
/
Provider.cs
221 lines (194 loc) · 9.94 KB
/
Provider.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
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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using LaunchDarkly.Logging;
using LaunchDarkly.Sdk;
using LaunchDarkly.Sdk.Internal.Concurrent;
using LaunchDarkly.Sdk.Server;
using LaunchDarkly.Sdk.Server.Interfaces;
using OpenFeature;
using OpenFeature.Constant;
using OpenFeature.Model;
namespace LaunchDarkly.OpenFeature.ServerProvider
{
/// <summary>
/// An OpenFeature <see cref="FeatureProvider"/> which enables the use of the LaunchDarkly Server-Side SDK for .NET
/// with OpenFeature.
/// </summary>
/// <example>
/// var provider = new Provider(Configuration.Builder("my-sdk-key").Build());
///
/// OpenFeature.Api.Instance.SetProvider(provider);
///
/// var client = OpenFeature.Api.Instance.GetClient();
/// </example>
public sealed partial class Provider : FeatureProvider
{
private const string NameSpace = "OpenFeature.ServerProvider";
private readonly Metadata _metadata = new Metadata($"LaunchDarkly.{NameSpace}");
private readonly ILdClient _client;
private readonly EvalContextConverter _contextConverter;
private readonly StatusProvider _statusProvider;
private readonly AtomicBoolean _initializeCalled = new AtomicBoolean(false);
// There is no support for void task completion, so we use bool as a dummy result type.
private readonly TaskCompletionSource<bool> _initCompletion = new TaskCompletionSource<bool>();
private readonly Logger _logger;
private const string ProviderShutdownMessage =
"the provider has encountered a permanent error or been shutdown";
internal Provider(ILdClient client)
{
_client = client;
_logger = _client.GetLogger().SubLogger(NameSpace);
_statusProvider = new StatusProvider(EventChannel, _metadata.Name, _logger);
_contextConverter = new EvalContextConverter(_logger);
}
/// <summary>
/// Construct a new instance of the provider with the given configuration.
/// </summary>
/// <param name="config">A client configuration object</param>
public Provider(Configuration config) : this(new LdClient(WrapConfig(config)))
{
}
/// <summary>
/// Construct a new instance of the provider with the given SDK key.
/// </summary>
/// <param name="sdkKey">The SDK key</param>
public Provider(string sdkKey) : this(new LdClient(WrapConfig(Configuration.Builder(sdkKey).Build())))
{
}
/// <summary>
/// <para>
/// Get the underlying client instance.
/// </para>
/// <para>
/// If the initialization/shutdown features of OpenFeature are being used, then the returned client instance
/// may be shutdown if the provider has been removed from the OpenFeature API instance.
/// </para>
/// <para>
/// This client instance can be used to take advantage of features which are not part of OpenFeature.
/// For instance using migration flags.
/// </para>
/// </summary>
/// <returns>The LaunchDarkly client instance</returns>
public ILdClient GetClient()
{
return _client;
}
private static Configuration WrapConfig(Configuration config)
{
return Configuration.Builder(config)
.WrapperInfo(Components.WrapperInfo().Name("open-feature-dotnet-server")
.Version(typeof(Provider).Assembly.GetName().Version.ToString()))
.Build();
}
#region FeatureProvider Implementation
/// <inheritdoc />
public override Metadata GetMetadata() => _metadata;
/// <inheritdoc />
public override Task<ResolutionDetails<bool>> ResolveBooleanValueAsync(string flagKey, bool defaultValue,
EvaluationContext context = null, CancellationToken cancellationToken = default) => Task.FromResult(_client
.BoolVariationDetail(flagKey, _contextConverter.ToLdContext(context), defaultValue)
.ToResolutionDetails(flagKey));
/// <inheritdoc />
public override Task<ResolutionDetails<string>> ResolveStringValueAsync(string flagKey, string defaultValue,
EvaluationContext context = null, CancellationToken cancellationToken = default) => Task.FromResult(_client
.StringVariationDetail(flagKey, _contextConverter.ToLdContext(context), defaultValue)
.ToResolutionDetails(flagKey));
/// <inheritdoc />
public override Task<ResolutionDetails<int>> ResolveIntegerValueAsync(string flagKey, int defaultValue,
EvaluationContext context = null, CancellationToken cancellationToken = default) => Task.FromResult(_client
.IntVariationDetail(flagKey, _contextConverter.ToLdContext(context), defaultValue)
.ToResolutionDetails(flagKey));
/// <inheritdoc />
public override Task<ResolutionDetails<double>> ResolveDoubleValueAsync(string flagKey, double defaultValue,
EvaluationContext context = null, CancellationToken cancellationToken = default) => Task.FromResult(_client
.DoubleVariationDetail(flagKey, _contextConverter.ToLdContext(context), defaultValue)
.ToResolutionDetails(flagKey));
/// <inheritdoc />
public override Task<ResolutionDetails<Value>> ResolveStructureValueAsync(string flagKey, Value defaultValue,
EvaluationContext context = null, CancellationToken cancellationToken = default) => Task.FromResult(_client
.JsonVariationDetail(flagKey, _contextConverter.ToLdContext(context), LdValue.Null)
.ToValueDetail(defaultValue).ToResolutionDetails(flagKey));
/// <inheritdoc />
public override Task InitializeAsync(EvaluationContext context, CancellationToken cancellationToken = default)
{
if (_initializeCalled.GetAndSet(true))
{
return _initCompletion.Task;
}
_client.FlagTracker.FlagChanged += FlagChangeHandler;
_client.DataSourceStatusProvider.StatusChanged += StatusChangeHandler;
// We start listening for status changes, and then we check the current status change. If we do not check
// then we could have missed a status change. If we check before registering a listener, then we could
// miss a change between checking and listening. Doing it this way we can get duplicates, but we filter
// when the status does not actually change, so we won't emit duplicate events.
if (_client.Initialized)
{
_statusProvider.SetStatus(ProviderStatus.Ready);
_initCompletion.TrySetResult(true);
}
if (_client.DataSourceStatusProvider.Status.State == DataSourceState.Off)
{
_statusProvider.SetStatus(ProviderStatus.Error, ProviderShutdownMessage);
_initCompletion.TrySetException(new LaunchDarklyProviderInitException(ProviderShutdownMessage));
}
return _initCompletion.Task;
}
/// <inheritdoc />
public override Task ShutdownAsync(CancellationToken cancellationToken = default)
{
_client.DataSourceStatusProvider.StatusChanged -= StatusChangeHandler;
_client.FlagTracker.FlagChanged -= FlagChangeHandler;
(_client as IDisposable)?.Dispose();
_statusProvider.SetStatus(ProviderStatus.NotReady);
return Task.CompletedTask;
}
#endregion
private void FlagChangeHandler(object sender, FlagChangeEvent changeEvent)
{
Task.Run(() => SafeWriteChangeEvent(changeEvent)).ConfigureAwait(false);
}
private async Task SafeWriteChangeEvent(FlagChangeEvent changeEvent)
{
try
{
await EventChannel.Writer.WriteAsync(new ProviderEventPayload
{
ProviderName = _metadata.Name,
Type = ProviderEventTypes.ProviderConfigurationChanged,
FlagsChanged = new List<string> {changeEvent.Key},
}).ConfigureAwait(false);
}
catch (Exception e)
{
_logger.Warn($"Encountered an error sending configuration changed events: {e.Message}");
}
}
private void StatusChangeHandler(object sender, DataSourceStatus status)
{
switch (status.State)
{
case DataSourceState.Initializing:
break;
case DataSourceState.Valid:
_statusProvider.SetStatus(ProviderStatus.Ready);
_initCompletion.TrySetResult(true);
break;
case DataSourceState.Interrupted:
// The "ProviderStatus.Error" state says it is unable to evaluate flags. We can always evaluate
// flags.
_statusProvider.SetStatus(ProviderStatus.Stale,
status.LastError?.Message ?? "encountered an unknown error");
break;
case DataSourceState.Off:
default:
// If we had initialized every, then we could still initialize flags, but I think we need to let
// a consumer know we have encountered an unrecoverable problem with the connection.
_statusProvider.SetStatus(ProviderStatus.Fatal, ProviderShutdownMessage);
_initCompletion.TrySetException(new LaunchDarklyProviderInitException(ProviderShutdownMessage));
break;
}
}
}
}