Giter VIP home page Giter VIP logo

polly-contrib / polly.contrib.waitandretry Goto Github PK

View Code? Open in Web Editor NEW
131.0 6.0 12.0 190 KB

Polly.Contrib.WaitAndRetry is an extension library for Polly containing helper methods for a variety of wait-and-retry strategies.

License: Other

Batchfile 0.24% C# 92.83% PowerShell 6.93%
resilience resiliency resiliency-patterns retry-strategies retry-intervals retry-policies retry-pattern dotnet fault-tolerance fault-handler

polly.contrib.waitandretry's People

Contributors

dtterastar avatar grant-d avatar hyrmn avatar reisenberger avatar wolan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

polly.contrib.waitandretry's Issues

Error in the exponential backoff docs?

In the readme here: https://github.com/Polly-Contrib/Polly.Contrib.WaitAndRetry/blob/master/README.md?plain=1

I found this:

var delay = Backoff.ExponentialBackoff(TimeSpan.FromMilliseconds(100), retryCount: 5, factor: 4);

It goes on to say:

The upper for this retry with a growth factor of four is 25,600ms. Care and a calculator should be used when changing the factor.

I eventually built this out in a spreadsheet because care a calculator wasn't working. ;) Shouldn't the sequence be:

1: 100ms
2: 1600ms
3: 8100ms
4: 25600ms
5: 62500ms (TL;DR: 100ms * 5 ^ 4)

...Making the upper 62,500ms? Please let me know if this is calculated differently.

In any event, keep up the great work!

No package on nuget?

I read (and sort of understood) the genesis of this lib over on Polly #530. I love love that all of that jitter work has found its own home. But, when I click through on the nuget badge to https://www.nuget.org/packages/Polly.Contrib.WaitAndRetry, I get an oops page. Since there's nothing in the search, I assume this package isn't currently published? What is the recommended consumption model?

How can i use the DecorrelatedJitterBackoffV2 method for infinite retries?

I am trying to implement the policy with DecorrelatedJitterBackoffV2 method:

private static void SetPolicies()
{
var delay = Backoff.DecorrelatedJitterBackoffV2(medianFirstRetryDelay: TimeSpan.FromSeconds(1), retryCount: 5);
_policy = Policy.Handle().WaitAndRetryForeverAsync(retryAttempt => TimeSpan.FromMilliseconds(Math.Pow(2, retryAttempt)),
(exception, timespan, context) =>
{
Log.Information( exception.Message);
});
}

But it has no been possible to include the delay because of the number of attempts. I mean, i need to use the delay generated in the method WaitAndRetryForeverAsync. is it possible?

Thanks in advance.

DecorrelatedJitterBackoffV2 without exponential side

Is your feature request related to a specific problem? Or an existing feature? Please describe.

Similar to existing feature.

Describe your proposed or preferred solution:

Expose a new backoff strategy, in this package, similar to Backoff.DecorrelatedJitterV2.cs but that does not include the exponential aspect in the returned sleep durations.

Describe any alternative options you've considered:

A naive implementation:

public static IEnumerable<TimeSpan> ConstantJitterBackoff(TimeSpan averageDelay, int retryCount, double jitterFactor = 0.5)
{
    for (var i = 0; i < retryCount; i++)
    {
        // Calculate random jitter
        var jitter = ((Random.Shared.NextDouble() * 2) - 1) * jitterFactor;

        // Apply jitter factor to the averageDelay
        var delayWithJitter = TimeSpan.FromTicks((long)(averageDelay.Ticks * (1 + jitter)));

        yield return delayWithJitter;
    }
}

Any additional info?

This would be useful for scenarios where we want to retry with some randomness but always around a specific interval.

Add how-to-use documentation

Add ReadMe documentation. It should ideally cover:

  • the package overall purpose
  • syntax example for each backoff strategy

( Example readme - not perfect - sure it can also be done better ๐Ÿ™‚ )

Delays generated by DecorrelatedJitterBackoffV2 are always shorter than delays generated by ExponentialBackoff

Summary:

Given that DecorrelatedJitterBackoffV2 uses takes the median of the inital delay, I had expected that the sum of all delay times would sometimes be higher than the sum of all delay times generated by ExponentialBackoff. This does not seem to be the case. Maybe I am misunderstanding? Can we be guaranteed that the sum of the DecorrelatedJitterBackoffV2 delays will always be less than the sum of the ExponentialBackoff delays?

