Buffering

转发前如何阅读请求

Buffering

缓冲中间件使您可以控制在将请求发送到服务之前如何读取请求.

通过"缓冲",Traefik将整个请求读入内存(可能将大请求缓冲到磁盘),并拒绝超过指定限制的请求.

这可以帮助服务处理大数据(例如,多部分/表单数据),并可以最大程度地减少将数据发送到服务所花费的时间.

Configuration Examples

# Sets the maximum request body to 2Mb
labels:
  - "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
# Sets the maximum request body to 2Mb
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: limit
spec:
  buffering:
    maxRequestBodyBytes: 2000000
"labels": {
  "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes": "2000000"
}
# Sets the maximum request body to 2Mb
labels:
  - "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
# Sets the maximum request body to 2Mb
[http.middlewares]
  [http.middlewares.limit.buffering]
    maxRequestBodyBytes = 2000000
# Sets the maximum request body to 2Mb
http:
  middlewares:
    limit:
      buffering:
        maxRequestBodyBytes: 2000000

Configuration Options

maxRequestBodyBytes

使用maxRequestBodyBytes选项,可以配置请求的最大允许主体大小(以字节为单位).

如果请求超出允许的大小,则不会将其转发到服务,并且客户端会收到413 (Request Entity Too Large)响应.

labels:
  - "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: limit
spec:
  buffering:
    maxRequestBodyBytes: 2000000
"labels": {
  "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes": "2000000"
}
labels:
  - "traefik.http.middlewares.limit.buffering.maxRequestBodyBytes=2000000"
[http.middlewares]
  [http.middlewares.limit.buffering]
    maxRequestBodyBytes = 2000000
http:
  middlewares:
    limit:
      buffering:
        maxRequestBodyBytes: 2000000

memRequestBodyBytes

您可以使用memRequestBodyBytes选项配置一个阈值(以字节为单位),从该阈值将请求缓存到磁盘上而不是内存中.

labels:
  - "traefik.http.middlewares.limit.buffering.memRequestBodyBytes=2000000"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: limit
spec:
  buffering:
    memRequestBodyBytes: 2000000
"labels": {
  "traefik.http.middlewares.limit.buffering.memRequestBodyBytes": "2000000"
}
labels:
  - "traefik.http.middlewares.limit.buffering.memRequestBodyBytes=2000000"
[http.middlewares]
  [http.middlewares.limit.buffering]
    memRequestBodyBytes = 2000000
http:
  middlewares:
    limit:
      buffering:
        memRequestBodyBytes: 2000000

maxResponseBodyBytes

使用maxReesponseBodyBytes选项,可以配置服务允许的最大响应大小(以字节为单位).

如果响应超出允许的大小,则不会转发给客户端. 客户端改为收到413 (Request Entity Too Large) response .

labels:
  - "traefik.http.middlewares.limit.buffering.maxResponseBodyBytes=2000000"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: limit
spec:
  buffering:
    maxResponseBodyBytes: 2000000
"labels": {
  "traefik.http.middlewares.limit.buffering.maxResponseBodyBytes": "2000000"
}
labels:
  - "traefik.http.middlewares.limit.buffering.maxResponseBodyBytes=2000000"
[http.middlewares]
  [http.middlewares.limit.buffering]
    maxResponseBodyBytes = 2000000
http:
  middlewares:
    limit:
      buffering:
        maxResponseBodyBytes: 2000000

memResponseBodyBytes

您可以使用memResponseBodyBytes选项配置一个阈值(以字节为单位),从该阈值将响应缓存在磁盘上而不是内存中.

labels:
  - "traefik.http.middlewares.limit.buffering.memResponseBodyBytes=2000000"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: limit
spec:
  buffering:
    memResponseBodyBytes: 2000000
"labels": {
  "traefik.http.middlewares.limit.buffering.memResponseBodyBytes": "2000000"
}
labels:
  - "traefik.http.middlewares.limit.buffering.memResponseBodyBytes=2000000"
[http.middlewares]
  [http.middlewares.limit.buffering]
    memResponseBodyBytes = 2000000
http:
  middlewares:
    limit:
      buffering:
        memResponseBodyBytes: 2000000

retryExpression

您可以在retryExpression选项的帮助下使缓冲中间件重播请求.

发生网络错误时重试一次
labels:
  - "traefik.http.middlewares.limit.buffering.retryExpression=IsNetworkError() && Attempts() < 2"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: limit
spec:
  buffering:
    retryExpression: "IsNetworkError() && Attempts() < 2"
"labels": {
  "traefik.http.middlewares.limit.buffering.retryExpression": "IsNetworkError() && Attempts() < 2"
}
labels:
  - "traefik.http.middlewares.limit.buffering.retryExpression=IsNetworkError() && Attempts() < 2"
[http.middlewares]
  [http.middlewares.limit.buffering]
    retryExpression = "IsNetworkError() && Attempts() < 2"
http:
  middlewares:
    limit:
      buffering:
        retryExpression: "IsNetworkError() && Attempts() < 2"

重试表达式定义为以下功能与运算符AND( && )和OR( || )的逻辑组合. 至少需要一个功能:

  • Attempts()尝试次数(第一个计数)
  • ResponseCode()响应代码
  • IsNetworkError() -如果响应代码与网络错误有关