Skip to content

Commit a2e8765

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

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

@@ -646,3 +646,50 @@ GET /t
646646
--- no_error_log
647647
[error]
648648
--- no_check_leak
649+
650+
651+
652+
=== TEST 20: override socket functions
653+
--- http_config eval: $::HttpConfig
654+
--- config
655+
location /t {
656+
content_by_lua '
657+
local resolver = require "resty.dns.resolver"
658+
659+
local r, err =
660+
resolver:new {
661+
nameservers = { "$TEST_NGINX_RESOLVER" },
662+
tcp = function(...)
663+
ngx.say("tcp called")
664+
return ngx.socket.tcp(...)
665+
end,
666+
udp = function(...)
667+
ngx.say("udp called")
668+
return ngx.socket.udp(...)
669+
end,
670+
}
671+
if not r then
672+
ngx.say("failed to instantiate resolver: ", err)
673+
return
674+
end
675+
676+
local ans, err = r:query("google.com")
677+
if not ans then
678+
ngx.say("failed to query (udp): ", err)
679+
return
680+
end
681+
local ans, err = r:tcp_query("google.com")
682+
if not ans then
683+
ngx.say("failed to query (tcp): ", err)
684+
return
685+
end
686+
';
687+
}
688+
--- request
689+
GET /t
690+
--- response
691+
udp called
692+
tcp called
693+
--- no_error_log
694+
[error]
695+
--- no_check_leak

0 commit comments

Comments
 (0)