Polly | NET resilience and transient-fault-handling library | Testing library

 by   App-vNext C# Version: 8.0.0-alpha.1 License: BSD-3-Clause

kandi X-RAY | Polly Summary

kandi X-RAY | Polly Summary

Polly is a C# library typically used in Testing applications. Polly has no bugs, it has no vulnerabilities, it has a Permissive License and it has medium support. You can download it from GitHub.

Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, Rate-limiting and Fallback in a fluent and thread-safe manner. Polly targets .NET Standard 1.1 (coverage: .NET Core 1.0, Mono, Xamarin, UWP, WP8.1+) and .NET Standard 2.0+ (coverage: .NET Core 2.0+, .NET Core 3.0, and later Mono, Xamarin and UWP targets). The NuGet package also includes direct targets for .NET Framework 4.6.1 and 4.7.2. For versions supporting earlier targets such as .NET4.0 and .NET3.5, see the supported targets grid.
Support
    Quality
      Security
        License
          Reuse

            kandi-support Support

              Polly has a medium active ecosystem.
              It has 11966 star(s) with 1061 fork(s). There are 358 watchers for this library.
              There were 2 major release(s) in the last 12 months.
              There are 108 open issues and 536 have been closed. On average issues are closed in 54 days. There are 5 open pull requests and 0 closed requests.
              It has a neutral sentiment in the developer community.
              The latest version of Polly is 8.0.0-alpha.1

            kandi-Quality Quality

              Polly has no bugs reported.

            kandi-Security Security

              Polly has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.

            kandi-License License

              Polly is licensed under the BSD-3-Clause License. This license is Permissive.
              Permissive licenses have the least restrictions, and you can use them in most projects.

            kandi-Reuse Reuse

              Polly releases are available to install and integrate.
              Installation instructions are not available. Examples and code snippets are available.

            Top functions reviewed by kandi - BETA

            kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.
            Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Polly
            Get all kandi verified functions for this library.

            Polly Key Features

            No Key Features are available at this moment for Polly.

            Polly Examples and Code Snippets

            No Code Snippets are available at this moment for Polly.

            Community Discussions

            QUESTION

            How to use the PolicyHttpMessageHandler as "standalone"?
            Asked 2022-Mar-21 at 09:59

            I'm just trying to create a simple test where I use DelegateHandlers to instantiate a HttpClient without bringing Asp.net Core packages. I have 2 deletage handlers

            • ThrottlingDelegatingHandler
            • PolicyHttpMessageHandler (from Polly package)

            How can I combine both and pass to the HttpClient?

            ...

            ANSWER

            Answered 2022-Mar-21 at 09:59

            You have to set the InnerHandler in your most inner handler to HttpClientHandler like this:

            Source https://stackoverflow.com/questions/71542054

            QUESTION

            Using Polly to do reconnects and timeouts
            Asked 2022-Mar-19 at 18:00

            I'm having a problem using Polly while trying to accomplish the following:

            • Reconnect logic - I tried to create a Polly policy which works when you try to execute StartAsync without Internet connection. However, when it reaches ReceiveLoop, the policy has no longer impact over that method and if our connection stops at that point, it never tries to reconnect back. It simply throws the following exception: Disconnected: The remote party closed the WebSocket connection without completing the close handshake.. Perhaps I should have two policies: one in StartAsync and one in ReceiveLoop, but for some reason it doesn't feel right to me, so that's why I ask the question.

            • Timeouts - I want to add timeouts for each ClientWebSocket method call e.g. ConnectAsync, SendAsync, etc. I'm not so familiar with Polly but I believe this policy automatically does that for us. However, I need someone to confirm that. By timeout, I mean similar logic to _webSocket.ConnectAsync(_url, CancellationToken.None).TimeoutAfter(timeoutMilliseconds), TimeoutAfter implementation can be found here. An example how other repos did it can be found here.

            Simplified, I want to make this class resilient, which means instead of trying to connect to a dead web socket server for 30 seconds without success, no matter what the reason is, it should fail fast -> retry in 10 seconds -> fail fast -> retry again and so on. This wait and retry logic should be repeated until we call StopAsync or dispose the instance.

            You can find the WebSocketDuplexPipe class on GitHub.

            ...

            ANSWER

            Answered 2022-Mar-19 at 18:00

            Let me capture in an answer the essence of our conversation via comments.

            ReceiveLoop with retry

            Your retry policy will exit with success from the ExecuteAsync while you are waiting for the input.ReadAsync to finish. The reason is that you are not awaiting the ReceiveLoop rather you just kick it off in a fire and forget manner.

            In other words, your retry logic will only apply for the StartAsync and the code before the await inside the ReceiveLoop.

            The fix is to move the retry logic inside ReceiveLoop.

            Timeout

            Polly's Timeout policy can use either optimistic or pessimistic strategy. The former one heavily relies on the CancellationToken.

            • So, if you pass for example CancellationToken.None to the ExecuteAsync then you basically says let TimeoutPolicy handle cancellation process.
            • If you pass an already existing token then the decorated Task can be cancelled by the TimeoutPolicy or by the provided token.

            Please bear in mind that it will throw TimeoutRejectedException not OperationCanceledException.

            onTimeoutAsync

            TimeoutAsync has several overloads which can accept one of the two onTimeoutAsync delegates

            Source https://stackoverflow.com/questions/71502940

            QUESTION

            Polly "retry" throws TaskCanceledException on first failure retry attempt
            Asked 2022-Jan-28 at 19:08

            I have implemented Polly in it's own "retry" HttpClient DelegateHandler in a dll written to .NET Standard 2.0. I have the Polly v7.2.3 package. My HttpClient is running separate from an HttpClientFactory since only one instance will ever exist during the short lifetime of the dll.

            My problem is this: The code executes great when my internet is working. However, when I disconnect my internet it throws a TaskCanceledException on the first retry and does not retry any more. Here are the relevant parts of my code...

            inside the ctor of my typed HttpClient:

            ...

            ANSWER

            Answered 2022-Jan-28 at 16:55

            All you need to do is to specify the HttpResponseMessage as a return type when you declare your policy.

            Source https://stackoverflow.com/questions/70896030

            QUESTION

            How to install llvm@13 with Homerew on macOS High Sierra 10.13.6? Got "Built target lldELF" error
            Asked 2022-Jan-10 at 17:20

            Although High Sierra is no longer supported by Homebrew, but I need to install llvm@13 formula as a dependency for other formulas. So I tried to install it this way:

            ...

            ANSWER

            Answered 2021-Nov-26 at 08:27

            Install llvm with debug mode enabled:

            Source https://stackoverflow.com/questions/69906053

            QUESTION

            What Happens When HttpClient.PostAsync is Not Awaited
            Asked 2022-Jan-05 at 09:12

            I'm inspecting some code that seems to throw

            System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at times.

            We use the library Polly for auto retries for HttpClient requests.

            • I'm starting to wonder if it has to do with the HttpClient.PostAsync not being awaited below?
            • Would that cause any connection exhaustion?

            Any guidance would be appreciated.

            ...

            ANSWER

            Answered 2022-Jan-04 at 16:32

            The two code blocks you show here are effectively identical. Internally, Polly will await the delegate anyway, and multiple awaits on a Task don't have any affect.

            However, if you're only using Polly to make resilient HTTP calls with HttpClient, then I would consider using the library itself to handle that. There are examples here https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/implement-http-call-retries-exponential-backoff-polly

            Source https://stackoverflow.com/questions/70581388

            QUESTION

            Polly patterns in C# for a workflow?
            Asked 2021-Dec-28 at 11:37

            I have a workflow like this:

            • call an API method
              • if any exception is thrown but timeout, the program logs that exception and throws it.
            • if timeout is thrown, the program has to call another API method.
              • after calling another API method, if everything goes true, the program returns a result.
              • otherwise the program throws an exception and log it.

            both API methods have the same type of result. I want to implement this with polly policies.

            This is my sample code:

            ...

            ANSWER

            Answered 2021-Jul-29 at 08:25

            If I understand your workflow correctly then you don't need the retry policy at all. The Fallback policy is enough for this.

            So, let suppose that the CallApi and CallAnotherApi are implemented like this:

            Source https://stackoverflow.com/questions/68560615

            QUESTION

            Polly Retry - Pass all execution until a retry is successful
            Asked 2021-Dec-19 at 10:12

            Currently, Polly Retry policy retires all the failed requests independently. So, if there are 10 requests failing and I have set the retry forever policy then it will send 10 more requests every time a retry happens and the server will never heal.

            How to asynchronously pass all failed requests and retry only one request and resume the normal flow if a retry is successful?

            I can't (don't want to) use Circuit Breaker because my service is a Background worker service and Circuit Breaker breaks the whole background service logic.

            ...

            ANSWER

            Answered 2021-Dec-19 at 10:12
            The Problem

            According to my understanding you have a single HttpClient which is used to issue N rate-limited, concurrent requests against the same downstream system.

            You want to handle the following failure scenarios:

            • If there is a transient network issue you want to retry an individual request
            • If the downstream system gets overloaded (so most of the concurrent requests fail) then you want to back off and use only a single request for probing its healthiness
            Option A - Combine CB and Retry

            The Circuit Breaker policy works as a proxy. It tracks the outgoing communication and if there are too much successive failures then it prevents further requests. It does that by short-cutting the requests by throwing an BrokenCircuitException.

            After a certain period of time CB will allow a single request to go out against the downstream system and if it succeeds then it allows all outgoing communication but if it fails then it will short-cut them. Here I have detailed how does CB work.

            You can adjust your retry policy to be aware of this exception. This means that your retry requests will be still issued but will not leave your application domain. Fortunately in Polly you can define multiple triggers for a policy:

            Source https://stackoverflow.com/questions/70405901

            QUESTION

            Need a built in way to add Deadlock Resilience to Dapper for existing repos without altering them
            Asked 2021-Dec-09 at 07:31

            Need to make all the existing repos (around 30+) fault tolerant to deadlock and recover from it with log and wait approach.

            Tried with success: I've answered below a custom SqlResiliencyPolicy using Polly after some research and tailored it to the project.

            But, what I seek: The present way (PFB answered), demands me to either

            1. Wrap all the existing DB calls with await _policy.ExecuteAsync OR
            2. Provide custom overloads which accepts IAsyncPolicy param. and later call the intended method. Sort of extension to IDbConnection:

            public static Task GetAsync(this IDbConnection connection, object primaryKey, IAsyncPolicy policy) => return await _policy.ExecuteAsync(async () => GetAsync (...));

            In both ways, I need to change all my 30+ repos. But, is there a built-in way in dapper/some-other-approaches, where we can

            "configure a Policy in startup and auto-magically all DB calls via dapper become resilient (fall backs to their fault tolerant mechanism) Similar to the ways of http clients resilience where policy is added while you register a client"

            By this: will've code changes to minimum, need not touch repos but only the startup.

            I've a below approach and sort of need an improvement over it.

            ...

            ANSWER

            Answered 2021-Nov-24 at 17:49

            My approach after following some research:

            Source https://stackoverflow.com/questions/70062183

            QUESTION

            Stop Execution on certain condition on Polly 4.3
            Asked 2021-Nov-29 at 15:40

            We started using the Polly library in our legacy WinForms project which still runs on the .NET 4.0 framework (is a requisite).

            The problem is that we have to use version 4.3 of the Polly library and it is difficult to find solutions to the problems because all the documentation we find is about more recent versions of the library.

            For example we can't pass Context values from retry callback to execute because Context is readonly and we can't pass parameters to execute delegate because it use an Action type.

            To all these problems we have find a creative solution but we can't still found a way to stop the execution on certain condition.

            In Polly 5, CancellationToken was introduced for this purpose but I guess there were ways to force the retry to stop in previous versions as well.

            ...

            ANSWER

            Answered 2021-Nov-29 at 15:40

            I think you have tried to solve the problem from the wrong angle.

            Rather than trying to cancel a retry, try to avoid triggering the retry.

            I've created a sample application in dotnetfiddle to make sure that my proposed solution work with Polly version 4.3 as well

            Source https://stackoverflow.com/questions/70152671

            QUESTION

            How do you pass a logger using HttpClientFactory and a PolicyRegistry?
            Asked 2021-Nov-25 at 16:04

            I've been reading this which seems to have a way of doing what I want.

            I've got something similar obviously, but within the PolicyContextExtensions the context.TryGetValue takes PolicyContextItems. This shows as an undefined object for me.

            Where does this come from and how is this set?

            For reference here is the PolicyContextExtensions class:

            ...

            ANSWER

            Answered 2021-Nov-25 at 16:04

            The Polly's Context class is basically a Dictionary.
            Which means you can register any kind of data with an arbitrary string.

            In the tutorial Stephen has created a new context like this:

            Source https://stackoverflow.com/questions/70112792

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network

            Vulnerabilities

            No vulnerabilities reported

            Install Polly

            You can download it from GitHub.

            Support

            For details of supported compilation targets by version, see the supported targets grid.
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
            CLONE
          • HTTPS

            https://github.com/App-vNext/Polly.git

          • CLI

            gh repo clone App-vNext/Polly

          • sshUrl

            git@github.com:App-vNext/Polly.git

          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link