Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor ResiliencePipelineRegistry cleanup of internals #1505

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/LegacySupport/ConcurrentDictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace System.Collections.Concurrent;

#if !NETCOREAPP
internal static class ConcurrentDictionaryExtensions
{
public static TValue GetOrAdd<TKey, TValue, TArg>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument)
{
if (dictionary.TryGetValue(key, out TValue value))
{
return value;
}

return dictionary.GetOrAdd(key, valueFactory(key, factoryArgument));
}
}
#endif
24 changes: 11 additions & 13 deletions src/Polly.Core/Registry/ResiliencePipelineRegistry.TResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ private sealed class GenericRegistry<TResult> : IDisposable, IAsyncDisposable
{
private readonly Func<ResiliencePipelineBuilder<TResult>> _activator;
private readonly ConcurrentDictionary<TKey, Action<ResiliencePipelineBuilder<TResult>, ConfigureBuilderContext<TKey>>> _builders;
private readonly ConcurrentDictionary<TKey, ResiliencePipeline<TResult>> _strategies;
private readonly ConcurrentDictionary<TKey, ResiliencePipeline<TResult>> _pipelines;

private readonly Func<TKey, string> _builderNameFormatter;
private readonly Func<TKey, string>? _instanceNameFormatter;
Expand All @@ -23,14 +23,14 @@ public GenericRegistry(
{
_activator = activator;
_builders = new ConcurrentDictionary<TKey, Action<ResiliencePipelineBuilder<TResult>, ConfigureBuilderContext<TKey>>>(builderComparer);
_strategies = new ConcurrentDictionary<TKey, ResiliencePipeline<TResult>>(strategyComparer);
_pipelines = new ConcurrentDictionary<TKey, ResiliencePipeline<TResult>>(strategyComparer);
_builderNameFormatter = builderNameFormatter;
_instanceNameFormatter = instanceNameFormatter;
}

public bool TryGet(TKey key, [NotNullWhen(true)] out ResiliencePipeline<TResult>? strategy)
{
if (_strategies.TryGetValue(key, out strategy))
if (_pipelines.TryGetValue(key, out strategy))
{
return true;
}
Expand All @@ -49,37 +49,35 @@ public ResiliencePipeline<TResult> GetOrAdd(TKey key, Action<ResiliencePipelineB
{
var context = new ConfigureBuilderContext<TKey>(key, _builderNameFormatter(key), _instanceNameFormatter?.Invoke(key));

#if NETCOREAPP3_0_OR_GREATER
return _strategies.GetOrAdd(key, static (_, factory) =>
return _pipelines.GetOrAdd(key, static (_, factory) =>
{
return new ResiliencePipeline<TResult>(CreatePipelineComponent(factory.instance._activator, factory.context, factory.configure), DisposeBehavior.Reject);
var component = CreatePipelineComponent(factory.instance._activator, factory.context, factory.configure);

return new ResiliencePipeline<TResult>(component, DisposeBehavior.Reject);
},
(instance: this, context, configure));
#else
return _strategies.GetOrAdd(key, _ => new ResiliencePipeline<TResult>(CreatePipelineComponent(_activator, context, configure), DisposeBehavior.Reject));
#endif
}

public bool TryAddBuilder(TKey key, Action<ResiliencePipelineBuilder<TResult>, ConfigureBuilderContext<TKey>> configure) => _builders.TryAdd(key, configure);

public void Dispose()
{
foreach (var strategy in _strategies.Values)
foreach (var strategy in _pipelines.Values)
{
strategy.DisposeHelper.ForceDispose();
}

_strategies.Clear();
_pipelines.Clear();
}

public async ValueTask DisposeAsync()
{
foreach (var strategy in _strategies.Values)
foreach (var strategy in _pipelines.Values)
{
await strategy.DisposeHelper.ForceDisposeAsync().ConfigureAwait(false);
}

_strategies.Clear();
_pipelines.Clear();
}
}
}
8 changes: 3 additions & 5 deletions src/Polly.Core/Registry/ResiliencePipelineRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,13 @@ public ResiliencePipeline GetOrAddPipeline(TKey key, Action<ResiliencePipelineBu

var context = new ConfigureBuilderContext<TKey>(key, _builderNameFormatter(key), _instanceNameFormatter?.Invoke(key));

#if NETCOREAPP3_0_OR_GREATER
return _pipelines.GetOrAdd(key, static (_, factory) =>
{
return new ResiliencePipeline(CreatePipelineComponent(factory.instance._activator, factory.context, factory.configure), DisposeBehavior.Reject);
var component = CreatePipelineComponent(factory.instance._activator, factory.context, factory.configure);

return new ResiliencePipeline(component, DisposeBehavior.Reject);
},
(instance: this, context, configure));
#else
return _pipelines.GetOrAdd(key, _ => new ResiliencePipeline(CreatePipelineComponent(_activator, context, configure), DisposeBehavior.Reject));
#endif
}

/// <summary>
Expand Down