Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into mtomka/exponentialwit…
Browse files Browse the repository at this point in the history
…hjitter
  • Loading branch information
martintmk committed May 14, 2023
2 parents 4c62e14 + 24da172 commit 594c914
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 29 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ jobs:
restore-keys: ${{ runner.os }}-nuget-

- name: Initialize CodeQL
uses: github/codeql-action/init@f3feb00acb00f31a6f60280e6ace9ca31d91c76a # v2.3.2
uses: github/codeql-action/init@29b1f65c5e92e24fe6b6647da1eaabe529cec70f # v2.3.3
with:
languages: ${{ matrix.language }}

- name: Autobuild
uses: github/codeql-action/autobuild@f3feb00acb00f31a6f60280e6ace9ca31d91c76a # v2.3.2
uses: github/codeql-action/autobuild@29b1f65c5e92e24fe6b6647da1eaabe529cec70f # v2.3.3

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@f3feb00acb00f31a6f60280e6ace9ca31d91c76a # v2.3.2
uses: github/codeql-action/analyze@29b1f65c5e92e24fe6b6647da1eaabe529cec70f # v2.3.3
with:
category: "/language:${{ matrix.language }}"
2 changes: 1 addition & 1 deletion .github/workflows/ossf-scorecard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ jobs:
retention-days: 5

- name: Upload to code-scanning
uses: github/codeql-action/upload-sarif@f3feb00acb00f31a6f60280e6ace9ca31d91c76a # v2.3.2
uses: github/codeql-action/upload-sarif@29b1f65c5e92e24fe6b6647da1eaabe529cec70f # v2.3.3
with:
sarif_file: results.sarif
2 changes: 1 addition & 1 deletion .github/workflows/update-dotnet-sdk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2

- name: Update .NET SDK
uses: martincostello/update-dotnet-sdk@bba9c5d796cf9e84046ae0cb42f7dd33bde568f3 # v2.1.4
uses: martincostello/update-dotnet-sdk@6cfd047b0c2ce72ea0e22708bc1918a3d912c050 # v2.2.0
with:
labels: "dependencies,.NET"
repo-token: ${{ secrets.GITHUB_TOKEN }}
4 changes: 2 additions & 2 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PackageVersion Include="coverlet.msbuild" Version="3.2.0" />
<PackageVersion Include="BenchmarkDotNet" Version="0.13.5" />
<PackageVersion Include="FluentAssertions" Version="6.11.0" />
<PackageVersion Include="GitHubActionsTestLogger" Version="2.0.2" />
<PackageVersion Include="GitHubActionsTestLogger" Version="2.1.0" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="1.0.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.1" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
Expand All @@ -13,7 +13,7 @@
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Moq" Version="4.18.4" />
<PackageVersion Include="Polly.Contrib.WaitAndRetry" Version="1.1.1" />
<PackageVersion Include="ReportGenerator" Version="5.1.19" />
<PackageVersion Include="ReportGenerator" Version="5.1.20" />
<PackageVersion Include="SonarAnalyzer.CSharp" Version="9.0.0.68202" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.435" />
<PackageVersion Include="System.ComponentModel.Annotations" Version="4.5.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ public void Ctor_EnsureDefaults()
{
ResilienceStrategyRegistryOptions<object> options = new();

options.KeyFormatter.Should().NotBeNull();
options.KeyFormatter(null!).Should().Be("");
options.KeyFormatter("ABC").Should().Be("ABC");
options.StrategyKeyFormatter.Should().NotBeNull();
options.StrategyKeyFormatter(null!).Should().Be("");
options.StrategyKeyFormatter("ABC").Should().Be("ABC");

options.BuilderNameFormatter.Should().NotBeNull();
options.BuilderNameFormatter(null!).Should().Be("");
options.BuilderNameFormatter("ABC").Should().Be("ABC");
}
}
35 changes: 21 additions & 14 deletions src/Polly.Core.Tests/Registry/ResilienceStrategyRegistryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@ namespace Polly.Core.Tests.Registry;