I want to know as I am trying to understand the maximum amount of time that an operation could take including timeouts, delays and retries.

Here are the inputs:

initialDelay: 300ms
retryCount: 3

First generate a list of delays using ExponentialBackoff. Sum the total of all of these delays. (2,100ms)
Next generate lists of delays using DecorrelatedJitterBackoffV2. Sum the total of all of these delays and compare to the sum from ExponentialBackoff.



Expected behavior:

Sometimes the sum of the delays generated by DecorrelatedJitterBackoffV2 is greater than the sum of the delays generated by ExponentialBackoff.



Actual behaviour:

The sum of the delays generated by DecorrelatedJitterBackoffV2 is never greater than the sum of the delays generated by ExponentialBackoff.

Initial commit

Get the initial project structure in place, with the basic code & units working too.

Currently Remora.Discord uses WaitAndRetry for Discord based sockets, however frequently we get issues of the sockets going into invalid states.

Summary:
Currently when using Discord Bots sometimes the sockets get into an invalid state, however it is most of the time frequently in about every few hours.

Expected behavior:

For the socket to not go into invalid state ('Aborted') and instead go into CloseSent so that way WebSocketException is not thrown.

Actual behaviour:

Transient error in gateway client: The WebSocket is in an invalid state ('Aborted') for this operation. Valid states are: 'Open, CloseSent'
System.Net.WebSockets.WebSocketException (0x80004005): The WebSocket is in an invalid state ('Aborted') for this operation. Valid states are: 'Open, CloseSent'
   at System.Net.WebSockets.WebSocketValidate.ThrowIfInvalidState(WebSocketState currentState, Boolean isDisposed, WebSocketState[] validStates)
   at System.Net.WebSockets.ManagedWebSocket.ReceiveAsync(ArraySegment`1 buffer, CancellationToken cancellationToken)
--- End of stack trace from previous location ---
   at Polly.Retry.AsyncRetryEngine.ImplementationAsync[TResult](Func`3 action, Context context, CancellationToken cancellationToken, ExceptionPredicates shouldRetryExceptionPredicates, ResultPredicates`1 shouldRetryResultPredicates, Func`5 onRetryAsync, Int32 permittedRetryCount, IEnumerable`1 sleepDurationsEnumerable, Func`4 sleepDurationProvider, Boolean continueOnCapturedContext)
   at Polly.AsyncPolicy`1.ExecuteAsync(Func`3 action, Context context, CancellationToken cancellationToken, Boolean continueOnCapturedContext)
   at Remora.Discord.Gateway.Transport.WebSocketPayloadTransportService.ReceivePayloadAsync(CancellationToken ct)
   at Remora.Discord.Gateway.Transport.WebSocketPayloadTransportService.ReceivePayloadAsync(CancellationToken ct)
   at Remora.Discord.Gateway.DiscordGatewayClient.GatewayReceiverAsync(CancellationToken disconnectRequested)

Steps / Code to reproduce the problem:

I sadly personally never used WaitAndRetry myself but I think Asking @Nihlus for example code that can reproduce it can help narrow down the cause.

Set up CI

Set up the build system & policies

Add strong name to the assembly

Is your feature request related to a specific problem? Or an existing feature? Please describe.
My request is related to a warning I get when using the nuget package ina strong named assembly - "Referenced assembly 'Polly.Contrib.WaitAndRetry, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have a strong name.".


Describe your proposed or preferred solution:
Sign the assembly.


Describe any alternative options you've considered:
Create another nuget package with strong name.

Custom routine for last attempt (giving up)

We want to log retries and 'giving up' with its own message.
I now implemented this with a condition in the Retry:
if (attempt < MaxAttempts) {normal behavior} else {custom log message}
with MaxAttempts one higher than the actual number of attempts.

I'm not sure what the most elegant solution would be. Maybe a fluent method like GiveUpWith() after Retry?

DecorrelatedJitterBackoffV2 returns negative TimeSpan with large number of retries

When using DecorrelatedJitterBackoffV2 with a large number of retries (i.e. 50), the formula starts returning negative TimeSpans. This is due to the prev value being larger than next.

Expected behavior:
DecorrelatedJitterBackoffV2 should never return an negative TimeSpan

Actual behaviour:
DecorrelatedJitterBackoffV2 returns a negative TimeSpan after a large number of retries

Steps / Code to reproduce the problem:
See the following c# fiddle for steps to reproduce:
https://dotnetfiddle.net/fAWqnz

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.