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 27c37b1  fix: avoid copying unwanted data when the domain's IP changed 
(#4952)
27c37b1 is described below

commit 27c37b191f35da983d40dd42e5ea517105107567
Author: 罗泽轩 <[email protected]>
AuthorDate: Thu Sep 2 09:38:30 2021 +0800

    fix: avoid copying unwanted data when the domain's IP changed (#4952)
    
    It happened when we are handling upstream with domain inside a route.
    
    Fix #4938
    Fix #4941
    
    Signed-off-by: spacewander <[email protected]>
---
 apisix/init.lua            |  4 ++-
 t/node/upstream-node-dns.t | 69 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/apisix/init.lua b/apisix/init.lua
index a6ad1a7..64879ed 100644
--- a/apisix/init.lua
+++ b/apisix/init.lua
@@ -226,7 +226,9 @@ local function parse_domain_in_route(route)
     -- don't modify the modifiedIndex to avoid plugin cache miss because of 
DNS resolve result
     -- has changed
 
-    route.dns_value = core.table.deepcopy(route.value)
+    -- Here we copy the whole route instead of part of it,
+    -- so that we can avoid going back from route.value to route during 
copying.
+    route.dns_value = core.table.deepcopy(route).value
     route.dns_value.upstream.nodes = new_nodes
     core.log.info("parse route which contain domain: ",
                   core.json.delay_encode(route, true))
diff --git a/t/node/upstream-node-dns.t b/t/node/upstream-node-dns.t
index c26ccf7..e48f05d 100644
--- a/t/node/upstream-node-dns.t
+++ b/t/node/upstream-node-dns.t
@@ -505,3 +505,72 @@ qr/call \/hello
 dns resolver domain: test.com to 127.0.0.1
 proxy request to 127.0.0.(1:1980|5:1981)
 /
+
+
+
+=== TEST 15: route with upstream node, the domain's IP is changed
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "upstream": {
+                        "nodes": {
+                            "test.com:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/hello"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 16: hit
+--- init_by_lua_block
+    require "resty.core"
+    apisix = require("apisix")
+    core = require("apisix.core")
+    apisix.http_init()
+
+    local utils = require("apisix.core.utils")
+    local count = 0
+    utils.dns_parse = function (domain)  -- mock: DNS parser
+        count = count + 1
+        if domain == "test.com" then
+            return {address = "127.0.0." .. count}
+        end
+
+        error("unknown domain: " .. domain)
+    end
+
+--- config
+location /t {
+    content_by_lua_block {
+        local t = require("lib.test_admin").test
+        t('/hello', ngx.HTTP_GET)
+        -- avoid adding more "dns_value" into the route
+        t('/hello', ngx.HTTP_GET)
+    }
+}
+
+--- request
+GET /t
+--- grep_error_log eval
+qr/parse route which contain domain: .+("dns_value":.+){3}/
+--- grep_error_log_out

Reply via email to