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

If connection is not established, the Polly retry policy i HttpMessageHandlers are not called. #2447

Open
magiino opened this issue May 31, 2024 · 0 comments
Labels
question Further information is requested

Comments

@magiino
Copy link

magiino commented May 31, 2024

Hello,

why is not called Polly policy http message handler when connection could not be established?
I would like to retry even if connection could not be established, the service might be restarting when I'm doing the grpc call.

How can I add http message handler which will be called even during connection?

      serviceCollection
                     .AddGrpcClient<TGrpcClientType>(option =>
                     {
                         option.Address = new Uri(configuration.EndpointAddress);
                         option.CallOptionsActions.Add(ctx =>
                             ctx.CallOptions =
                                 ctx.CallOptions
                                     .WithDeadline(DateTime.UtcNow.Add(configuration.Deadline)))
                                     ;
                     })
                     .AddPolicyHandler(retryFunc);

This sample code was taken from past discussion #686

    Func<HttpRequestMessage, IAsyncPolicy<HttpResponseMessage>> retryFunc = (request) =>
    {
        return Policy.HandleResult<HttpResponseMessage>(r => r.Headers.GetValues("grpc-status").FirstOrDefault() != "0")
                                 .WaitAndRetryAsync(3, (input) => TimeSpan.FromSeconds(3 + input), (result, timeSpan, retryCount, context) =>
                                 {
                                     var grpcStatus = (StatusCode)int.Parse(result.Result.Headers.GetValues("grpc-status").FirstOrDefault() ?? "0");
                                     Console.WriteLine($"Request failed with {grpcStatus}. Retry.");
                                 });
    };
@magiino magiino added the question Further information is requested label May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant