App-vNext / Polly

Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. From version 6.0.1, Polly targets .NET Standard 1.1 and 2.0+.
https://www.thepollyproject.org
BSD 3-Clause "New" or "Revised" License
13.44k stars 1.23k forks source link

Will Polly create new request on retry or use same request? #587

Closed shagilt closed 5 years ago

shagilt commented 5 years ago

Summary: What are you wanting to achieve? We are adding new handler to add header in the request before sending request. I see when retry happens, I see header is already present from previous retry. Will Polly use same request in each retry?

What code or approach do you have so far?
This is the extension I am using

    public static IHttpClientBuilder AddPolicyHandler(this IHttpClientBuilder builder, Func<IServiceProvider, HttpRequestMessage, IAsyncPolicy<HttpResponseMessage>> policySelector);



reisenberger commented 5 years ago

TL;DR

Will Polly use same request in each retry?

Yes


A Polly retry policy in a delegating handler created by HttpClientFactory does not create a new HttpRequestMessage; it just retries using the HttpRequestMessage which has been passed to it. The lifetime/scope of HttpRequestMessage is controlled by whatever created it - HttpClient, or your code if you create the HttpRequestMessage and pass it into an overload on HttpClient which takes HttpRequestMessage as an input parameter.

To understand how HttpClientFactory uses the policy in a delegating handler, here you can see the latest source code of the DelegatingHandler which HttpClientFactory creates, to wrap the Polly policy.


The extension overload you quote

public static IHttpClientBuilder AddPolicyHandler(this IHttpClientBuilder builder, Func<IServiceProvider, HttpRequestMessage, IAsyncPolicy> policySelector);

takes the existing HttpRequestMessage as an input parameter, and returns (selects or creates) a policy using it.

shagilt commented 5 years ago

Thank you @reisenberger