perf(grpc transcode): find proto in O(1) way (#5331)

This commit is contained in:
Bisakh 2021-10-27 07:39:39 +05:30 committed by GitHub
parent a08e02c370
commit 5fd95ce608
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 31 deletions

View File

@ -18,6 +18,7 @@ local core = require("apisix.core")
local config_util = require("apisix.core.config_util")
local protoc = require("protoc")
local pcall = pcall
local ipairs = ipairs
local protos
@ -25,6 +26,7 @@ local lrucache_proto = core.lrucache.new({
ttl = 300, count = 100
})
local proto_fake_file = "filename for loaded"
local function compile_proto(content)
local _p = protoc.new()
@ -33,7 +35,7 @@ local function compile_proto(content)
-- name to keep the code below unchanged, or we can create our own load function with returning
-- the loaded DescriptorProto table additionally, see more details in
-- https://github.com/apache/apisix/pull/4368
local ok, res = pcall(_p.load, _p, content, "filename for loaded")
local ok, res = pcall(_p.load, _p, content, proto_fake_file)
if not ok then
return nil, res
end
@ -46,6 +48,12 @@ local function compile_proto(content)
end
local _M = {
version = 0.1,
compile_proto = compile_proto,
proto_fake_file = proto_fake_file
}
local function create_proto_obj(proto_id)
if protos.values == nil then
return nil
@ -63,16 +71,27 @@ local function create_proto_obj(proto_id)
return nil, "failed to find proto by id: " .. proto_id
end
return compile_proto(content)
local compiled, err = compile_proto(content)
if not compiled then
return nil, err
end
local index = {}
for _, s in ipairs(compiled[proto_fake_file].service or {}) do
local method_index = {}
for _, m in ipairs(s.method) do
method_index[m.name] = m
end
index[compiled[proto_fake_file].package .. '.' .. s.name] = method_index
end
compiled[proto_fake_file].index = index
return compiled
end
local _M = {
version = 0.1,
compile_proto = compile_proto,
}
function _M.fetch(proto_id)
return lrucache_proto(proto_id, protos.conf_version,
create_proto_obj, proto_id)

View File

@ -14,37 +14,30 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local core = require("apisix.core")
local json = core.json
local pb = require("pb")
local ngx = ngx
local pairs = pairs
local ipairs = ipairs
local string = string
local tonumber = tonumber
local type = type
local core = require("apisix.core")
local proto_fake_file = require("apisix.plugins.grpc-transcode.proto").proto_fake_file
local json = core.json
local pb = require("pb")
local ngx = ngx
local string = string
local tonumber = tonumber
local type = type
local _M = {version = 0.1}
function _M.find_method(protos, service, method)
for k, loaded in pairs(protos) do
if type(loaded) == 'table' then
local package = loaded.package
for _, s in ipairs(loaded.service or {}) do
if package .. "." .. s.name == service then
for _, m in ipairs(s.method) do
if m.name == method then
return m
end
end
end
end
end
local loaded = protos[proto_fake_file]
if not loaded or type(loaded) ~= "table" then
return nil
end
return nil
if not loaded.index[service] or type(loaded.index[service]) ~= "table" then
return nil
end
return loaded.index[service][method]
end