How to migrate to v8 when using wait and retry with enumerable sleep durations #1717
-
For example, given the following: public sealed class BggService : IBggService
{
public BggService(ILogger<BggService> logger)
{
Logger = logger;
FlurlClient = new FlurlClient("https://boardgamegeek.com")
{
Settings = { BeforeCall = call => { Logger.LogDebug($"{call.Request.Verb} {call.Request.Url}"); } }
};
RetryPolicy = Policy.Handle<FlurlHttpException>(ex => ex.Call.HttpResponseMessage.StatusCode == HttpStatusCode.TooManyRequests)
.OrResult<IFlurlResponse>(response => response.ResponseMessage.StatusCode == HttpStatusCode.Accepted)
.WaitAndRetryAsync(EnumerateDelay(), (response, _) =>
{
if (response.Exception is FlurlHttpException ex)
{
Logger.LogWarning($"{ex.Call.HttpResponseMessage.StatusCode:D} {ex.Call.HttpResponseMessage.StatusCode}");
}
else
{
Logger.LogWarning($"{response.Result.ResponseMessage.StatusCode:D} {response.Result.ResponseMessage.StatusCode}");
}
});
}
private static IEnumerable<TimeSpan> EnumerateDelay()
{
return Enumerable.Range(2, 5).Select(seconds => TimeSpan.FromSeconds(Math.Pow(2, seconds)))
.Concat(Enumerable.Repeat(TimeSpan.FromMinutes(1), int.MaxValue));
}
} I don't see a way to migrate the enumerable sleep durations: ResiliencePipeline = new ResiliencePipelineBuilder<IFlurlResponse>().AddRetry(new RetryStrategyOptions<IFlurlResponse>
{
ShouldHandle = new PredicateBuilder<IFlurlResponse>()
.Handle<FlurlHttpException>(ex => ex.Call.HttpResponseMessage.StatusCode == HttpStatusCode.TooManyRequests)
.HandleResult(response => response.ResponseMessage.StatusCode == HttpStatusCode.Accepted),
DelayGenerator = args => new ValueTask<TimeSpan?>(), //TODO was enumerable
OnRetry = args =>
{
if (args.Outcome.Exception is FlurlHttpException ex)
{
Logger.LogWarning($"{ex.Call.HttpResponseMessage.StatusCode:D} {ex.Call.HttpResponseMessage.StatusCode}");
}
else if (args.Outcome.Result is { } response)
{
Logger.LogWarning($"{response.ResponseMessage.StatusCode:D} {response.ResponseMessage.StatusCode}");
}
return default;
}
}).Build(); Thoughts? |
Beta Was this translation helpful? Give feedback.
Answered by
peter-csala
Oct 23, 2023
Replies: 2 comments 3 replies
-
Looking at your code, I think you want something like this to be functionally equivalent: DelayGenerator = args => args.AttemptNumber switch
{
< 5 => new(TimeSpan.FromSeconds(Math.Pow(2, args.AttemptNumber + 2))),
_ => new(TimeSpan.FromMinutes(1)),
}, |
Beta Was this translation helpful? Give feedback.
2 replies
-
For any arbitrary var delaysEnumerator = EnumerateDelay().GetEnumerator();
ResiliencePipeline = new ...
{
DelayGenerator = args => ValueTask.FromResult<TimeSpan?>(delaysEnumerator.MoveNext() ? delaysEnumerator.Current : null),
...
}).Build(); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
gitfool
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For any arbitrary
EnumerateDelay
you can do the following: