fix: init dns resolvers in stream subsystem (#4186)
diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua
index 5f935cd..c9c5d78 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -85,7 +85,11 @@
init_by_lua_block {
require "resty.core"
apisix = require("apisix")
- apisix.stream_init()
+ local dns_resolver = { {% for _, dns_addr in ipairs(dns_resolver or {}) do %} "{*dns_addr*}", {% end %} }
+ local args = {
+ dns_resolver = dns_resolver,
+ }
+ apisix.stream_init(args)
}
init_worker_by_lua_block {
diff --git a/apisix/init.lua b/apisix/init.lua
index 6225fcc..87da8bc 100644
--- a/apisix/init.lua
+++ b/apisix/init.lua
@@ -715,9 +715,11 @@
end
-function _M.stream_init()
+function _M.stream_init(args)
core.log.info("enter stream_init")
+ core.resolver.init_resolver(args)
+
if core.config.init then
local ok, err = core.config.init()
if not ok then
diff --git a/t/APISIX.pm b/t/APISIX.pm
index 8e662f6..811a7c6 100644
--- a/t/APISIX.pm
+++ b/t/APISIX.pm
@@ -263,7 +263,10 @@
require "resty.core"
apisix = require("apisix")
- apisix.stream_init()
+ local args = {
+ dns_resolver = $dns_addrs_tbl_str,
+ }
+ apisix.stream_init(args)
_EOC_
$stream_config .= <<_EOC_;
diff --git a/t/misc/patch.t b/t/misc/patch.t
index 1635715..a570faf 100644
--- a/t/misc/patch.t
+++ b/t/misc/patch.t
@@ -151,3 +151,36 @@
GET /t
--- response_body
301
+
+
+
+=== TEST 5: resolve host by ourselves (in stream sub-system)
+--- yaml_config
+apisix:
+ node_listen: 1984
+ enable_resolv_search_opt: true
+--- stream_enable
+--- stream_server_config
+ content_by_lua_block {
+ local sock = ngx.req.socket(true)
+ -- drain the buffer
+ local _, err = sock:receive(1)
+ if err ~= nil then
+ ngx.log(ngx.ERR, err)
+ return ngx.exit(-1)
+ end
+ local http = require("resty.http")
+ local httpc = http.new()
+ local res, err = httpc:request_uri("http://apisix")
+ if not res then
+ ngx.log(ngx.ERR, err)
+ return ngx.exit(-1)
+ end
+ sock:send(res.status)
+ }
+--- stream_request eval
+m
+--- stream_response: 301
+
+--- no_error_log
+[error]