简介
Envoy支持多种超时,可根据需要进行配置。本文利用下述配置样例,总结了常见的不同场景中会使用到的超时配置。
注:只关注配置样例中的timeout等时间相关配置即可,并结合注释进行阅读。
overload_manager: #J
actions:
- name: envoy.overload_actions.shrink_heap
triggers:
- name: envoy.resource_monitors.fixed_heap
threshold:
value: 0.9
- name: envoy.overload_actions.stop_accepting_requests
triggers:
- name: envoy.resource_monitors.fixed_heap
threshold:
value: 0.95
refresh_interval: 0.25s
resource_monitors:
- name: envoy.resource_monitors.fixed_heap
typed_config:
'@type': type.googleapis.com/envoy.extensions.resource_monitors.fixed_heap.v3.FixedHeapConfig
max_heap_size_bytes:
static_resources:
listeners:
- name:
address:
socket_address:
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
common_http_protocol_options:
idle_timeout: 3600s #A
request_timeout: 200s #C
request_headers_timeout: 5s #D
stream_idle_timeout: 100s #E
http_filters:
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
route_config:
name:
virtual_hosts:
- name: local_service
domains:
routes:
- match:
prefix:
route:
timeout: 300s #F
idle_timeout: 20s #G
cluster: cluster_common_https
retry_policy:
per_try_timeout: 10s #H
per_try_idle_timeout: 5s #I
transport_socket_connect_timeout: 30s #K
clusters:
- name: cluster_common_https
typed_extension_protocol_options:
envoy.extensions.upstreams.http.v3.HttpProtocolOptions:
"@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions
common_http_protocol_options:
idle_timeout: 3600s #B
load_assignment:
cluster_name:
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
HTTP/gRPC
Connection timeouts
idle_timeout定义了HTTP链接的空闲超时时长,即若在该时间段内,链接中没有任何业务流量通过,则终止该链接。
Envoy可针对上游与下游分别配置idle_timeout,可参见上述配置样例中的#A(下游)与#B(上游)。
Stream timeouts
Stream虽然是HTTP2与HTTP3的概念,但Envoy中将HTTP1的请求映射为Stream,因此此处的配置可应用于三个版本的HTTP。
request_timeout(#C)定义了Envoy等待整个请求的时长。即,从Envoy初始化请求到Envoy将请求全部发送到上游服务的超时时长。(该配置与streaming请求不兼容)
request_headers_timeout(#D)定义了Envoy完成从下游接收一个请求的请求头的超时时长。
stream_idle_timeout(#E)定义了Envoy允许一个没有任何业务流量(上游方向和下游方向)的stream存在的超时时长。
Route timeouts
Envoy允许在route这一层定义对timeout进行一些额外配置,以及覆盖上述的一些timeout的配置。
timeout(#F)定义了Envoy允许等待上游服务完成回包的时长。即,从Envoy收到下游完整的请求开始到Envoy收到上游完整的回包的超时时长。注意,该配置与streaming请求不兼容,对于streaming类流量,应将该配置设置为0。
idle_timeout(#G)覆盖HttpConnectionManager下的stream_idle_timeout。
per_try_timeout为route的重试机制定义了超时时长。即在重试机制触发后,Envoy为每次重试可以等待的超时时长(重试触发到有回包数据发送到下游之前)。
per_try_idle_timeout(#I)为route的重试机制定义了Envoy与上游服务之间的idle timeout。
Scaled timeouts
在Envoy高负载的情况下,Envoy可配置scaled timeout(#J)。
一个比较常见的做法是,当Envoy的堆内存使用超过某个阈值后,将Envoy的idle timeout调低;而当Envoy的堆内存的使用继续增大到某个阈值后,Envoy将拒绝处理下游的请求。
Transport Socket
transport_socket_connect_timeout定义了Envoy与下游完成传输层协商的超时时长。该配置也包含了对TLS/ATLS握手(若有)的限制。
参考链接
https://www.envoyproxy.io/docs/envoy/latest/faq/configuration/timeouts