Skip to content

Commit

Permalink
fix: avoid copying unwanted data when the domain's IP changed
Browse files Browse the repository at this point in the history
It happened when we are handling upstream with domain inside a route.

Fix apache#4938
Fix apache#4941

Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander committed Sep 1, 2021
1 parent a062a9c commit 75eb7a8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
4 changes: 3 additions & 1 deletion apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
69 changes: 69 additions & 0 deletions t/node/upstream-node-dns.t
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 75eb7a8

Please sign in to comment.