-
-
Couldn't load subscription status.
- Fork 1.3k
Allow to dispose linked resources on pipeline disposal #1511
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
43 changes: 43 additions & 0 deletions
43
src/Polly.Core/Utils/Pipeline/ComponentWithDisposeCallbacks.cs
This file contains hidden or 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,43 @@ | ||
| namespace Polly.Utils.Pipeline; | ||
|
|
||
| internal class ComponentWithDisposeCallbacks : PipelineComponent | ||
| { | ||
| private readonly List<Action> _callbacks; | ||
|
|
||
| public ComponentWithDisposeCallbacks(PipelineComponent component, List<Action> callbacks) | ||
| { | ||
| Component = component; | ||
| _callbacks = callbacks; | ||
| } | ||
|
|
||
| internal PipelineComponent Component { get; } | ||
|
|
||
| public override void Dispose() | ||
| { | ||
| ExecuteCallbacks(); | ||
|
|
||
| Component.Dispose(); | ||
| } | ||
|
|
||
| public override ValueTask DisposeAsync() | ||
| { | ||
| ExecuteCallbacks(); | ||
|
|
||
| return Component.DisposeAsync(); | ||
| } | ||
|
|
||
| internal override ValueTask<Outcome<TResult>> ExecuteCore<TResult, TState>( | ||
| Func<ResilienceContext, TState, ValueTask<Outcome<TResult>>> callback, | ||
| ResilienceContext context, | ||
| TState state) => Component.ExecuteCore(callback, context, state); | ||
|
|
||
| private void ExecuteCallbacks() | ||
| { | ||
| foreach (var callback in _callbacks) | ||
| { | ||
| callback(); | ||
| } | ||
|
|
||
| _callbacks.Clear(); | ||
martincostello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
46 changes: 46 additions & 0 deletions
46
test/Polly.Core.Tests/Utils/Pipeline/ComponentWithDisposeCallbacksTests.cs
This file contains hidden or 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,46 @@ | ||
| using NSubstitute; | ||
| using Polly.Utils.Pipeline; | ||
|
|
||
| namespace Polly.Core.Tests.Utils.Pipeline; | ||
|
|
||
| public class ComponentWithDisposeCallbacksTests | ||
| { | ||
| [InlineData(true)] | ||
| [InlineData(false)] | ||
| [Theory] | ||
| public async Task Dispose_Ok(bool isAsync) | ||
| { | ||
| // Arrange | ||
| var called1 = 0; | ||
| var called2 = 0; | ||
|
|
||
| var callbacks = new List<Action> | ||
| { | ||
| () => called1++, | ||
| () => called2++ | ||
| }; | ||
| var component = Substitute.For<PipelineComponent>(); | ||
| var sut = new ComponentWithDisposeCallbacks(component, callbacks); | ||
|
|
||
| // Act | ||
| if (isAsync) | ||
| { | ||
| await sut.DisposeAsync(); | ||
| await sut.DisposeAsync(); | ||
| await component.Received(2).DisposeAsync(); | ||
| } | ||
| else | ||
| { | ||
| sut.Dispose(); | ||
| #pragma warning disable S3966 // Objects should not be disposed more than once | ||
| sut.Dispose(); | ||
| #pragma warning restore S3966 // Objects should not be disposed more than once | ||
| component.Received(2).Dispose(); | ||
| } | ||
|
|
||
| // Assert | ||
| callbacks.Should().BeEmpty(); | ||
| called1.Should().Be(1); | ||
| called2.Should().Be(1); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
test/Polly.Core.Tests/Utils/Pipeline/PipelineComponentFactoryTests.cs
This file contains hidden or 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,26 @@ | ||
| using NSubstitute; | ||
| using Polly.Utils.Pipeline; | ||
|
|
||
| namespace Polly.Core.Tests.Utils.Pipeline; | ||
|
|
||
| public class PipelineComponentFactoryTests | ||
| { | ||
| [Fact] | ||
| public void WithDisposableCallbacks_NoCallbacks_ReturnsOriginalComponent() | ||
| { | ||
| var component = Substitute.For<PipelineComponent>(); | ||
| var result = PipelineComponentFactory.WithDisposableCallbacks(component, new List<Action>()); | ||
| result.Should().BeSameAs(component); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void PipelineComponentFactory_Should_Return_WrapperComponent_With_Callbacks() | ||
| { | ||
| var component = Substitute.For<PipelineComponent>(); | ||
| var callbacks = new List<Action> { () => { } }; | ||
|
|
||
| var result = PipelineComponentFactory.WithDisposableCallbacks(component, callbacks); | ||
|
|
||
| result.Should().BeOfType<ComponentWithDisposeCallbacks>(); | ||
| } | ||
| } |
This file contains hidden or 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,58 @@ | ||
| using System.Threading.RateLimiting; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Polly.RateLimiting; | ||
| using Polly.Registry; | ||
|
|
||
| namespace Polly.Extensions.Tests; | ||
|
|
||
| public class DisposablePipelineTests | ||
| { | ||
| [Fact] | ||
| public void DisposePipeline_EnsureLinkedResourcesDisposedToo() | ||
| { | ||
| var limiters = new List<RateLimiter>(); | ||
|
|
||
| var provider = new ServiceCollection() | ||
| .AddResiliencePipeline("my-pipeline", (builder, context) => | ||
| { | ||
| var limiter = new ConcurrencyLimiter(new ConcurrencyLimiterOptions | ||
| { | ||
| PermitLimit = 1, | ||
| QueueLimit = 1 | ||
| }); | ||
| limiters.Add(limiter); | ||
|
|
||
| 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()); | ||
| }) | ||
| .BuildServiceProvider(); | ||
|
|
||
| limiters.Should().HaveCount(0); | ||
| provider.GetRequiredService<ResiliencePipelineProvider<string>>().GetPipeline("my-pipeline"); | ||
| provider.GetRequiredService<ResiliencePipelineProvider<string>>().GetPipeline("my-pipeline"); | ||
| limiters.Should().HaveCount(1); | ||
| IsDisposed(limiters[0]).Should().BeFalse(); | ||
|
|
||
| provider.Dispose(); | ||
| limiters.Should().HaveCount(1); | ||
| IsDisposed(limiters[0]).Should().BeTrue(); | ||
| } | ||
|
|
||
| private static bool IsDisposed(RateLimiter limiter) | ||
| { | ||
| try | ||
| { | ||
| limiter.AcquireAsync(1).AsTask().GetAwaiter().GetResult(); | ||
| return false; | ||
| } | ||
| catch (ObjectDisposedException) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.