grpc/grpc-dotnet

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

magiino opened this issue · 0 comments

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.");
                                 });
    };