Skip to content

Commit c718a41

Browse files
fix(core): RPC - test config template, array of strings, better errors
- the config template used for tests was missing the inter-module RPC socket - to reuse the data-gathering function on both HTTP and Stream modules, the RPC message should support array of strings, just like `ngx.print()` . - prefix errors with the intent. "connection closed" is far too ambiguous.
1 parent 2f8bbf0 commit c718a41

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

kong/tools/stream_api.lua

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,27 @@ function stream_api.request(key, data, socket_path)
4444
end
4545

4646
local socket = assert(ngx.socket.udp())
47-
assert(socket:setpeername(socket_path or "unix:" .. PREFIX .. "/stream_rpc.sock"))
47+
local ok, err = socket:setpeername(socket_path or "unix:" .. PREFIX .. "/stream_rpc.sock")
48+
if not ok then
49+
return nil, "opening internal RPC socket: " .. tostring(err)
50+
end
4851

49-
local ok, err = socket:send(st_pack("=PP", key, data))
52+
ok, err = socket:send(st_pack("=PP", key, data))
5053
if not ok then
5154
socket:close()
52-
return ok, err
55+
return nil, "sending stream-api request: " .. tostring(err)
5356
end
5457

5558
data, err = socket:receive()
5659
if not data then
5760
socket:close()
58-
return data, err
61+
return nil, "retrieving stream-api response: " .. tostring(err)
5962
end
6063

6164
local _, status, payload = st_unpack(data, "=SP")
6265
if status ~= 0 then
6366
socket:close()
64-
return nil, payload
67+
return nil, "stream-api errmsg: " .. payload
6568
end
6669

6770
socket:close()
@@ -93,6 +96,10 @@ function stream_api.handle()
9396
return
9497
end
9598

99+
if type(res) == "table" then
100+
res = table.concat(res)
101+
end
102+
96103
if type(res) ~= "string" then
97104
error(st_format("stream_api handler %q response is not a string", key))
98105
end

spec/02-integration/12-stream_api/01-stream_api_endpoint_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe("Stream module API endpoint", function()
2323
it("error response for unknown path", function()
2424
local res, err = stream_api.request("not-this", "nope", socket_path)
2525
assert.is.falsy(res)
26-
assert.equal("no handler", err)
26+
assert.equal("stream-api errmsg: no handler", err)
2727
end)
2828

2929
it("calls an echo handler", function ()

spec/fixtures/custom_nginx.template

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,5 +876,14 @@ stream {
876876
}
877877

878878
include '*.stream_mock';
879+
880+
server { # ignore (and close }, to ignore content)
881+
listen unix:${{PREFIX}}/stream_rpc.sock udp;
882+
error_log ${{ADMIN_ERROR_LOG}} ${{LOG_LEVEL}};
883+
content_by_lua_block {
884+
Kong.stream_api()
885+
}
886+
}
887+
879888
}
880889
> end

0 commit comments

Comments
 (0)