-
-
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.
Implement HedgingResilienceStrategy (#1161)
- Loading branch information
Showing
39 changed files
with
2,750 additions
and
43 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
using Polly.Core.Benchmarks; | ||
|
||
namespace Polly.Benchmarks; | ||
|
||
public class HedgingBenchmark | ||
{ | ||
private ResilienceStrategy? _strategy; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_strategy = Helper.CreateHedging(); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public async ValueTask Hedging_Primary() | ||
=> await _strategy!.ExecuteValueTaskAsync(static _ => new ValueTask<string>("primary")).ConfigureAwait(false); | ||
|
||
[Benchmark] | ||
public async ValueTask Hedging_Secondary() | ||
=> await _strategy!.ExecuteValueTaskAsync(static _ => new ValueTask<string>(Helper.Failure)).ConfigureAwait(false); | ||
|
||
[Benchmark] | ||
public async ValueTask Hedging_Primary_AsyncWork() | ||
=> await _strategy!.ExecuteValueTaskAsync( | ||
static async _ => | ||
{ | ||
await Task.Yield(); | ||
return "primary"; | ||
}).ConfigureAwait(false); | ||
|
||
[Benchmark] | ||
public async ValueTask Hedging_Secondary_AsyncWork() | ||
=> await _strategy!.ExecuteValueTaskAsync( | ||
static async _ => | ||
{ | ||
await Task.Yield(); | ||
return Helper.Failure; | ||
}).ConfigureAwait(false); | ||
} |
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,23 @@ | ||
using Polly.Hedging; | ||
|
||
namespace Polly.Core.Benchmarks; | ||
|
||
internal static partial class Helper | ||
{ | ||
public const string Failure = "failure"; | ||
|
||
public static ResilienceStrategy CreateHedging() | ||
{ | ||
return CreateStrategy(builder => | ||
{ | ||
builder.AddHedging(new HedgingStrategyOptions | ||
{ | ||
Handler = new HedgingHandler().SetHedging<string>(handler => | ||
{ | ||
handler.ShouldHandle.HandleResult(Failure); | ||
handler.HedgingActionGenerator = args => () => Task.FromResult("hedged response"); | ||
}) | ||
}); | ||
}); | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
src/Polly.Core.Tests/Hedging/Controller/HedgingControllerTests.cs
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,36 @@ | ||
using Polly.Hedging; | ||
using Polly.Hedging.Utils; | ||
|
||
namespace Polly.Core.Tests.Hedging.Controller; | ||
|
||
public class HedgingControllerTests | ||
{ | ||
[Fact] | ||
public async Task Pooling_Ok() | ||
{ | ||
var handler = new HedgingHandler().SetHedging<int>(handler => handler.HedgingActionGenerator = args => null).CreateHandler(); | ||
var controller = new HedgingController(new HedgingTimeProvider(), handler!, 3); | ||
|
||
var context1 = controller.GetContext(ResilienceContext.Get()); | ||
await PrepareAsync(context1); | ||
|
||
var context2 = controller.GetContext(ResilienceContext.Get()); | ||
await PrepareAsync(context2); | ||
|
||
controller.RentedContexts.Should().Be(2); | ||
controller.RentedExecutions.Should().Be(2); | ||
|
||
context1.Complete(); | ||
context2.Complete(); | ||
|
||
controller.RentedContexts.Should().Be(0); | ||
controller.RentedExecutions.Should().Be(0); | ||
} | ||
|
||
private static async Task PrepareAsync(HedgingExecutionContext context) | ||
{ | ||
await context.LoadExecutionAsync((_, _) => new ValueTask<int>(10), "state"); | ||
await context.TryWaitForCompletedExecutionAsync(HedgingStrategyOptions.InfiniteHedgingDelay); | ||
context.Tasks[0].AcceptOutcome(); | ||
} | ||
} |
Oops, something went wrong.