Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Http.Resilience] Timeout can't filter based on request? #5048

Open
marcinjahn opened this issue Mar 16, 2024 · 3 comments
Open

[Http.Resilience] Timeout can't filter based on request? #5048

marcinjahn opened this issue Mar 16, 2024 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-resilience

Comments

@marcinjahn
Copy link

marcinjahn commented Mar 16, 2024

It seems that Microsoft.Extensions.Http.Resilience replaces the older Microsoft.Extensions.Http.Polly extensions.

One thing I noticed is that the newer package lost the functionality to filter whether to apply timeouts or not based on the HttpRequestMessage object.

In the Microsoft.Extensions.Http.Polly package, I could do:

 services
     .AddHttpClient<MyClient>()
     .AddPolicyHandler((serviceProvider, request) =>
           request.RequestUri?.AbsolutePath.Contains("something", StringComparison.OrdinalIgnoreCase) is true
                ? Policy.TimeoutAsync(timeSpan)
                : Policy.NoOpAsync());

I don't see such option with the new APIs (which seem to directly use the Polly API). Some strategies, like Retry, do allow to access the HttpRequestMessage object, in the RetryStrategyOptions's ShouldHandle. The TimeoutStrategyOptions doesn't seem to have any configuration like that.

Am I missing something and there is some way to filter timeout strategy based on the request message, or is this functionality just not there? Will it be added?

@geeknoid geeknoid added area-resilience api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Mar 16, 2024
@geeknoid
Copy link
Member

@martintmk Any feedback on this?

@martintmk
Copy link
Contributor

Hey @marcinjahn

You can use TimeoutGenerator to dynamically enable/disable timeout. What you need to do is to store a flag into ResilienceContext that determines whether the timeout is disabled or not. If disabled, return Timeout.InfiniteTimespan.

See https://www.pollydocs.org/strategies/timeout.html#defaults for more details.

@sommmen
Copy link

sommmen commented Apr 7, 2024

Hey @marcinjahn

You can use TimeoutGenerator to dynamically enable/disable timeout. What you need to do is to store a flag into ResilienceContext that determines whether the timeout is disabled or not. If disabled, return Timeout.InfiniteTimespan.

See https://www.pollydocs.org/strategies/timeout.html#defaults for more details.

Could you add some pseudo-code? i'm still a bit puzzeld.
I'm using .net aspire which by default uses;

builder.Services.ConfigureHttpClientDefaults(http =>
{
    // Turn on resilience by default
    http.AddStandardResilienceHandler();

    // Turn on service discovery by default
    http.UseServiceDiscovery();
});

This is great, but i have some third-party services for which i definitely don't want to retry.
I need some way to be able to disable the retry for a single client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-resilience
Projects
None yet
Development

No branches or pull requests

4 participants