Add IConcurrentPolicyRegistry interface/implementation, for PolicyRegistry
See original GitHub issueWhy IPolicyRegistry does not have TryAdd() method?
I am seeing KeyAlreadyExistiError when I run this code in multi-threaded test. This is due to multiple requests are trying to add policy at the same time - registry.Add(“TimeoutPolicy”,, retryTimeoutPolicy);
IPolicyRegistry<string> registry = sp.GetRequiredService<IPolicyRegistry<string>>();
IAsyncPolicy<HttpResponseMessage> policy = null;
registry.TryGet<IAsyncPolicy<HttpResponseMessage>>(“TimeoutPolicy”, out policy);
if (policy == null)
{
policy = Policy
.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromMilliseconds(10000));
registry.Add(“TimeoutPolicy”,policy);
}
return policy;
I fixed this issue by replacing .Add with this code - registry[“TimeoutPolicy”] = policy;
What is the recommended way in this scenario?
This could have solved if we have TryAdd method like ConcurrentDictionary. Why IPolicyRegistry does not have TryAdd() method?
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (7 by maintainers)
Top Results From Across the Web
Polly is a .NET resilience and transient-fault-handling ...
Allows different parts of a policy execution to exchange data via the mutable Context travelling with each execution. @ankitbko - Add PolicyRegistry for...
Read more >Proper way to handle multiple services with polly circuit ...
Yes, your best bet is to create a separate policy per endpoint. This is better than doing it per host because an endpoint...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Reviewing PolicyRegistry, it’s already a facade for ConcurrentDictionary, so I think adding a new interface dedicated to concurrent methods seems redundant. Especially since the pattern of including Try methods in the Policy interfaces already exists with TryGet being defined in IReadOnlyPolicyRegistry.
ConcurrentDictionary has a number of methods that don’t seem useful/relevant to this ticket. Based on the existing methods in PolicyRegistry (including TryGet), I’ve added:
These have been committed. Once I’ve added tests for these (probably this weekend), I’ll open a PR.
@reisenberger yes, this code is an existing code(before new extension method available in HttpClientFactory). I will have to change our code to use the new extension method.