0

I get a 504 message back when I make a request through a YARP server. Apparently this is a timing issue between the backend and the YARP server. The backend server responds normally when I address it directly with this request through the browser without a proxy. I have added middleware. If the HTTP status of the response of the backend server is 504, the same request should be sent again. I use the the BasicYARPSample(https://github.com/microsoft/reverse-proxy/tree/release/latest/samples/BasicYarpSample)+ code for the middleware.

My code doesn't work. The command "await next();" in the if-clause throws an exception. I get the following error message:" An unhandled exception has occurred while executing the request. System.InvalidOperationException: The request cannot be forwarded, the response has already started"

Any suggestions?

app.UseEndpoints(endpoints =>
            {
                endpoints.MapReverseProxy(proxyPipeline => {
    
                    proxyPipeline.Use(async (context, next) =>
                    {
                        await next();
                    
                        if ((context.Response.StatusCode>=500) &&(context.Response.StatusCode <= 504))
                        {
                            int maxRetries = 3;
                            TimeSpan retryDelay = TimeSpan.FromSeconds(5);
                            int retries = 0;

                            while (retries < maxRetries)
                            {

                                await Task.Delay(retryDelay);
                                await next();

                                if (!((context.Response.StatusCode >= 500) && (context.Response.StatusCode <= 504)))
                                {
                                    break;
                                }

                                retries++;
                            }
                        }
                        
                    });
                        
                    proxyPipeline.UseSessionAffinity();
                    proxyPipeline.UseLoadBalancing();
                    proxyPipeline.UsePassiveHealthChecks();

                });
            });  ``` 
teca
  • 1
  • 2
  • Proxy is probably using 30 second timeout. So when you try second time the proxy is still trying to connect while your app is timing out after 5 seconds. Is your uri HTTP or HTTPS? The proxy not connecting may be a TLS issue. – jdweng Aug 02 '23 at 12:35
  • Yarp is HTTPS and the backend is HTTP. In production the backend is also HTTPS. – teca Aug 02 '23 at 12:55
  • Code is working locally but not when deployed? This isn't clear in the description. What OS is the deployed. This sound like a TLS issue where you have an older OS in the deployed environment. The certificate for TLS has an encryption mode and older OS may not support the encryption. – jdweng Aug 02 '23 at 12:58
  • The backend and the proxy are not deployed. They are in development stage on the same development machine(Windows 10). When both are deployed, they will be accessible via HTTPS. – teca Aug 02 '23 at 13:06
  • You have an issue between proxy and backend. The proxy to backend connection is not completing in 30 second (default timeout) and the proxy is returning error 504. A connection not completing can be for lots of reasons. One reason is the TLS is failing because the certificate (or encryption) is failing. Your connection is from local machine to proxy with a 5 second timeout and 3 retries. The second retry at 5 seconds fails because the connection from proxy to backend is still waiting for the 30 second timeout. – jdweng Aug 02 '23 at 14:17

0 Answers0