Request timeouts

Set the route-level timeout with an HTTPRoute or AgentgatewayPolicy. To ensure that your apps are available even if they are temporarily unavailable, you can use timeouts alongside Retries.

Before you begin

  1. Set up an agentgateway proxy.
  2. Install the httpbin sample app.

Set up timeouts

Specify timeouts for a specific route.

  1. Configure a timeout for specific routes by using the Kubernetes Gateway API-native configuration in an HTTPRoute or by using an AgentgatewayPolicy.

    1. Configure the HTTPRoute. In the following example, you set a timeout of 2 seconds for the /delay path of the httpbin app.

      kubectl apply -f- <<EOF
      apiVersion: gateway.networking.k8s.io/v1
      kind: HTTPRoute
      metadata:
        name: httpbin-timeout
        namespace: httpbin
      spec:
        hostnames:
        - timeout.example
        parentRefs:
        - name: agentgateway-proxy
          namespace: agentgateway-system
        rules:
        - matches: 
          - path:
              type: PathPrefix
              value: /delay
          backendRefs:
          - name: httpbin
            port: 8000
            namespace: httpbin
          timeouts:
            request: 2s
      EOF
    2. Verify that the gateway proxy is configured to apply the timeout to a request.

      1. Port-forward the gateway proxy on port 15000.

        kubectl port-forward deploy/agentgateway-proxy -n agentgateway-system 15000
      2. Find the route configuration for the cluster in the config dump. Verify that the timeout policy is set as you configured it. In the output for the following jq command, the delay prefix is included because it has a timeout set.

        Example jq command:

        curl -s http://localhost:15000/config_dump | jq '[.binds[].listeners | to_entries[] | .value.routes | to_entries[] | select(.value.inlinePolicies[]? | has("timeout")) | .value] | .[0]'

        Example output:

        http://localhost:15000/config_dump
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        
        {
           "key": "httpbin/httpbin-timeout.0.0.http",
           "name": "httpbin-timeout",
           "namespace": "httpbin",
           "hostnames": [
             "timeout.example"
           ],
           "matches": [
             {
               "path": {
                 "pathPrefix": "/delay"
               }
             }
           ],
           "backends": [
             {
               "weight": 1,
               "service": {
                 "name": "httpbin/httpbin.httpbin.svc.cluster.local",
                 "port": 8000
               }
             }
           ],
           "inlinePolicies": [
             {
               "timeout": {
                 "requestTimeout": "2s"
               }
             }
           ]
        }
    1. Configure the HTTPRoute. In the following example, you set a timeout of 2 seconds for the /delay path of the httpbin app and add an HTTPRoute rule name to the path. You use the rule name later to apply the timeout to a particular route.

      kubectl apply -n httpbin -f- <<EOF
      apiVersion: gateway.networking.k8s.io/v1
      kind: HTTPRoute
      metadata:
        name: httpbin-timeout
        namespace: httpbin
      spec:
        hostnames:
        - timeout.example
        parentRefs:
        - name: agentgateway-proxy
          namespace: agentgateway-system
        rules:
        - matches: 
          - path:
              type: PathPrefix
              value: /delay
          backendRefs:
          - kind: Service
            name: httpbin
            port: 8000
          name: timeout
      EOF
    2. Create an AgentgatewayPolicy with your timeout settings and use the targetRefs.sectionName to apply the timeout to a specific HTTPRoute rule. In this example, you apply the policy to the timeout rule that points to the /delay path in your HTTPRoute resource.

      kubectl apply -f- <<EOF
      apiVersion: agentgateway.dev/v1alpha1
      kind: AgentgatewayPolicy
      metadata:
        name: timeout
        namespace: httpbin
      spec:
        targetRefs:
        - kind: HTTPRoute
          group: gateway.networking.k8s.io
          name: httpbin-timeout
          sectionName: timeout
        traffic:
          timeouts:
            request: 2s
      EOF
    3. Find the route configuration for the cluster in the config dump. Verify that the timeout policy is set as you configured it.

      Example jq command:

      curl -s http://localhost:15000/config_dump | jq '[.policies[] | select(.policy.traffic.timeout?)] | .[0]'

      Example output:

      http://localhost:15000/config_dump
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      
      {
         "key": "traffic/httpbin/timeout:timeout:httpbin/httpbin-timeout/timeout",
         "name": {
           "kind": "AgentgatewayPolicy",
           "name": "timeout",
           "namespace": "httpbin"
         },
         "target": {
           "route": {
             "name": "httpbin-timeout",
             "namespace": "httpbin",
             "ruleName": "timeout"
           }
         },
         "policy": {
           "traffic": {
             "phase": "route",
             "timeout": {
               "requestTimeout": "2s"
             }
           }
         }
      }
    1. Create an HTTPRoute that configures a route to the /delay path of the httpbin app.

      kubectl apply -n httpbin -f- <<EOF
      apiVersion: gateway.networking.k8s.io/v1
      kind: HTTPRoute
      metadata:
        name: httpbin-timeout
        namespace: httpbin
      spec:
        hostnames:
        - timeout.example
        parentRefs:
        - name: agentgateway-proxy
          namespace: agentgateway-system
        rules:
        - matches: 
          - path:
              type: PathPrefix
              value: /delay
          backendRefs:
          - kind: Service
            name: httpbin
            port: 8000
      EOF
    2. Create an AgentgatewayPolicy with your timeout settings and use the targetRefs.sectionName to apply the timeout to a Gateway listener.

      kubectl apply -f- <<EOF
      apiVersion: agentgateway.dev/v1alpha1
      kind: AgentgatewayPolicy
      metadata:
        name: timeout
        namespace: agentgateway-system
      spec:
        targetRefs:
        - kind: Gateway
          group: gateway.networking.k8s.io
          name: agentgateway-proxy
          sectionName: http
        traffic:
          timeouts:
            request: 2s
      EOF
    3. Find the route configuration for the cluster in the config dump. Verify that the timeout policy is set as you configured it.

      Example jq command:

      curl -s http://localhost:15000/config_dump | jq '[.policies[] | select(.policy.traffic.timeout?)] | .[0]'

      Example output:

      http://localhost:15000/config_dump
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      
      {
        "key": "traffic/agentgateway-system/timeout:timeout:agentgateway-system/agentgateway-proxy/http",
        "name": {
          "kind": "AgentgatewayPolicy",
          "name": "timeout",
          "namespace": "agentgateway-system"
        },
        "target": {
          "gateway": {
            "gatewayName": "agentgateway-proxy",
            "gatewayNamespace": "agentgateway-system",
            "listenerName": "http"
          }
        },
        "policy": {
          "traffic": {
            "phase": "route",
            "timeout": {
              "requestTimeout": "2s"
            }
          }
        }
      }

  2. Send a request along the /delay path of the httpbin. This path delays requests for the number of seconds that you specify. In this example, you delay the request by 1 second. Because the delay is shorter than the timeout that you configured, the request succeeds and a 200 HTTP response code is returned.

    curl -vi http://$INGRESS_GW_ADDRESS:80/delay/1 -H "host: timeout.example:80"
    curl -vi localhost:8080/delay/1 -H "host: timeout.example"

    Example output:

    ...
    < HTTP/1.1 200 OK
    ...
    {
       "args": {},
       "headers": {
         "Accept": [
           "*/*"
         ],
         "Host": [
           "timeout.example"
         ],
         "User-Agent": [
           "curl/8.7.1"
         ]
       },
       "origin": "10.244.0.2:37150",
       "url": "http://timeout.example/delay/1",
       "data": "",
       "files": null,
       "form": null,
       "json": null
    }
  3. Review the logs of the httpbin app and verify that you can see the successful request.

    kubectl logs -n agentgateway-system \
    -l gateway.networking.k8s.io/gateway-name=agentgateway-proxy \
    --tail=1 | grep -E 'timeout.example' 

    Example output:

    info	request gateway=agentgateway-system/agentgateway-proxy
    listener=http route=httpbin/timeout endpoint=10.244.0.13:8080
    src.addr=127.0.0.1:34300 http.method=GET http.host=timeout.example
    http.path=/delay/1 http.version=HTTP/1.1 http.status=200
    protocol=http duration=0ms
  4. Repeat the request along the /delay path. This time, you use a delay that is longer than the request timeout that you previously specified and therefore simulates an app that is slow to respond. Verify that the request times out and that you get back a 504 HTTP response code.

    curl -vi http://$INGRESS_GW_ADDRESS:80/delay/5 -H "host: timeout.example:80"
    curl -vi localhost:8080/delay/5 -H "host: timeout.example"

    Example output:

    ...
    < HTTP/1.1 504 Gateway Timeout
    ...
    request timeout%    
  5. In the httpbin logs, verify that the request took longer than 2 seconds, which triggered the request timeout.

    kubectl logs -n agentgateway-system \
    -l gateway.networking.k8s.io/gateway-name=agentgateway-proxy \
    --tail=1 | grep -E 'timeout.example' 

    Example output:

    info	request gateway=agentgateway-system/agentgateway-proxy listener=http
    route=httpbin/httpbin-timeout endpoint=10.244.0.21:8080 src.addr=127.0.0.1:43640
    http.method=GET http.host=timeout.example http.path=/delay/5 http.version=HTTP/1.1
    http.status=504 protocol=http error="upstream call timeout" duration=2001ms

Cleanup

You can remove the resources that you created in this guide. Run the following commands.

  1. Delete the HTTPRoute resource.
kubectl delete httproute httpbin-timeout -n httpbin
  1. If you created an AgentgatewayPolicy, delete it from the namespace you created it in.
kubectl delete AgentgatewayPolicy timeout -n httpbin
kubectl delete AgentgatewayPolicy timeout -n agentgateway-system
Agentgateway assistant

Ask me anything about agentgateway configuration, features, or usage.

Note: AI-generated content might contain errors; please verify and test all returned information.

↑↓ navigate select esc dismiss

What could be improved?