Microsoft C# .NET YARP 504 Gateway error and Failover

2.6k views Asked by At

I am using Microsoft C# .NET YARP as an API gateway in front of ASP.NET Core Web API (latest version) with Swagger and Kestrel. YARP is used for intern redirections to many microservices.

Questions:

  1. I am facing 504 http API gateway error for requests that execute more than approx. 2 minutes from swagger UI. How do I set an increased timeout?

Ex: https://URL/swagger/index.html => Execute POST FUNCTION => YARP_API_GATEWAY => SERVICE_API_PORT

  1. If an internal service destination (endpoint) is down for any reason, how do I configure "Failover" to another destination?

Ex. configuration:

   "ReverseProxy": {
    "Routes": [
      {
        "RouteId": "Service1",
        "ClusterId": "ServiceCluster1",
        "Match": {
          "Path": "/api/Action1/{**rest}"
        },
        "Transforms": [
          { "RequestHeadersCopy": "true" },
          { "RequestHeaderOriginalHost": "true" }
        ]
      },
      {
        "RouteId": "Service2",
        "ClusterId": "ServiceCluster2",
        "Match": {
          "Path": "/api/Action2/{**rest}"
        },
        "Transforms": [
          { "RequestHeadersCopy": "true" },
          { "RequestHeaderOriginalHost": "true" }
        ]
      ],
    "Clusters": {
      "ServiceCluster1": {
        "Destinations": {
          "ServiceCluster1/destination1": {
            "Address": "http://localhost:5001/"
          },
          "ServiceCluster1/destination2": {
            "Address": "http://localhost:5002/"
          }
        }
      },
      "ServiceCluster2": {
        "Destinations": {
          "ServiceCluster2/destination1": {
            "Address": "http://localhost:5003/"
          },
          "ServiceCluster2/destination2": {
            "Address": "http://localhost:5004/"
          }
        }
      },
  }
1

There are 1 answers

6
Peter Wishart On

I've not used YARP but going from the docs it looks like:

  1. Default timeout for an outgoing HTTP request is 100 seconds. You can set the timeout per cluster via setting the HttpMessageInvoker timeout, e.g. add the following to the cluster config within each cluster:

         "HttpRequest": {
             "ActivityTimeout": "00:05:00"
         },
    
  2. You can use passive health checks to avoid destinations after a failed request (again, set at cluster level):

     "HealthCheck": {
       "Passive": {
         "Enabled": "true",
         "Policy": "TransportFailureRate",
         "ReactivationPeriod": "00:10:00"
       }
     },
    

For a faster response to the destination being down, you can configure regular active health checks.