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

feature: allow socket override #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ It accepts a `opts` table argument. The following options are supported:
* `no_recurse`

a boolean flag controls whether to disable the "recursion desired" (RD) flag in the UDP request. Defaults to `false`.
* `tcp`

an optional function to create a tcp socket. This allows using the library in contexts where the co-sockets are unavailable. Defaults to `ngx.socket.tcp`.
* `udp`
an optional function to create a udp socket. This allows using the library in contexts where the co-sockets are unavailable. Defaults to `ngx.socket.udp`.


[Back to TOC](#table-of-contents)

Expand Down
5 changes: 3 additions & 2 deletions lib/resty/dns/resolver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

-- local socket = require "socket"
local bit = require "bit"
local udp = ngx.socket.udp
local rand = math.random
local char = string.char
local byte = string.byte
Expand All @@ -18,7 +17,6 @@ local lshift = bit.lshift
local insert = table.insert
local concat = table.concat
local re_sub = ngx.re.sub
local tcp = ngx.socket.tcp
local log = ngx.log
local DEBUG = ngx.DEBUG
local unpack = unpack
Expand Down Expand Up @@ -112,6 +110,9 @@ function _M.new(class, opts)

local timeout = opts.timeout or 2000 -- default 2 sec

local udp = opts.udp or ngx.socket.udp
local tcp = opts.tcp or ngx.socket.tcp

local n = #servers

local socks = {}
Expand Down
49 changes: 48 additions & 1 deletion t/sanity.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use Cwd qw(cwd);

repeat_each(2);

plan tests => repeat_each() * (3 * blocks());
plan tests => repeat_each() * (3 * blocks()) - 2;

my $pwd = cwd();

Expand Down Expand Up @@ -684,3 +684,50 @@ GET /t
--- no_error_log
[error]
--- no_check_leak



=== TEST 21: override socket functions
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua '
local resolver = require "resty.dns.resolver"

local r, err =
resolver:new {
nameservers = { "$TEST_NGINX_RESOLVER" },
tcp = function(...)
ngx.say("tcp called")
return ngx.socket.tcp(...)
end,
udp = function(...)
ngx.say("udp called")
return ngx.socket.udp(...)
end,
}
if not r then
ngx.say("failed to instantiate resolver: ", err)
return
end

local ans, err = r:query("google.com")
if not ans then
ngx.say("failed to query (udp): ", err)
return
end
local ans, err = r:tcp_query("google.com")
if not ans then
ngx.say("failed to query (tcp): ", err)
return
end
';
}
--- request
GET /t
--- response
udp called
tcp called
--- no_error_log
[error]
--- no_check_leak