You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2022/03/14 03:44:26 UTC

[apisix] branch master updated: feat: allow set timeout for proxy-mirror (#6562)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 92047c2  feat: allow set timeout for proxy-mirror (#6562)
92047c2 is described below

commit 92047c269420f43ef40c40d867e2e87fc13a6fc6
Author: Gerrard-YNWA <gy...@163.com>
AuthorDate: Mon Mar 14 11:44:22 2022 +0800

    feat: allow set timeout for proxy-mirror (#6562)
---
 apisix/cli/ngx_tpl.lua                 | 12 ++++++++++
 apisix/cli/ops.lua                     |  6 +++++
 conf/config-default.yaml               |  6 ++++-
 docs/en/latest/plugins/proxy-mirror.md | 21 ++++++++++++++++-
 docs/zh/latest/plugins/proxy-mirror.md | 19 +++++++++++++++
 t/cli/test_proxy_mirror_timeout.sh     | 43 ++++++++++++++++++++++++++++++++++
 6 files changed, 105 insertions(+), 2 deletions(-)

diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua
index aaac144..4f1c8c2 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -765,6 +765,18 @@ http {
             }
             {% end %}
 
+
+            {% if proxy_mirror_timeouts then %}
+                {% if proxy_mirror_timeouts.connect then %}
+            proxy_connect_timeout {* proxy_mirror_timeouts.connect *};
+                {% end %}
+                {% if proxy_mirror_timeouts.read then %}
+            proxy_read_timeout {* proxy_mirror_timeouts.read *};
+                {% end %}
+                {% if proxy_mirror_timeouts.send then %}
+            proxy_send_timeout {* proxy_mirror_timeouts.send *};
+                {% end %}
+            {% end %}
             proxy_http_version 1.1;
             proxy_set_header Host $upstream_host;
             proxy_pass $upstream_mirror_uri;
diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua
index 219afc4..7208e5c 100644
--- a/apisix/cli/ops.lua
+++ b/apisix/cli/ops.lua
@@ -513,6 +513,11 @@ Please modify "admin_key" in conf/config.yaml .
         end
     end
 
+    local proxy_mirror_timeouts
+    if yaml_conf.plugin_attr["proxy-mirror"] then
+        proxy_mirror_timeouts = yaml_conf.plugin_attr["proxy-mirror"].timeout
+    end
+
     -- Using template.render
     local sys_conf = {
         use_openresty_1_17 = use_openresty_1_17,
@@ -530,6 +535,7 @@ Please modify "admin_key" in conf/config.yaml .
         admin_server_addr = admin_server_addr,
         control_server_addr = control_server_addr,
         prometheus_server_addr = prometheus_server_addr,
+        proxy_mirror_timeouts = proxy_mirror_timeouts,
     }
 
     if not yaml_conf.apisix then
diff --git a/conf/config-default.yaml b/conf/config-default.yaml
index 8d2c7d7..fcc8d68 100644
--- a/conf/config-default.yaml
+++ b/conf/config-default.yaml
@@ -450,4 +450,8 @@ plugin_attr:
       sequence_bits: 10               # each machine generates a maximum of (1 << sequence_bits) serial numbers per millisecond
       data_machine_ttl: 30            # live time for data_machine in etcd (unit: second)
       data_machine_interval: 10       # lease renewal interval in etcd (unit: second)
-
+  proxy-mirror:
+    timeout:                          # proxy timeout in mirrored sub-request
+      connect: 60s
+      read: 60s
+      send: 60s
diff --git a/docs/en/latest/plugins/proxy-mirror.md b/docs/en/latest/plugins/proxy-mirror.md
index 8c433ef..5b8d8b4 100644
--- a/docs/en/latest/plugins/proxy-mirror.md
+++ b/docs/en/latest/plugins/proxy-mirror.md
@@ -77,7 +77,7 @@ hello world
 > Since the specified mirror address is 127.0.0.1:9797, so to verify whether this plugin is in effect, we need to confirm on the service with port 9797.
 > For example, we can start a simple server:  python -m SimpleHTTPServer 9797
 
-## Disable Plugin
+#### Disable Plugin
 
 Remove the corresponding JSON in the plugin configuration to disable the plugin immediately without restarting the service:
 
@@ -96,3 +96,22 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f1
 ```
 
 The plugin has been disabled now.
+
+## Customize proxy timeouts for mirrored sub-request
+
+We can customize proxy timeouts for the mirrored sub-request in the `plugin_attr` of `conf/config.yaml`. This is useful while mirroring traffic to a slow backend in a long connection scenario.
+
+| Name | Type | Default | Description |
+| --- | --- | --- | --- |
+| connect | string | 60s | connect timeout to the mirrored upstream |
+| read | string | 60s | read timeout to the mirrored upstream |
+| send | string | 60s | send timeout to the mirrored upstream |
+
+```yaml
+plugin_attr:
+  proxy-mirror:
+    timeout:
+      connect: 2000ms
+      read: 2000ms
+      send: 2000ms
+```
diff --git a/docs/zh/latest/plugins/proxy-mirror.md b/docs/zh/latest/plugins/proxy-mirror.md
index cf4acb9..b96717f 100644
--- a/docs/zh/latest/plugins/proxy-mirror.md
+++ b/docs/zh/latest/plugins/proxy-mirror.md
@@ -95,3 +95,22 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f1
 ```
 
 这时该插件已被禁用。
+
+## 指定mirror子请求的超时时间
+
+我们可以在 `conf/config.yaml` 的 `plugin_attr` 中指定子请求的超时时间。这在连接复用的场景下 mirror 流量到一个非常慢的后端服务时非常有用。
+
+| 名称 | 类型 | 默认值 | 描述 |
+| --- | --- | --- | --- |
+| connect | string | 60s | 镜像请求到上游的连接超时时间 |
+| read | string | 60s | 镜像请求到上游的读超时时间 |
+| send | string | 60s | 镜像请求到上游的写超时时间 |
+
+```yaml
+plugin_attr:
+  proxy-mirror:
+    timeout:
+      connect: 2000ms
+      read: 2000ms
+      send: 2000ms
+```
diff --git a/t/cli/test_proxy_mirror_timeout.sh b/t/cli/test_proxy_mirror_timeout.sh
new file mode 100755
index 0000000..90b7fd0
--- /dev/null
+++ b/t/cli/test_proxy_mirror_timeout.sh
@@ -0,0 +1,43 @@
+#!/usr/bin/env bash
+
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+. ./t/cli/common.sh
+
+echo '
+plugin_attr:
+  proxy-mirror:
+    timeout:
+        connect: 2000ms
+        read: 2s
+        send: 2000ms
+' > conf/config.yaml
+
+make init
+
+if ! grep "proxy_connect_timeout 2000ms;" conf/nginx.conf > /dev/null; then
+    echo "failed: proxy_connect_timeout not found in nginx.conf"
+    exit 1
+fi
+
+if ! grep "proxy_read_timeout 2s;" conf/nginx.conf > /dev/null; then
+    echo "failed: proxy_read_timeout not found in nginx.conf"
+    exit 1
+fi
+
+echo "passed: proxy timeout configuration is validated"