Skip to content

Commit

Permalink
Avoid capturing where possible. (#1609)
Browse files Browse the repository at this point in the history
* Avoid capturing where possible.

* Post dotnet mdsnippets run
  • Loading branch information
IEvangelist authored Sep 22, 2023
1 parent 5cd3368 commit a01e9c0
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README_V8.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ResiliencePipeline pipeline = new ResiliencePipelineBuilder()
.Build(); // Builds the resilience pipeline
// Execute the pipeline asynchronously
await pipeline.ExecuteAsync(async cancellationToken => { /*Your custom logic here */ }, cancellationToken);
await pipeline.ExecuteAsync(static async cancellationToken => { /*Your custom logic here */ }, cancellationToken);
```
<!-- endSnippet -->

Expand Down Expand Up @@ -84,7 +84,7 @@ var pipelineProvider = serviceProvider.GetRequiredService<ResiliencePipelineProv
ResiliencePipeline pipeline = pipelineProvider.GetPipeline("my-pipeline");

// Execute the pipeline
await pipeline.ExecuteAsync(async token =>
await pipeline.ExecuteAsync(static async token =>
{
// Your custom logic here
});
Expand Down
3 changes: 2 additions & 1 deletion docs/advanced/dependency-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ ResiliencePipelineProvider<string> pipelineProvider = serviceProvider.GetRequire
ResiliencePipeline pipeline = pipelineProvider.GetPipeline("my-key");

// Use it
await pipeline.ExecuteAsync(async cancellation => await Task.Delay(100, cancellation));
await pipeline.ExecuteAsync(
static async cancellation => await Task.Delay(100, cancellation));
```
<!-- endSnippet -->

Expand Down
2 changes: 1 addition & 1 deletion docs/advanced/resilience-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ ResiliencePipeline pipeline = new ResiliencePipelineBuilder()

// Execute the resilience pipeline asynchronously
await pipeline.ExecuteAsync(
async context =>
static async context =>
{
// Insert your execution logic here
},
Expand Down
2 changes: 1 addition & 1 deletion docs/general.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ await pipeline.ExecuteAsync(
ResilienceContext context = ResilienceContextPool.Shared.Get(cancellationToken: cancellationToken);

await pipeline.ExecuteAsync(
async context => await MyMethodAsync(context.CancellationToken),
static async context => await MyMethodAsync(context.CancellationToken),
context);
```
<!-- endSnippet -->
Expand Down
4 changes: 2 additions & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ResiliencePipeline pipeline = new ResiliencePipelineBuilder()
.Build(); // Builds the resilience pipeline
// Execute the pipeline asynchronously
await pipeline.ExecuteAsync(async cancellationToken => { /*Your custom logic here */ }, cancellationToken);
await pipeline.ExecuteAsync(static async cancellationToken => { /*Your custom logic here */ }, cancellationToken);
```
<!-- endSnippet -->

Expand Down Expand Up @@ -55,7 +55,7 @@ var pipelineProvider = serviceProvider.GetRequiredService<ResiliencePipelineProv
ResiliencePipeline pipeline = pipelineProvider.GetPipeline("my-pipeline");

// Execute the pipeline
await pipeline.ExecuteAsync(async token =>
await pipeline.ExecuteAsync(static async token =>
{
// Your custom logic here
});
Expand Down
4 changes: 2 additions & 2 deletions docs/migration-v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pipeline.Execute(() =>
});

// Asynchronous execution is also supported with the same pipeline instance
await pipeline.ExecuteAsync(async cancellationToken =>
await pipeline.ExecuteAsync(static async cancellationToken =>
{
// your code here
},
Expand Down Expand Up @@ -135,7 +135,7 @@ pipelineT.Execute(() =>
});

// Asynchronous execution
await pipelineT.ExecuteAsync(async cancellationToken =>
await pipelineT.ExecuteAsync(static async cancellationToken =>
{
// your code here
return await GetResponseAsync(cancellationToken);
Expand Down
2 changes: 1 addition & 1 deletion docs/strategies/fallback.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ Nest `ExecuteAsync` calls
```cs
var result = await fallback.ExecuteAsync(async (CancellationToken outerCT) =>
{
return await timeout.ExecuteAsync(async (CancellationToken innerCT) =>
return await timeout.ExecuteAsync(static async (CancellationToken innerCT) =>
{
return await CallExternalSystem(innerCT);
}, outerCT);
Expand Down
6 changes: 3 additions & 3 deletions docs/strategies/hedging.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ internal class HedgingHandler : DelegatingHandler

try
{
return await _pipeline.ExecuteAsync(async context =>
return await _pipeline.ExecuteAsync(async cxt =>
{
// Allow the pipeline to use request message that was stored in the context.
// This allows replacing the request message with a new one in the resilience pipeline.
request = context.Properties.GetValue(ResilienceKeys.RequestMessage, request);
request = cxt.Properties.GetValue(ResilienceKeys.RequestMessage, request);

return await base.SendAsync(request, context.CancellationToken);
return await base.SendAsync(request, cxt.CancellationToken);
},
context);
}
Expand Down
10 changes: 7 additions & 3 deletions docs/strategies/retry.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,12 @@ var builder = new ResiliencePipelineBuilder()
builder.AddTimeout(TimeSpan.FromMinutes(1));

var pipeline = builder.Build();
await pipeline.ExecuteAsync(async (ct) =>
await pipeline.ExecuteAsync(static async (httpClient, ct) =>
{
var stream = await httpClient.GetStreamAsync(new Uri("endpoint"), ct);
var foo = await JsonSerializer.DeserializeAsync<Foo>(stream, cancellationToken: ct);
});
},
httpClient);
```
<!-- endSnippet -->

Expand All @@ -409,7 +410,10 @@ var retry = new ResiliencePipelineBuilder()
})
.Build();

var stream = await retry.ExecuteAsync(async (ct) => await httpClient.GetStreamAsync(new Uri("endpoint"), ct));
var stream = await retry.ExecuteAsync(
static async (httpClient, ct) =>
await httpClient.GetStreamAsync(new Uri("endpoint"), ct),
httpClient);

var timeout = new ResiliencePipelineBuilder<Foo>()
.AddTimeout(TimeSpan.FromMinutes(1))
Expand Down
3 changes: 2 additions & 1 deletion src/Snippets/Docs/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public static async Task AddResiliencePipeline()
ResiliencePipeline pipeline = pipelineProvider.GetPipeline("my-key");

// Use it
await pipeline.ExecuteAsync(async cancellation => await Task.Delay(100, cancellation));
await pipeline.ExecuteAsync(
static async cancellation => await Task.Delay(100, cancellation));

#endregion
}
Expand Down
2 changes: 1 addition & 1 deletion src/Snippets/Docs/Fallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private static ValueTask<HttpResponseMessage> ActionCore()
#region fallback-anti-pattern-3
var result = await fallback.ExecuteAsync(async (CancellationToken outerCT) =>
{
return await timeout.ExecuteAsync(async (CancellationToken innerCT) =>
return await timeout.ExecuteAsync(static async (CancellationToken innerCT) =>
{
return await CallExternalSystem(innerCT);
}, outerCT);
Expand Down
2 changes: 1 addition & 1 deletion src/Snippets/Docs/General.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ await pipeline.ExecuteAsync(
ResilienceContext context = ResilienceContextPool.Shared.Get(cancellationToken: cancellationToken);

await pipeline.ExecuteAsync(
async context => await MyMethodAsync(context.CancellationToken),
static async context => await MyMethodAsync(context.CancellationToken),
context);

#endregion
Expand Down
6 changes: 3 additions & 3 deletions src/Snippets/Docs/Hedging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

try
{
return await _pipeline.ExecuteAsync(async context =>
return await _pipeline.ExecuteAsync(async cxt =>
{
// Allow the pipeline to use request message that was stored in the context.
// This allows replacing the request message with a new one in the resilience pipeline.
request = context.Properties.GetValue(ResilienceKeys.RequestMessage, request);
request = cxt.Properties.GetValue(ResilienceKeys.RequestMessage, request);
return await base.SendAsync(request, context.CancellationToken);
return await base.SendAsync(request, cxt.CancellationToken);
},
context);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Snippets/Docs/Migration.Policies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static async Task Strategies()
});

// Asynchronous execution is also supported with the same pipeline instance
await pipeline.ExecuteAsync(async cancellationToken =>
await pipeline.ExecuteAsync(static async cancellationToken =>
{
// your code here
},
Expand Down Expand Up @@ -111,7 +111,7 @@ await pipeline.ExecuteAsync(async cancellationToken =>
});

// Asynchronous execution
await pipelineT.ExecuteAsync(async cancellationToken =>
await pipelineT.ExecuteAsync(static async cancellationToken =>
{
// your code here
return await GetResponseAsync(cancellationToken);
Expand Down
4 changes: 2 additions & 2 deletions src/Snippets/Docs/Readme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static async Task QuickStart()
.Build(); // Builds the resilience pipeline

// Execute the pipeline asynchronously
await pipeline.ExecuteAsync(async cancellationToken => { /*Your custom logic here */ }, cancellationToken);
await pipeline.ExecuteAsync(static async cancellationToken => { /*Your custom logic here */ }, cancellationToken);

#endregion
}
Expand Down Expand Up @@ -48,7 +48,7 @@ public static async Task QuickStartDi()
ResiliencePipeline pipeline = pipelineProvider.GetPipeline("my-pipeline");

// Execute the pipeline
await pipeline.ExecuteAsync(async token =>
await pipeline.ExecuteAsync(static async token =>
{
// Your custom logic here
});
Expand Down
2 changes: 1 addition & 1 deletion src/Snippets/Docs/ResilienceContextUsage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static async Task Usage()

// Execute the resilience pipeline asynchronously
await pipeline.ExecuteAsync(
async context =>
static async context =>
{
// Insert your execution logic here
},
Expand Down
10 changes: 7 additions & 3 deletions src/Snippets/Docs/Retry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,12 @@ public static async Task AntiPattern_6()
builder.AddTimeout(TimeSpan.FromMinutes(1));

var pipeline = builder.Build();
await pipeline.ExecuteAsync(async (ct) =>
await pipeline.ExecuteAsync(static async (httpClient, ct) =>
{
var stream = await httpClient.GetStreamAsync(new Uri("endpoint"), ct);
var foo = await JsonSerializer.DeserializeAsync<Foo>(stream, cancellationToken: ct);
});
},
httpClient);

#endregion
}
Expand All @@ -337,7 +338,10 @@ public static async Task Pattern_6()
})
.Build();

var stream = await retry.ExecuteAsync(async (ct) => await httpClient.GetStreamAsync(new Uri("endpoint"), ct));
var stream = await retry.ExecuteAsync(
static async (httpClient, ct) =>
await httpClient.GetStreamAsync(new Uri("endpoint"), ct),
httpClient);

var timeout = new ResiliencePipelineBuilder<Foo>()
.AddTimeout(TimeSpan.FromMinutes(1))
Expand Down

0 comments on commit a01e9c0

Please sign in to comment.