-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[Question]: In what scenario should I use TryAddBuilder() instead of GetOrAddPipeline() for a registry? #2539
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
Comments
It doesn't look like TryAddBuilder is thread safe as calling GetPipeline() with multiple threads can cause issues with dynamic reloading. At best, it seems to reload more often than necessary.
With the above code, the number of registered |
This is typical with the usage of
You're not using it wrong, and the Polly code is safe to be used concurrently, but the code of yours that it will call during the process of creation needs to be safe to use concurrently too. You'll need to adjust your application as appropriate to be safe in this way if it's accessed this way. I can't give a specific recommendation of how to do that, as it depends entirely on what your application as actually does. |
We've ended up restructuring our code to create the pipeline during startup, using My confusion was that we had originally set up the registry once and added a
Perhaps a lock should be introduced so we can call If not, then can we update the documentation that we should be careful on how |
I think we'd prefer resolving this with documentation rather than a
|
Thanks. Documentation would definitely help. I was thinking that adding a lock here, , just right before the getOrAdd is called would be helpful.Since the line before returns the pipeline if found, the lock should only affect calls that are creating the pipeline the first time? Another option is perhaps using Lazy here, https://andrewlock.net/making-getoradd-on-concurrentdictionary-thread-safe-using-lazy/, but that might be a bigger change to the dictionary. |
What are you wanting to achieve?
I'm trying to figure out what the primary difference between TryAddBuilder() and GetOrAddPipeline() when used with the registry.
During most of my testing, using both methods will allow me to update the pipeline whenever the cancellation token is cancelled.
The one thing I've noticed is that GetOrAddPipeline() will always return a single pipeline instance when called with
_registry.GetPipeline("A");
With
TryAddBuilder()
, I'm thinking that it should also return a single cached pipeline instance when_registry.GetPipeline("A")
is called.The primary difference I've noticed so far is that
TryAddBuilder()
can be registered once but when_registry.GetPipeline("A")
is called by multiple threads at once, it seems that our_valueChecker.OnUpdate
will contain more invocations than expected.I think it might be because
Polly/src/Polly.Core/Registry/ResiliencePipelineRegistry.cs
Line 83 in 2d02db1
Polly/src/Polly.Core/Registry/ResiliencePipelineRegistry.cs
Line 125 in 2d02db1
_registry.GetPipeline()
when the pipeline isn't cached and a builder is used,configure
can be executed multiple times as a concurrent dictionary will only save one value. This causes issues with my event handlers as there are now more registered than I expected.What code or approach do you have so far?
Additional context
No response
The text was updated successfully, but these errors were encountered: