diff --git a/lua/apisix/plugins/grpc-proxy/proto.lua b/lua/apisix/plugins/grpc-proxy/proto.lua index 5045fe2e92b69..9c0c9a05b743d 100644 --- a/lua/apisix/plugins/grpc-proxy/proto.lua +++ b/lua/apisix/plugins/grpc-proxy/proto.lua @@ -1,14 +1,11 @@ -local protoc = require("protoc") -local core = require("apisix.core") -local config = require("apisix.core.config_etcd") -local schema = require("apisix.core.schema") +local core = require("apisix.core") +local protoc = require("protoc") +local lrucache = require("apisix.core.lrucache") +local config = require("apisix.core.config_etcd") +local schema = require("apisix.core.schema") local protos -local lrucache_proto = core.lrucache.new({ - ttl = 300, count = 256 -}) - local function protos_arrange(proto_id) if protos.values == nil then return nil @@ -18,8 +15,7 @@ local function protos_arrange(proto_id) local content for _, proto in ipairs(protos.values) do - local id = proto.value.id - if proto_id==proto.value.id then + if proto_id == proto.value.id then content = proto.value.content break end @@ -27,7 +23,7 @@ local function protos_arrange(proto_id) if not content then ngx.log(ngx.ERR, "failed to find proto by id: " .. proto_id) - return + return end local _p = protoc.new() @@ -37,25 +33,25 @@ local function protos_arrange(proto_id) end -local _M = {} +local _M = {version = 0.1} -_M.new = function(proto_id) - local key = "/proto"..proto_id - return lrucache_proto(key, protos.conf_version, protos_arrange, proto_id) + +function _M.new(proto_id) + return lrucache.global("/protoc", protos.conf_version, protos_arrange, proto_id) end -_M.init_worker = function() +function _M.init() local err - protos, err = config.new("/proto", - { - automatic = true, - item_schema = schema.proto - }) + protos, err = config.new("/proto", { + automatic = true, + item_schema = schema.proto + }) if not protos then - ngx.log(ngx.ERR, "failed to create etcd instance for fetching protos: " .. err) + core.log.error("failed to create etcd instance for fetching protos: " .. err) return end end + return _M