Timeouts

Set a maximum time for the gateway to handle a request, including error retries.

About

A timeout is the amount of time (duration) that K8sGateway waits for replies from an upstream service before the service is considered unavailable. This setting can be useful to avoid your apps from hanging or fail if no response is returned in a specific timeframe. With timeouts, calls either succeed or fail within a predicatble timeframe.

The time an app needs to process a request can vary a lot which is why applying the same timeout across services can cause a variety of issues. For example, a timeout that is too long can result in excessive latency from waiting for replies from failing services. On the other hand, a timeout that is too short can result in calls failing unnecessarily while waiting for an operation that needs responses from multiple services.

ℹ️
Timeouts can be configured for a route by using a RouteOption resource. Configuring gateway-level timeouts with a VirtualHostOption resource is not supported.

Before you begin

  1. Follow the Get started guide to install K8sGateway, set up a gateway resource, and deploy the httpbin sample app.

  2. Get the external address of the gateway and save it in an environment variable.

    export INGRESS_GW_ADDRESS=$(kubectl get svc -n gloo-system gloo-proxy-http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}")
    echo $INGRESS_GW_ADDRESS  
    kubectl port-forward deployment/gloo-proxy-http -n gloo-system 8080:8080

Set up timeouts

Use a RouteOption resource to specify timeouts for a specific route.

  1. Create a RouteOption custom resource to specify your timeout rules. In the following example, the request must be completed within 20 seconds.

    kubectl apply -n httpbin -f- <<EOF
    apiVersion: gateway.solo.io/v1
    kind: RouteOption
    metadata:
      name: timeout
      namespace: httpbin
    spec:
      options:
        timeout: '20s'
    EOF
  2. Create an HTTPRoute resource for the httpbin app that references the RouteOption resource that you created.

    kubectl apply -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1beta1
    kind: HTTPRoute
    metadata:
      name: httpbin-timeout
      namespace: httpbin
    spec:
      parentRefs:
      - name: http
        namespace: gloo-system
      hostnames:
        - timeout.example
      rules:
        - filters:
            - type: ExtensionRef
              extensionRef:
                group: gateway.solo.io
                kind: RouteOption
                name: timeout
          backendRefs:
            - name: httpbin
              port: 8000
    EOF
  3. Send a request to the httpbin app on the timout.example domain. Verify that the request succeeds and that you see a X-Envoy-Expected-Rq-Timeout-Ms header. If the header is present, K8sGateway expects requests to the httpbin app to succeed within the set timeout.

    curl -vik http://$INGRESS_GW_ADDRESS:8080/headers -H "host: timeout.example:8080"
    curl -vik localhost:8080/headers -H "host: timeout.example"

    Example output for a successful response:

    {
     "headers": {
       "Accept": [
         "*/*"
       ],
       "Host": [
         "timeout.example:8080"
       ],
       "User-Agent": [
         "curl/7.77.0"
       ],
       "X-Envoy-Expected-Rq-Timeout-Ms": [
         "20000"
       ],
       "X-Forwarded-Proto": [
         "http"
       ],
       "X-Request-Id": [
         "0ae53bc3-2644-44f2-8603-158d2ccf9f78"
       ]
     }
    }
    
  4. Optional: Remove the resources that you created.

    kubectl delete httproute httpbin-timeout -n httpbin
    kubectl delete routeoption timeout -n httpbin