-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the V7 -> V8 bridge implementation
- Loading branch information
Showing
11 changed files
with
169 additions
and
65 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
...DotNet.Artifacts/results/Polly.Core.Benchmarks.BridgeBenchmark-report-github.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
``` ini | ||
|
||
BenchmarkDotNet=v0.13.5, OS=Windows 11 (10.0.22621.1702/22H2/2022Update/SunValley2), VM=Hyper-V | ||
Intel Xeon Platinum 8370C CPU 2.80GHz, 1 CPU, 16 logical and 8 physical cores | ||
.NET SDK=7.0.302 | ||
[Host] : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2 | ||
|
||
Job=MediumRun Toolchain=InProcessEmitToolchain IterationCount=15 | ||
LaunchCount=2 WarmupCount=10 | ||
|
||
``` | ||
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio | | ||
|----------------------- |----------:|---------:|---------:|------:|--------:|-------:|----------:|------------:| | ||
| NoOpAsync | 85.38 ns | 1.001 ns | 1.371 ns | 1.00 | 0.00 | 0.0120 | 304 B | 1.00 | | ||
| NullResilienceStrategy | 416.38 ns | 1.924 ns | 2.820 ns | 4.87 | 0.10 | 0.0148 | 376 B | 1.24 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Polly.Core.Benchmarks; | ||
|
||
public class BridgeBenchmark | ||
{ | ||
private IAsyncPolicy<string>? _policy; | ||
private IAsyncPolicy<string>? _policyWrapped; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_policy = Policy.NoOpAsync<string>(); | ||
_policyWrapped = NullResilienceStrategy<string>.Instance.AsAsyncPolicy(); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public Task NoOpAsync() => _policy!.ExecuteAsync(() => Task.FromResult("dummy")); | ||
|
||
[Benchmark] | ||
public Task NullResilienceStrategy() => _policyWrapped!.ExecuteAsync(() => Task.FromResult("dummy")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Collections.Generic; | ||
using Polly.Utils; | ||
|
||
namespace Polly.Core.Tests.Utils; | ||
|
||
public class LegacySupportTests | ||
{ | ||
[Fact] | ||
public void SetProperties_Ok() | ||
{ | ||
var resilienceProperties = new ResilienceProperties(); | ||
var oldProps = resilienceProperties.Options; | ||
var newProps = new Dictionary<string, object?>(); | ||
|
||
resilienceProperties.SetProperties(newProps, out var oldProperties2); | ||
|
||
resilienceProperties.Options.Should().BeSameAs(newProps); | ||
oldProperties2.Should().BeSameAs(oldProps); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.ComponentModel; | ||
|
||
namespace Polly.Utils; | ||
|
||
/// <summary> | ||
/// Legacy support for older versions of Polly. | ||
/// </summary> | ||
/// <remarks> | ||
/// This class is used by the legacy Polly infrastructure and should not be used directly by user code. | ||
/// </remarks> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static class LegacySupport | ||
{ | ||
/// <summary> | ||
/// Changes the underlying properties of a <see cref="ResilienceProperties"/> instance. | ||
/// </summary> | ||
/// <param name="resilienceProperties">The resilience properties.</param> | ||
/// <param name="properties">The properties to use.</param> | ||
/// <param name="oldProperties">The old properties used by <paramref name="resilienceProperties"/>.</param> | ||
/// <remarks> | ||
/// This method is used by the legacy Polly infrastructure and should not be used directly by user code. | ||
/// </remarks> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static void SetProperties( | ||
this ResilienceProperties resilienceProperties, | ||
IDictionary<string, object?> properties, | ||
out IDictionary<string, object?> oldProperties) | ||
{ | ||
Guard.NotNull(resilienceProperties); | ||
Guard.NotNull(properties); | ||
|
||
oldProperties = resilienceProperties.Options; | ||
resilienceProperties.Options = properties; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,26 @@ | ||
using Polly.Utils; | ||
|
||
namespace Polly.Utilities.Wrappers; | ||
|
||
internal static class ResilienceContextFactory | ||
{ | ||
public static readonly ResiliencePropertyKey<Context> ContextKey = new("Polly.Legacy.Context"); | ||
|
||
public static ResilienceContext Create(Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) | ||
public static ResilienceContext Create( | ||
Context context, | ||
CancellationToken cancellationToken, | ||
bool continueOnCapturedContext, | ||
out IDictionary<string, object> oldProperties) | ||
{ | ||
var resilienceContext = ResilienceContext.Get(); | ||
resilienceContext.CancellationToken = cancellationToken; | ||
resilienceContext.ContinueOnCapturedContext = continueOnCapturedContext; | ||
|
||
foreach (var pair in context) | ||
{ | ||
var props = (IDictionary<string, object>)resilienceContext.Properties; | ||
props.Add(pair.Key, pair.Value); | ||
} | ||
|
||
resilienceContext.Properties.Set(ContextKey, context); | ||
resilienceContext.Properties.SetProperties(context, out oldProperties); | ||
|
||
return resilienceContext; | ||
} | ||
|
||
public static void Restore(ResilienceContext context) | ||
public static void Cleanup(ResilienceContext resilienceContext, IDictionary<string, object> oldProperties) | ||
{ | ||
var originalContext = context.GetContext(); | ||
|
||
foreach (var pair in context.Properties) | ||
{ | ||
if (pair.Key == ContextKey.Key) | ||
{ | ||
continue; | ||
} | ||
|
||
originalContext[pair.Key] = pair.Value; | ||
} | ||
|
||
ResilienceContext.Return(context); | ||
resilienceContext.Properties.SetProperties(oldProperties, out _); | ||
ResilienceContext.Return(resilienceContext); | ||
} | ||
|
||
public static Context GetContext(this ResilienceContext resilienceContext) => resilienceContext.Properties.GetValue(ContextKey, null!); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.