Skip to content

Commit

Permalink
fixup: remove unused callbacks
Browse files Browse the repository at this point in the history
Signed-off-by: Todd Baert <[email protected]>
  • Loading branch information
toddbaert committed Jun 12, 2024
1 parent 72203e3 commit 216ebc7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 239 deletions.
6 changes: 3 additions & 3 deletions src/OpenFeature/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private Api() { }
public async Task SetProviderAsync(FeatureProvider? featureProvider)
{
this._eventExecutor.RegisterDefaultFeatureProvider(featureProvider);
await this._repository.SetProvider(featureProvider, this.GetContext(), null, afterInitialization, afterError).ConfigureAwait(false);
await this._repository.SetProvider(featureProvider, this.GetContext(), afterInitialization, afterError).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -62,7 +62,7 @@ public async Task SetProviderAsync(string clientName, FeatureProvider featurePro
throw new ArgumentNullException(nameof(clientName));
}
this._eventExecutor.RegisterClientFeatureProvider(clientName, featureProvider);
await this._repository.SetProvider(clientName, featureProvider, this.GetContext(), null, afterInitialization, afterError).ConfigureAwait(false);
await this._repository.SetProvider(clientName, featureProvider, this.GetContext(), afterInitialization, afterError).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -269,7 +269,7 @@ internal void RemoveClientHandler(string client, ProviderEventTypes eventType, E
/// <summary>
/// Update the provider state to READY and emit an READY after successful init.
/// </summary>
private void afterInitialization(FeatureProvider provider)
private void afterInitialization(FeatureProvider provider)
{
provider.Status = ProviderStatus.Ready;
var eventPayload = new ProviderEventPayload
Expand Down
72 changes: 17 additions & 55 deletions src/OpenFeature/ProviderRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,18 @@ public async ValueTask DisposeAsync()
/// </summary>
/// <param name="featureProvider">the provider to set as the default, passing null has no effect</param>
/// <param name="context">the context to initialize the provider with</param>
/// <param name="afterSet">
/// <para>
/// Called after the provider is set, but before any actions are taken on it.
/// </para>
/// This can be used for tasks such as registering event handlers. It should be noted that this can be called
/// several times for a single provider. For instance registering a provider with multiple names or as the
/// default and named provider.
/// <para>
/// </para>
/// </param>
/// <param name="afterInitialization">
/// <param name="afterInitSuccess">
/// called after the provider has initialized successfully, only called if the provider needed initialization
/// </param>
/// <param name="afterError">
/// <param name="afterInitError">
/// called if an error happens during the initialization of the provider, only called if the provider needed
/// initialization
/// </param>
/// <param name="afterShutdown">called after a provider is shutdown, can be used to remove event handlers</param>
public async Task SetProvider(
FeatureProvider? featureProvider,
EvaluationContext context,
Action<FeatureProvider>? afterSet = null,
Action<FeatureProvider>? afterInitialization = null,
Action<FeatureProvider, Exception>? afterError = null,
Action<FeatureProvider>? afterShutdown = null)
Action<FeatureProvider>? afterInitSuccess = null,
Action<FeatureProvider, Exception>? afterInitError = null)
{
// Cannot unset the feature provider.
if (featureProvider == null)
Expand All @@ -88,19 +75,16 @@ public async Task SetProvider(

var oldProvider = this._defaultProvider;
this._defaultProvider = featureProvider;
afterSet?.Invoke(featureProvider);
// We want to allow shutdown to happen concurrently with initialization, and the caller to not
// wait for it.
#pragma warning disable CS4014
this.ShutdownIfUnused(oldProvider, afterShutdown, afterError);
#pragma warning restore CS4014
_ = this.ShutdownIfUnused(oldProvider);
}
finally
{
this._providersLock.ExitWriteLock();
}

await InitProvider(this._defaultProvider, context, afterInitialization, afterError)
await InitProvider(this._defaultProvider, context, afterInitSuccess, afterInitError)
.ConfigureAwait(false);
}

Expand Down Expand Up @@ -134,31 +118,18 @@ private static async Task InitProvider(
/// <param name="clientName">the name to associate with the provider</param>
/// <param name="featureProvider">the provider to set as the default, passing null has no effect</param>
/// <param name="context">the context to initialize the provider with</param>
/// <param name="afterSet">
/// <para>
/// Called after the provider is set, but before any actions are taken on it.
/// </para>
/// This can be used for tasks such as registering event handlers. It should be noted that this can be called
/// several times for a single provider. For instance registering a provider with multiple names or as the
/// default and named provider.
/// <para>
/// </para>
/// </param>
/// <param name="afterInitialization">
/// <param name="afterInitSuccess">
/// called after the provider has initialized successfully, only called if the provider needed initialization
/// </param>
/// <param name="afterError">
/// <param name="afterInitError">
/// called if an error happens during the initialization of the provider, only called if the provider needed
/// initialization
/// </param>
/// <param name="afterShutdown">called after a provider is shutdown, can be used to remove event handlers</param>
public async Task SetProvider(string? clientName,
FeatureProvider? featureProvider,
EvaluationContext context,
Action<FeatureProvider>? afterSet = null,
Action<FeatureProvider>? afterInitialization = null,
Action<FeatureProvider, Exception>? afterError = null,
Action<FeatureProvider>? afterShutdown = null)
Action<FeatureProvider>? afterInitSuccess = null,
Action<FeatureProvider, Exception>? afterInitError = null)
{
// Cannot set a provider for a null clientName.
if (clientName == null)
Expand All @@ -175,7 +146,6 @@ public async Task SetProvider(string? clientName,
{
this._featureProviders.AddOrUpdate(clientName, featureProvider,
(key, current) => featureProvider);
afterSet?.Invoke(featureProvider);
}
else
{
Expand All @@ -186,25 +156,21 @@ public async Task SetProvider(string? clientName,

// We want to allow shutdown to happen concurrently with initialization, and the caller to not
// wait for it.
#pragma warning disable CS4014
this.ShutdownIfUnused(oldProvider, afterShutdown, afterError);
#pragma warning restore CS4014
_ = this.ShutdownIfUnused(oldProvider);
}
finally
{
this._providersLock.ExitWriteLock();
}

await InitProvider(featureProvider, context, afterInitialization, afterError).ConfigureAwait(false);
await InitProvider(featureProvider, context, afterInitSuccess, afterInitError).ConfigureAwait(false);
}

/// <remarks>
/// Shutdown the feature provider if it is unused. This must be called within a write lock of the _providersLock.
/// </remarks>
private async Task ShutdownIfUnused(
FeatureProvider? targetProvider,
Action<FeatureProvider>? afterShutdown,
Action<FeatureProvider, Exception>? afterError)
FeatureProvider? targetProvider)
{
if (ReferenceEquals(this._defaultProvider, targetProvider))
{
Expand All @@ -216,7 +182,7 @@ private async Task ShutdownIfUnused(
return;
}

await SafeShutdownProvider(targetProvider, afterShutdown, afterError).ConfigureAwait(false);
await SafeShutdownProvider(targetProvider).ConfigureAwait(false);
}

/// <remarks>
Expand All @@ -228,9 +194,7 @@ private async Task ShutdownIfUnused(
/// it would not be meaningful to emit an error.
/// </para>
/// </remarks>
private static async Task SafeShutdownProvider(FeatureProvider? targetProvider,
Action<FeatureProvider>? afterShutdown,
Action<FeatureProvider, Exception>? afterError)
private async Task SafeShutdownProvider(FeatureProvider? targetProvider)
{
if (targetProvider == null)
{
Expand All @@ -240,11 +204,9 @@ private static async Task SafeShutdownProvider(FeatureProvider? targetProvider,
try
{
await targetProvider.Shutdown().ConfigureAwait(false);
afterShutdown?.Invoke(targetProvider);
}
catch (Exception ex)
catch (Exception)
{
afterError?.Invoke(targetProvider, ex);
}
}

Expand Down Expand Up @@ -305,7 +267,7 @@ public async Task Shutdown(Action<FeatureProvider, Exception>? afterError = null
foreach (var targetProvider in providers)
{
// We don't need to take any actions after shutdown.
await SafeShutdownProvider(targetProvider, null, afterError).ConfigureAwait(false);
await SafeShutdownProvider(targetProvider).ConfigureAwait(false);
}
}
}
Expand Down
Loading

0 comments on commit 216ebc7

Please sign in to comment.