Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to dispose linked resources on pipeline disposal #1511

Merged
merged 3 commits into from
Aug 24, 2023

Conversation

martintmk
Copy link
Contributor

@martintmk martintmk commented Aug 24, 2023

Details on the issue fix or feature implementation

Allows disposal of resources attached to the pipeline or to attach any cleanup actions:

var provider = new ServiceCollection()
    .AddResiliencePipeline("my-pipeline", (builder, context) =>
    {
        var limiter = new ConcurrencyLimiter(new ConcurrencyLimiterOptions
        {
            PermitLimit = 1,
            QueueLimit = 1
        });

        builder.AddRateLimiter(new RateLimiterStrategyOptions
        {
            RateLimiter = args => limiter.AcquireAsync(1, args.Context.CancellationToken)
        });

        // when the pipeline instance is disposed, limiter is disposed too
        context.OnPipelineDisposed(() => limiter.Dispose());
    });

This is not inclusive to rate limiters as any disposable resource created when configuring the pipeline can be now freed after the pipeline is disposed. This prevents memory leaks and improves the performance.

Contributes to #1507 (comment)

Confirm the following

  • I started this PR by branching from the head of the default branch
  • I have targeted the PR to merge into the default branch
  • I have included unit tests for the issue/feature
  • I have successfully run a local build

@martintmk martintmk added the v8 Issues related to the new version 8 of the Polly library. label Aug 24, 2023
@martintmk martintmk added this to the v8.0.0 milestone Aug 24, 2023
@codecov
Copy link

codecov bot commented Aug 24, 2023

Codecov Report

Merging #1511 (b5ef91d) into main (32a3390) will increase coverage by 0.07%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #1511      +/-   ##
==========================================
+ Coverage   83.93%   84.01%   +0.07%     
==========================================
  Files         274      275       +1     
  Lines        6511     6542      +31     
  Branches     1018     1022       +4     
==========================================
+ Hits         5465     5496      +31     
  Misses        837      837              
  Partials      209      209              
Flag Coverage Δ
linux ?
macos 84.01% <100.00%> (+0.07%) ⬆️
windows 84.01% <100.00%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
src/Polly.Core/Registry/ConfigureBuilderContext.cs 100.00% <100.00%> (ø)
....Core/Registry/RegistryPipelineComponentBuilder.cs 100.00% <100.00%> (ø)
...re/Utils/Pipeline/ComponentWithDisposeCallbacks.cs 100.00% <100.00%> (ø)
...ly.Core/Utils/Pipeline/PipelineComponentFactory.cs 100.00% <100.00%> (ø)
...ependencyInjection/AddResiliencePipelineContext.cs 100.00% <100.00%> (ø)
src/Polly.Testing/ResiliencePipelineExtensions.cs 100.00% <100.00%> (ø)

@martintmk martintmk enabled auto-merge (squash) August 24, 2023 07:49
@martintmk martintmk merged commit 3c76e05 into main Aug 24, 2023
12 checks passed
@martintmk martintmk deleted the mtomka/OnPipelineDisposed branch August 24, 2023 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v8 Issues related to the new version 8 of the Polly library.
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants