Skip to content

Commit

Permalink
modify the capability format to be list instead of set for more
Browse files Browse the repository at this point in the history
efficient transmission
  • Loading branch information
dndx committed Mar 21, 2024
1 parent 020e4f3 commit b5ddc3c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
28 changes: 15 additions & 13 deletions kong/clustering/rpc/callbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ local _MT = { __index = _M, }


local utils = require("kong.clustering.rpc.utils")
local pl_tablex = require("pl.tablex")


local pairs = pairs
local parse_method_name = utils.parse_method_name
local unpack = table.unpack
local pl_tablex_keys = pl_tablex.keys


function _M.new()
local self = {
callbacks = {},
capabilities = {}, -- updated as register() is called
capabilities_list = {}, -- updated as register() is called
}

return setmetatable(self, _MT)
Expand All @@ -24,23 +28,21 @@ function _M:register(method, func)
error("duplicate registration of " .. method)
end

local cap, func_or_err = parse_method_name(method)
if not cap then
return nil, "unable to get capabilities: " .. func_or_err
end

self.capabilities[cap] = true
self.capabilities_list = pl_tablex_keys(self.capabilities)
self.callbacks[method] = func
end


function _M:get_capabilities()
local capabilities = {}

for m in pairs(self.callbacks) do
local cap, func_or_err = parse_method_name(m)
if not cap then
return nil, "unable to get capabilities: " .. func_or_err
end

capabilities[cap] = true
end

return capabilities
-- returns a list of capabilities of this node, like:
-- ["kong.meta.v1", "kong.debug.v1", ...]
function _M:get_capabilities_list()
return self.capabilities_list
end


Expand Down
21 changes: 15 additions & 6 deletions kong/clustering/rpc/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ local callbacks = require("kong.clustering.rpc.callbacks")
local queue = require("kong.clustering.rpc.queue")
local constants = require("kong.constants")
local table_isempty = require("table.isempty")
local pl_tablex = require("pl.tablex")


local ngx_var = ngx.var
local ngx_ERR = ngx.ERR
local ngx_log = ngx.log
local ngx_exit = ngx.exit
local exiting = ngx.worker.exiting
local pl_tablex_makeset = pl_tablex.makeset


local WS_OPTS = {
Expand All @@ -40,9 +42,9 @@ function _M.new(conf, node_id)
}

self.callbacks:register("kong.meta.v1.capability_advertisement", function(node_id, capabilities)
self.client_capabilities[node_id] = capabilities
self.client_capabilities[node_id] = { set = pl_tablex_makeset(capabilities), list = capabilities, }

return self.callbacks:get_capabilities()
return self.callbacks:get_capabilities_list()
end)

return setmetatable(self, _MT)
Expand Down Expand Up @@ -81,7 +83,7 @@ function _M:call(node_id, method, ...)
return nil, "node is not connected, node_id: " .. node_id
end

if not self.client_capabilities[node_id][cap] then
if not self.client_capabilities[node_id].set[cap] then
return nil, "requested capability does not exist, capability: " ..
cap .. ", node_id: " .. node_id
end
Expand Down Expand Up @@ -190,7 +192,7 @@ function _M:connect(premature, node_id, host, path, cert, key)
assert(s:start())

-- capability advertisement
local fut = future.new(s, "kong.meta.v1.capability_advertisement", { self.callbacks:get_capabilities(), })
local fut = future.new(s, "kong.meta.v1.capability_advertisement", { self.callbacks:get_capabilities_list(), })
assert(fut:start())

ok, err = fut:wait(5)
Expand All @@ -200,7 +202,8 @@ function _M:connect(premature, node_id, host, path, cert, key)
goto err
end

self.client_capabilities[node_id] = fut.result
self.client_capabilities[node_id] = { list = fut.result,
set = pl_tablex_makeset(fut.result), }

self:_add_socket(s)

Expand All @@ -225,7 +228,13 @@ end


function _M:get_peers()
return self.client_capabilities
local res = {}

for node_id, cap in pairs(self.client_capabilities) do
res[node_id] = cap.list
end

return res
end


Expand Down

0 comments on commit b5ddc3c

Please sign in to comment.