Skip to content

HTTP 超时

原文:https://gateway-api.sigs.k8s.io/guides/user-guides/http-timeouts/

HTTPRoute 资源 可用于配置 HTTP 请求的超时。它有助于防止长时间挂起的请求占用资源,也能为最终用户带来更好的体验。

HTTPRouteRule 中的 timeouts 字段用于指定请求的超时时间。

设置请求超时

下面这条 HTTPRoute 把所有路径前缀为 /request-timeout 的请求的超时时间设为 500 毫秒

yaml
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: request-timeout
  namespace: gateway-conformance-infra
spec:
  parentRefs:
  - name: same-namespace
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /request-timeout
    backendRefs:
    - name: infra-backend-v1
      port: 8080
    timeouts:
      request: 500ms

如果对该路径的请求耗时超过 500 毫秒,Gateway 就会返回超时错误

关闭请求超时

若要关闭请求超时,可以把 request 字段设为 "0s"

yaml
  - matches:
    - path:
        type: PathPrefix
        value: /disable-request-timeout
    backendRefs:
    - name: infra-backend-v1
      port: 8080
    timeouts:
      request: "0s"

基于 MIT 协议发布