Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dns): resolv.conf options timeout: 0 is ignored #12640

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/fix-dns-resolv-timeout-zero.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "**DNS Client**: Ignore a non-positive values on resolv.conf for options timeout, and use a default value of 2 seconds instead."
type: bugfix
scope: Core
12 changes: 10 additions & 2 deletions kong/resty/dns/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ local log = ngx.log
local ERR = ngx.ERR
local WARN = ngx.WARN
local ALERT = ngx.ALERT
local NOTICE = ngx.NOTICE
local DEBUG = ngx.DEBUG
--[[
DEBUG = ngx.WARN
Expand All @@ -54,6 +55,8 @@ local req_dyn_hook_run_hooks = req_dyn_hook.run_hooks

local DOT = string_byte(".")
local COLON = string_byte(":")
local DEFAULT_TIMEOUT = 2000 -- 2000 is openresty default


local EMPTY = setmetatable({},
{__newindex = function() error("The 'EMPTY' table is read-only") end})
Expand Down Expand Up @@ -621,10 +624,15 @@ _M.init = function(options)
if resolv.options.timeout then
options.timeout = resolv.options.timeout * 1000
else
options.timeout = 2000 -- 2000 is openresty default
options.timeout = DEFAULT_TIMEOUT
end
end
log(DEBUG, PREFIX, "timeout = ", options.timeout, " ms")
if options.timeout > 0 then
log(DEBUG, PREFIX, "timeout = ", options.timeout, " ms")
else
log(NOTICE, PREFIX, "timeout = ", DEFAULT_TIMEOUT, " ms (a non-positive timeout of ", options.timeout, " configured - using default timeout)")
outsinre marked this conversation as resolved.
Show resolved Hide resolved
options.timeout = DEFAULT_TIMEOUT
end

-- setup the search order
options.ndots = options.ndots or resolv.options.ndots or 1
Expand Down
19 changes: 18 additions & 1 deletion t/03-dns-client/00-sanity.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use strict;
use warnings FATAL => 'all';
use Test::Nginx::Socket::Lua;

plan tests => 2;
plan tests => 5;

run_tests();

Expand All @@ -25,3 +25,20 @@ GET /t
--- response_body
127.0.0.1
--- no_error_log
=== TEST 2: load lua-resty-dns-client
--- config
location = /t {
access_by_lua_block {
local client = require("kong.resty.dns.client")
assert(client.init({ timeout = 0 }))
ngx.exit(200)
}
}
--- request
GET /t
--- error_log
[notice]
timeout = 2000 ms (a non-positive timeout of 0 configured - using default timeout)
Loading