-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathApi.cs
206 lines (186 loc) · 8.94 KB
/
Api.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
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using OpenFeature.Model;
namespace OpenFeature
{
/// <summary>
/// The evaluation API allows for the evaluation of feature flag values, independent of any flag control plane or vendor.
/// In the absence of a provider the evaluation API uses the "No-op provider", which simply returns the supplied default flag value.
/// </summary>
/// <seealso href="https://github.com/open-feature/spec/blob/v0.5.2/specification/sections/01-flag-evaluation.md#1-flag-evaluation-api"/>
public sealed class Api
{
private EvaluationContext _evaluationContext = EvaluationContext.Empty;
private readonly ProviderRepository _repository = new ProviderRepository();
private readonly ConcurrentStack<Hook> _hooks = new ConcurrentStack<Hook>();
/// The reader/writer locks are not disposed because the singleton instance should never be disposed.
private readonly ReaderWriterLockSlim _evaluationContextLock = new ReaderWriterLockSlim();
/// <summary>
/// Singleton instance of Api
/// </summary>
public static Api Instance { get; } = new Api();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
// IE Lazy way of ensuring this is thread safe without using locks
static Api() { }
private Api() { }
/// <summary>
/// Sets the feature provider. In order to wait for the provider to be set, and initialization to complete,
/// await the returned task.
/// </summary>
/// <remarks>The provider cannot be set to null. Attempting to set the provider to null has no effect.</remarks>
/// <param name="featureProvider">Implementation of <see cref="FeatureProvider"/></param>
public async Task SetProvider(FeatureProvider featureProvider)
{
await this._repository.SetProvider(featureProvider, this.GetContext()).ConfigureAwait(false);
}
/// <summary>
/// Sets the feature provider to given clientName. In order to wait for the provider to be set, and
/// initialization to complete, await the returned task.
/// </summary>
/// <param name="clientName">Name of client</param>
/// <param name="featureProvider">Implementation of <see cref="FeatureProvider"/></param>
public async Task SetProvider(string clientName, FeatureProvider featureProvider)
{
await this._repository.SetProvider(clientName, featureProvider, this.GetContext()).ConfigureAwait(false);
}
/// <summary>
/// Gets the feature provider
/// <para>
/// The feature provider may be set from multiple threads, when accessing the global feature provider
/// it should be accessed once for an operation, and then that reference should be used for all dependent
/// operations. For instance, during an evaluation the flag resolution method, and the provider hooks
/// should be accessed from the same reference, not two independent calls to
/// <see cref="GetProvider()"/>.
/// </para>
/// </summary>
/// <returns><see cref="FeatureProvider"/></returns>
public FeatureProvider GetProvider()
{
return this._repository.GetProvider();
}
/// <summary>
/// Gets the feature provider with given clientName
/// </summary>
/// <param name="clientName">Name of client</param>
/// <returns>A provider associated with the given clientName, if clientName is empty or doesn't
/// have a corresponding provider the default provider will be returned</returns>
public FeatureProvider GetProvider(string clientName)
{
return this._repository.GetProvider(clientName);
}
/// <summary>
/// Gets providers metadata
/// <para>
/// This method is not guaranteed to return the same provider instance that may be used during an evaluation
/// in the case where the provider may be changed from another thread.
/// For multiple dependent provider operations see <see cref="GetProvider()"/>.
/// </para>
/// </summary>
/// <returns><see cref="ClientMetadata"/></returns>
public Metadata GetProviderMetadata() => this.GetProvider().GetMetadata();
/// <summary>
/// Gets providers metadata assigned to the given clientName. If the clientName has no provider
/// assigned to it the default provider will be returned
/// </summary>
/// <param name="clientName">Name of client</param>
/// <returns>Metadata assigned to provider</returns>
public Metadata GetProviderMetadata(string clientName) => this.GetProvider(clientName).GetMetadata();
/// <summary>
/// Create a new instance of <see cref="FeatureClient"/> using the current provider
/// </summary>
/// <param name="name">Name of client</param>
/// <param name="version">Version of client</param>
/// <param name="logger">Logger instance used by client</param>
/// <param name="context">Context given to this client</param>
/// <returns><see cref="FeatureClient"/></returns>
public FeatureClient GetClient(string name = null, string version = null, ILogger logger = null,
EvaluationContext context = null) =>
new FeatureClient(name, version, logger, context);
/// <summary>
/// Appends list of hooks to global hooks list
/// <para>
/// The appending operation will be atomic.
/// </para>
/// </summary>
/// <param name="hooks">A list of <see cref="Hook"/></param>
public void AddHooks(IEnumerable<Hook> hooks) => this._hooks.PushRange(hooks.ToArray());
/// <summary>
/// Adds a hook to global hooks list
/// <para>
/// Hooks which are dependent on each other should be provided in a collection
/// using the <see cref="AddHooks(IEnumerable{Hook})"/>.
/// </para>
/// </summary>
/// <param name="hook">Hook that implements the <see cref="Hook"/> interface</param>
public void AddHooks(Hook hook) => this._hooks.Push(hook);
/// <summary>
/// Enumerates the global hooks.
/// <para>
/// The items enumerated will reflect the registered hooks
/// at the start of enumeration. Hooks added during enumeration
/// will not be included.
/// </para>
/// </summary>
/// <returns>Enumeration of <see cref="Hook"/></returns>
public IEnumerable<Hook> GetHooks() => this._hooks.Reverse();
/// <summary>
/// Removes all hooks from global hooks list
/// </summary>
public void ClearHooks() => this._hooks.Clear();
/// <summary>
/// Sets the global <see cref="EvaluationContext"/>
/// </summary>
/// <param name="context">The <see cref="EvaluationContext"/> to set</param>
public void SetContext(EvaluationContext context)
{
this._evaluationContextLock.EnterWriteLock();
try
{
this._evaluationContext = context ?? EvaluationContext.Empty;
}
finally
{
this._evaluationContextLock.ExitWriteLock();
}
}
/// <summary>
/// Gets the global <see cref="EvaluationContext"/>
/// <para>
/// The evaluation context may be set from multiple threads, when accessing the global evaluation context
/// it should be accessed once for an operation, and then that reference should be used for all dependent
/// operations.
/// </para>
/// </summary>
/// <returns>An <see cref="EvaluationContext"/></returns>
public EvaluationContext GetContext()
{
this._evaluationContextLock.EnterReadLock();
try
{
return this._evaluationContext;
}
finally
{
this._evaluationContextLock.ExitReadLock();
}
}
/// <summary>
/// <para>
/// Shut down and reset the current status of OpenFeature API.
/// </para>
/// <para>
/// This call cleans up all active providers and attempts to shut down internal event handling mechanisms.
/// Once shut down is complete, API is reset and ready to use again.
/// </para>
/// </summary>
public async Task Shutdown()
{
await this._repository.Shutdown().ConfigureAwait(false);
}
}
}