public class ResilienceStrategyRegistryTests
{
private readonly ResilienceStrategyRegistryOptions<StrategyId> _options;

private Action<ResilienceStrategyBuilder> _callback = _ => { };

public ResilienceStrategyRegistryTests() => _options = new()
{
BuilderFactory = () =>
{
var builder = new ResilienceStrategyBuilder();
_callback(builder);
return builder;
},
StrategyComparer = StrategyId.Comparer,
BuilderComparer = StrategyId.BuilderComparer
};

[Fact]
public void Ctor_Default_Ok()
{
Expand Down Expand Up @@ -121,18 +135,21 @@ public void AddBuilder_GetStrategy_EnsureCalled()
[Fact]
public void AddBuilder_EnsureStrategyKey()
{
_options.BuilderNameFormatter = k => k.BuilderName;
_options.StrategyKeyFormatter = k => k.InstanceName;

var called = false;
var registry = CreateRegistry();
registry.TryAddBuilder(StrategyId.Create("A"), (key, builder) =>
registry.TryAddBuilder(StrategyId.Create("A"), (_, builder) =>
{
builder.AddStrategy(new TestResilienceStrategy());
builder.BuilderName.Should().Be("A");
builder.Properties.TryGetValue(TelemetryUtil.StrategyKey, out var val).Should().BeTrue();
val.Should().Be(key.ToString());
val.Should().Be("Instance1");
called = true;
});

registry.Get(StrategyId.Create("A", "Instance1"));

called.Should().BeTrue();
}

Expand Down Expand Up @@ -175,16 +192,6 @@ public void TryAdd_Twice_SecondNotAdded()

private ResilienceStrategyRegistry<StrategyId> CreateRegistry()
{
return new ResilienceStrategyRegistry<StrategyId>(new ResilienceStrategyRegistryOptions<StrategyId>
{
BuilderFactory = () =>
{
var builder = new ResilienceStrategyBuilder();
_callback(builder);
return builder;
},
StrategyComparer = StrategyId.Comparer,
BuilderComparer = StrategyId.BuilderComparer
});
return new ResilienceStrategyRegistry<StrategyId>(_options);
}
}
9 changes: 6 additions & 3 deletions src/Polly.Core/Registry/ResilienceStrategyRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public sealed class ResilienceStrategyRegistry<TKey> : ResilienceStrategyProvide
private readonly Func<ResilienceStrategyBuilder> _activator;
private readonly ConcurrentDictionary<TKey, Action<TKey, ResilienceStrategyBuilder>> _builders;
private readonly ConcurrentDictionary<TKey, ResilienceStrategy> _strategies;
private readonly Func<TKey, string> _keyFormatter;
private readonly Func<TKey, string> _strategyKeyFormatter;
private readonly Func<TKey, string> _builderNameFormatter;

/// <summary>
/// Initializes a new instance of the <see cref="ResilienceStrategyRegistry{TKey}"/> class with the default comparer.
Expand All @@ -44,7 +45,8 @@ public ResilienceStrategyRegistry(ResilienceStrategyRegistryOptions<TKey> option
_activator = options.BuilderFactory;
_builders = new ConcurrentDictionary<TKey, Action<TKey, ResilienceStrategyBuilder>>(options.BuilderComparer);
_strategies = new ConcurrentDictionary<TKey, ResilienceStrategy>(options.StrategyComparer);
_keyFormatter = options.KeyFormatter;
_strategyKeyFormatter = options.StrategyKeyFormatter;
_builderNameFormatter = options.BuilderNameFormatter;
}

/// <summary>
Expand Down Expand Up @@ -89,7 +91,8 @@ public override bool TryGet(TKey key, [NotNullWhen(true)] out ResilienceStrategy
strategy = _strategies.GetOrAdd(key, key =>
{
var builder = _activator();
builder.Properties.Set(TelemetryUtil.StrategyKey, _keyFormatter(key));
builder.BuilderName = _builderNameFormatter(key);
builder.Properties.Set(TelemetryUtil.StrategyKey, _strategyKeyFormatter(key));
configure(key, builder);
return builder.Build();
});
Expand Down
26 changes: 24 additions & 2 deletions src/Polly.Core/Registry/ResilienceStrategyRegistryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,30 @@ public class ResilienceStrategyRegistryOptions<TKey>
public IEqualityComparer<TKey> BuilderComparer { get; set; } = EqualityComparer<TKey>.Default;

/// <summary>
/// Gets or sets the formatter that is used by the registry to format the keys as a string.
/// Gets or sets the formatter that is used by the registry to format the <typeparamref name="TKey"/> to a string that
/// represents the strategy key.
/// </summary>
/// <remarks>
/// By default, the formatter uses the <see cref="object.ToString"/> method.
/// <para>
/// Use custom formatter for composite keys in case you want to have different metric values for a builder and strategy key.
/// In general, strategies can have the same builder name and different strategy keys.
/// </para>
/// </remarks>
[Required]
public Func<TKey, string> StrategyKeyFormatter { get; set; } = (key) => key?.ToString() ?? string.Empty;

/// <summary>
/// Gets or sets the formatter that is used by the registry to format the <typeparamref name="TKey"/> to a string that
/// represents the builder name.
/// </summary>
/// <remarks>
/// By default, the formatter uses the <see cref="object.ToString"/> method.
/// <para>
/// Use custom formatter for composite keys in case you want to have different metric values for a builder and strategy key.
/// In general, strategies can have the same builder name and different strategy keys.
/// </para>
/// </remarks>
[Required]
public Func<TKey, string> KeyFormatter { get; set; } = (key) => key?.ToString() ?? string.Empty;
public Func<TKey, string> BuilderNameFormatter { get; set; } = (key) => key?.ToString() ?? string.Empty;
}

0 comments on commit 594c914

Please sign in to comment.