Skip to content

Commit

Permalink
Move some internals into Pipeline folder (#1497)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored Aug 17, 2023
1 parent 149ed82 commit c21a307
Show file tree
Hide file tree
Showing 36 changed files with 498 additions and 496 deletions.
3 changes: 2 additions & 1 deletion src/Polly.Core/Registry/ResiliencePipelineRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using Polly.Telemetry;
using Polly.Utils.Pipeline;

namespace Polly.Registry;

Expand Down Expand Up @@ -288,7 +289,7 @@ private static PipelineComponent CreatePipelineComponent<TBuilder>(
return pipeline;
}

return PipelineComponent.CreateReloadable(pipeline, context.ReloadTokenProducer(), () => factory().BuildPipelineComponent(), telemetry);
return PipelineComponentFactory.CreateReloadable(pipeline, context.ReloadTokenProducer(), () => factory().BuildPipelineComponent(), telemetry);
}

private GenericRegistry<TResult> GetGenericRegistry<TResult>()
Expand Down
8 changes: 4 additions & 4 deletions src/Polly.Core/ResiliencePipeline.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async ValueTask ExecuteAsync<TState>(

InitializeAsyncContext(context);

var outcome = await ExecuteCore(
var outcome = await Component.ExecuteCore(
static async (context, state) =>
{
try
Expand Down Expand Up @@ -59,7 +59,7 @@ public async ValueTask ExecuteAsync(

InitializeAsyncContext(context);

var outcome = await ExecuteCore(
var outcome = await Component.ExecuteCore(
static async (context, state) =>
{
try
Expand Down Expand Up @@ -98,7 +98,7 @@ public async ValueTask ExecuteAsync<TState>(

try
{
var outcome = await ExecuteCore(
var outcome = await Component.ExecuteCore(
static async (context, state) =>
{
try
Expand Down Expand Up @@ -139,7 +139,7 @@ public async ValueTask ExecuteAsync(

try
{
var outcome = await ExecuteCore(
var outcome = await Component.ExecuteCore(
static async (context, state) =>
{
try
Expand Down
10 changes: 5 additions & 5 deletions src/Polly.Core/ResiliencePipeline.AsyncT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ValueTask<Outcome<TResult>> ExecuteOutcomeAsync<TResult, TState>(

InitializeAsyncContext<TResult>(context);

return ExecuteCore(callback, context, state);
return Component.ExecuteCore(callback, context, state);
}

/// <summary>
Expand All @@ -52,7 +52,7 @@ public async ValueTask<TResult> ExecuteAsync<TResult, TState>(

InitializeAsyncContext<TResult>(context);

var outcome = await ExecuteCore(
var outcome = await Component.ExecuteCore(
static async (context, state) =>
{
try
Expand Down Expand Up @@ -87,7 +87,7 @@ public async ValueTask<TResult> ExecuteAsync<TResult>(

InitializeAsyncContext<TResult>(context);

var outcome = await ExecuteCore(
var outcome = await Component.ExecuteCore(
static async (context, state) =>
{
try
Expand Down Expand Up @@ -126,7 +126,7 @@ public async ValueTask<TResult> ExecuteAsync<TResult, TState>(

try
{
var outcome = await ExecuteCore(
var outcome = await Component.ExecuteCore(
static async (context, state) =>
{
try
Expand Down Expand Up @@ -167,7 +167,7 @@ public async ValueTask<TResult> ExecuteAsync<TResult>(

try
{
var outcome = await ExecuteCore(
var outcome = await Component.ExecuteCore(
static async (context, state) =>
{
try
Expand Down
12 changes: 6 additions & 6 deletions src/Polly.Core/ResiliencePipeline.Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Execute<TState>(

InitializeSyncContext(context);

ExecuteCoreSync(
Component.ExecuteCoreSync(
static (context, state) =>
{
try
Expand Down Expand Up @@ -55,7 +55,7 @@ public void Execute(

InitializeSyncContext(context);

ExecuteCoreSync(
Component.ExecuteCoreSync(
static (context, state) =>
{
try
Expand Down Expand Up @@ -91,7 +91,7 @@ public void Execute<TState>(

try
{
ExecuteCoreSync(
Component.ExecuteCoreSync(
static (context, state) =>
{
try
Expand Down Expand Up @@ -129,7 +129,7 @@ public void Execute(

try
{
ExecuteCoreSync(
Component.ExecuteCoreSync(
static (context, state) =>
{
try
Expand Down Expand Up @@ -168,7 +168,7 @@ public void Execute<TState>(

try
{
ExecuteCoreSync(
Component.ExecuteCoreSync(
static (_, state) =>
{
try
Expand Down Expand Up @@ -203,7 +203,7 @@ public void Execute(Action callback)

try
{
ExecuteCoreSync(
Component.ExecuteCoreSync(
static (_, state) =>
{
try
Expand Down
12 changes: 6 additions & 6 deletions src/Polly.Core/ResiliencePipeline.SyncT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public TResult Execute<TResult, TState>(

InitializeSyncContext<TResult>(context);

return ExecuteCoreSync(
return Component.ExecuteCoreSync(
static (context, state) =>
{
try
Expand Down Expand Up @@ -59,7 +59,7 @@ public TResult Execute<TResult>(

InitializeSyncContext<TResult>(context);

return ExecuteCoreSync(
return Component.ExecuteCoreSync(
static (context, state) =>
{
try
Expand Down Expand Up @@ -94,7 +94,7 @@ public TResult Execute<TResult>(

try
{
return ExecuteCoreSync(
return Component.ExecuteCoreSync(
static (context, state) =>
{
try
Expand Down Expand Up @@ -130,7 +130,7 @@ public TResult Execute<TResult>(Func<TResult> callback)

try
{
return ExecuteCoreSync(
return Component.ExecuteCoreSync(
static (_, state) =>
{
try
Expand Down Expand Up @@ -168,7 +168,7 @@ public TResult Execute<TResult, TState>(Func<TState, TResult> callback, TState s

try
{
return ExecuteCoreSync(
return Component.ExecuteCoreSync(
static (_, state) =>
{
try
Expand Down Expand Up @@ -210,7 +210,7 @@ public TResult Execute<TResult, TState>(

try
{
return ExecuteCoreSync(
return Component.ExecuteCoreSync(
static (context, state) =>
{
try
Expand Down
23 changes: 2 additions & 21 deletions src/Polly.Core/ResiliencePipeline.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Polly.Utils.Pipeline;

namespace Polly;

/// <summary>
Expand Down Expand Up @@ -25,25 +27,4 @@ internal ResiliencePipeline(PipelineComponent component, DisposeBehavior dispose
internal PipelineComponent Component { get; }

internal ComponentDisposeHelper DisposeHelper { get; }

internal ValueTask<Outcome<TResult>> ExecuteCore<TResult, TState>(
Func<ResilienceContext, TState, ValueTask<Outcome<TResult>>> callback,
ResilienceContext context,
TState state) => Component.ExecuteCore(callback, context, state);

private Outcome<TResult> ExecuteCoreSync<TResult, TState>(
Func<ResilienceContext, TState, Outcome<TResult>> callback,
ResilienceContext context,
TState state)
{
return ExecuteCore(
static (context, state) =>
{
var result = state.callback(context, state.state);

return new ValueTask<Outcome<TResult>>(result);
},
context,
(callback, state)).GetResult();
}
}
3 changes: 2 additions & 1 deletion src/Polly.Core/ResiliencePipelineBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using Polly.Telemetry;
using Polly.Utils.Pipeline;

namespace Polly;

Expand Down Expand Up @@ -123,7 +124,7 @@ internal PipelineComponent BuildPipelineComponent()
throw new InvalidOperationException("The resilience pipeline must contain unique resilience strategies.");
}

return PipelineComponent.CreateComposite(components, new ResilienceStrategyTelemetry(source, TelemetryListener), TimeProvider);
return PipelineComponentFactory.CreateComposite(components, new ResilienceStrategyTelemetry(source, TelemetryListener), TimeProvider);
}

private PipelineComponent CreateComponent(Entry entry)
Expand Down
11 changes: 6 additions & 5 deletions src/Polly.Core/ResiliencePipelineBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using Polly.Utils.Pipeline;

namespace Polly;

Expand Down Expand Up @@ -27,7 +28,7 @@ public static TBuilder AddPipeline<TBuilder>(this TBuilder builder, ResiliencePi
Guard.NotNull(builder);
Guard.NotNull(pipeline);

builder.AddPipelineComponent(_ => PipelineComponent.FromPipeline(pipeline), EmptyOptions.Instance);
builder.AddPipelineComponent(_ => PipelineComponentFactory.FromPipeline(pipeline), EmptyOptions.Instance);
return builder;
}

Expand All @@ -49,7 +50,7 @@ public static ResiliencePipelineBuilder<TResult> AddPipeline<TResult>(this Resil
Guard.NotNull(builder);
Guard.NotNull(pipeline);

builder.AddPipelineComponent(_ => PipelineComponent.FromPipeline(pipeline), EmptyOptions.Instance);
builder.AddPipelineComponent(_ => PipelineComponentFactory.FromPipeline(pipeline), EmptyOptions.Instance);
return builder;
}

Expand All @@ -72,7 +73,7 @@ public static TBuilder AddStrategy<TBuilder>(this TBuilder builder, Func<Strateg
Guard.NotNull(factory);
Guard.NotNull(options);

builder.AddPipelineComponent(context => PipelineComponent.FromStrategy(factory(context)), options);
builder.AddPipelineComponent(context => PipelineComponentFactory.FromStrategy(factory(context)), options);
return builder;
}

Expand All @@ -95,7 +96,7 @@ public static ResiliencePipelineBuilder AddStrategy(
Guard.NotNull(factory);
Guard.NotNull(options);

builder.AddPipelineComponent(context => PipelineComponent.FromStrategy(factory(context)), options);
builder.AddPipelineComponent(context => PipelineComponentFactory.FromStrategy(factory(context)), options);
return builder;
}

Expand All @@ -119,7 +120,7 @@ public static ResiliencePipelineBuilder<TResult> AddStrategy<TResult>(
Guard.NotNull(factory);
Guard.NotNull(options);

builder.AddPipelineComponent(context => PipelineComponent.FromStrategy(factory(context)), options);
builder.AddPipelineComponent(context => PipelineComponentFactory.FromStrategy(factory(context)), options);
return builder;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Polly.Core/ResiliencePipelineT.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Polly.Utils.Pipeline;

namespace Polly;

/// <summary>
Expand Down
37 changes: 37 additions & 0 deletions src/Polly.Core/Utils/Pipeline/BridgeComponent.TResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace Polly.Utils.Pipeline;

[DebuggerDisplay("{Strategy}")]
internal sealed class BridgeComponent<T> : BridgeComponentBase
{
public BridgeComponent(ResilienceStrategy<T> strategy)
: base(strategy) => Strategy = strategy;

public ResilienceStrategy<T> Strategy { get; }

internal override ValueTask<Outcome<TResult>> ExecuteCore<TResult, TState>(
Func<ResilienceContext, TState, ValueTask<Outcome<TResult>>> callback,
ResilienceContext context,
TState state)
{
// Check if we can cast directly, thus saving some cycles and improving the performance
if (callback is Func<ResilienceContext, TState, ValueTask<Outcome<T>>> casted)
{
return TaskHelper.ConvertValueTask<T, TResult>(
Strategy.ExecuteCore(casted, context, state),
context);
}
else
{
var valueTask = Strategy.ExecuteCore(
static async (context, state) =>
{
var outcome = await state.callback(context, state.state).ConfigureAwait(context.ContinueOnCapturedContext);
return outcome.AsOutcome<T>();
},
context,
(callback, state));

return TaskHelper.ConvertValueTask<T, TResult>(valueTask, context);
}
}
}
15 changes: 15 additions & 0 deletions src/Polly.Core/Utils/Pipeline/BridgeComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Polly.Utils.Pipeline;

[DebuggerDisplay("{Strategy}")]
internal sealed class BridgeComponent : BridgeComponentBase
{
public BridgeComponent(ResilienceStrategy strategy)
: base(strategy) => Strategy = strategy;

public ResilienceStrategy Strategy { get; }

internal override ValueTask<Outcome<TResult>> ExecuteCore<TResult, TState>(
Func<ResilienceContext, TState, ValueTask<Outcome<TResult>>> callback,
ResilienceContext context,
TState state) => Strategy.ExecuteCore(callback, context, state);
}
Loading

0 comments on commit c21a307

Please sign in to comment.