question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add IConcurrentPolicyRegistry interface/implementation, for PolicyRegistry

See original GitHub issue

Why 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:closed
  • Created 4 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
aerotogcommented, Oct 18, 2019

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:

  • TryAdd
  • TryRemove

These have been committed. Once I’ve added tests for these (probably this weekend), I’ll open a PR.

1reaction
shagiltcommented, May 29, 2019

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found