Skip to content

Commit 7bbdc0b

Browse files
authored
feat: bypass unix socket proxy if the socket file is deleted (#172)
1 parent 5bce561 commit 7bbdc0b

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ jobs:
4343
- name: get dependencies
4444
run: sudo apt install -y cpanminus build-essential libncurses5-dev libreadline-dev libssl-dev perl
4545

46-
- name: Install Lua
47-
uses: leafo/gh-actions-lua@v8
46+
- name: Setup Lua
47+
uses: leafo/[email protected]
48+
with:
49+
luaVersion: "5.1.5"
4850

4951
- name: Install Luarocks
5052
uses: leafo/gh-actions-luarocks@v4

lib/resty/etcd/v3.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ local encode_base64 = ngx.encode_base64
2626
local decode_base64 = ngx.decode_base64
2727
local semaphore = require("ngx.semaphore")
2828
local health_check = require("resty.etcd.health_check")
29+
local pl_path = require("pl.path")
2930

3031
math.randomseed(now() * 1000 + ngx.worker.pid())
3132

@@ -83,7 +84,14 @@ local function request_uri_via_unix_socket(self, uri, params)
8384

8485
local path, query
8586
params.scheme, params.host, params.port, path, query = unpack(parsed_uri)
87+
88+
local use_unix_socket = false
8689
if params.unix_socket_proxy then
90+
local sock_path = params.unix_socket_proxy:sub(#"unix:" + 1)
91+
use_unix_socket = pl_path.exists(sock_path)
92+
end
93+
94+
if use_unix_socket then
8795
if not params.headers then
8896
params.headers = {}
8997
end
@@ -342,6 +350,7 @@ function _M.new(opts)
342350
tab_insert(endpoints, {
343351
full_prefix = host .. utils.normalize(api_prefix),
344352
http_host = host,
353+
parsed_uri = m,
345354
scheme = m[1],
346355
host = m[2] or "127.0.0.1",
347356
address = addr,
@@ -634,6 +643,14 @@ local function http_request_chunk(self, http_cli)
634643
return nil, err
635644
end
636645

646+
if not endpoint.port then
647+
local sock_path = endpoint.address:sub(#"unix:" + 1)
648+
if not pl_path.exists(sock_path) then
649+
endpoint.address = endpoint.parsed_uri[2]
650+
endpoint.port = endpoint.parsed_uri[3]
651+
end
652+
end
653+
637654
local ok
638655
ok, err = http_cli:connect({
639656
scheme = endpoint.scheme,

rockspec/lua-resty-etcd-master-0.1-0.rockspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ description = {
1414

1515
dependencies = {
1616
"api7-lua-resty-http = 0.1.0",
17+
"luafilesystem = 1.7.0-2",
18+
"penlight = 1.9.2-1",
1719
"lua-typeof = 0.1"
1820
}
1921

t/v3/unix_socket.t

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,66 @@ value: bcd3
126126
closed
127127
ok
128128
--- timeout: 5
129+
130+
131+
132+
=== TEST 2: request over unix socket, unix socket doesn't exist
133+
--- http_config eval: $::HttpConfig
134+
--- config
135+
location /t {
136+
content_by_lua_block {
137+
local etcd, err = require("resty.etcd").new({protocol = "v3", http_host = "http://127.0.0.1:2379",
138+
unix_socket_proxy = "unix:$TEST_NGINX_HTML_DIR/bad.sock"})
139+
check_res(etcd, err)
140+
141+
local res, err = etcd:set("/test", "abc")
142+
check_res(res, err)
143+
144+
ngx.timer.at(0.1, function ()
145+
etcd:set("/test", "bcd3")
146+
end)
147+
148+
ngx.timer.at(0.2, function ()
149+
etcd:set("/test", "bcd4")
150+
end)
151+
152+
local cur_time = ngx.now()
153+
local body_chunk_fun, err, http_cli = etcd:watch("/test", {timeout = 0.5, need_cancel = true})
154+
155+
if type(http_cli) ~= "table" then
156+
ngx.say("need_cancel failed")
157+
end
158+
159+
if not body_chunk_fun then
160+
ngx.say("failed to watch: ", err)
161+
end
162+
163+
local chunk, err = body_chunk_fun()
164+
ngx.say("created: ", chunk.result.created)
165+
local chunk, err = body_chunk_fun()
166+
ngx.say("value: ", chunk.result.events[1].kv.value)
167+
168+
local res, err = etcd:watchcancel(http_cli)
169+
if not res then
170+
ngx.say("failed to cancel: ", err)
171+
end
172+
173+
local chunk, err = body_chunk_fun()
174+
ngx.say(err)
175+
176+
ngx.say("ok")
177+
}
178+
}
179+
--- request
180+
GET /t
181+
--- no_error_log
182+
[error]
183+
--- grep_error_log eval
184+
qr/hit with host 127.0.0.1/
185+
--- grep_error_log_out
186+
--- response_body
187+
created: true
188+
value: bcd3
189+
closed
190+
ok
191+
--- timeout: 5

0 commit comments

Comments
 (0)