Skip to content

Commit 56ce788

Browse files
committed
feature: allow socket override
this allows to fallback on LuaSocket in contexts that do not support co-sockets.
1 parent 6d96cfb commit 56ce788

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

README.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ It accepts a `opts` table argument. The following options are supported:
134134
* `no_recurse`
135135

136136
a boolean flag controls whether to disable the "recursion desired" (RD) flag in the UDP request. Defaults to `false`.
137+
* `tcp`
138+
139+
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`.
140+
* `udp`
141+
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`.
142+
137143

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

lib/resty/dns/resolver.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
-- local socket = require "socket"
55
local bit = require "bit"
6-
local udp = ngx.socket.udp
76
local rand = math.random
87
local char = string.char
98
local byte = string.byte
@@ -18,7 +17,6 @@ local lshift = bit.lshift
1817
local insert = table.insert
1918
local concat = table.concat
2019
local re_sub = ngx.re.sub
21-
local tcp = ngx.socket.tcp
2220
local log = ngx.log
2321
local DEBUG = ngx.DEBUG
2422
local unpack = unpack
@@ -112,6 +110,9 @@ function _M.new(class, opts)
112110

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

113+
local udp = opts.udp or ngx.socket.udp
114+
local tcp = opts.tcp or ngx.socket.tcp
115+
115116
local n = #servers
116117

117118
local socks = {}

t/sanity.t

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use Cwd qw(cwd);
55

66
repeat_each(2);
77

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

1010
my $pwd = cwd();
1111

@@ -684,3 +684,50 @@ GET /t
684684
--- no_error_log
685685
[error]
686686
--- no_check_leak
687+
688+
689+
690+
=== TEST 21: override socket functions
691+
--- http_config eval: $::HttpConfig
692+
--- config
693+
location /t {
694+
content_by_lua '
695+
local resolver = require "resty.dns.resolver"
696+
697+
local r, err =
698+
resolver:new {
699+
nameservers = { "$TEST_NGINX_RESOLVER" },
700+
tcp = function(...)
701+
ngx.say("tcp called")
702+
return ngx.socket.tcp(...)
703+
end,
704+
udp = function(...)
705+
ngx.say("udp called")
706+
return ngx.socket.udp(...)
707+
end,
708+
}
709+
if not r then
710+
ngx.say("failed to instantiate resolver: ", err)
711+
return
712+
end
713+
714+
local ans, err = r:query("google.com")
715+
if not ans then
716+
ngx.say("failed to query (udp): ", err)
717+
return
718+
end
719+
local ans, err = r:tcp_query("google.com")
720+
if not ans then
721+
ngx.say("failed to query (tcp): ", err)
722+
return
723+
end
724+
';
725+
}
726+
--- request
727+
GET /t
728+
--- response
729+
udp called
730+
tcp called
731+
--- no_error_log
732+
[error]
733+
--- no_check_leak

0 commit comments

Comments
 (0)