-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: accelerate the creation of the consumer cache #11840
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -80,7 +80,10 @@ local function filter_consumers_list(data_list) | |||||
return list | ||||||
end | ||||||
|
||||||
local function plugin_consumer() | ||||||
local plugin_consumer | ||||||
do | ||||||
local consumers_id_cache = {} | ||||||
function plugin_consumer() | ||||||
local plugins = {} | ||||||
|
||||||
if consumers.values == nil then | ||||||
|
@@ -101,10 +104,19 @@ local function plugin_consumer() | |||||
if not plugins[name] then | ||||||
plugins[name] = { | ||||||
nodes = {}, | ||||||
len = 0, | ||||||
conf_version = consumers.conf_version | ||||||
} | ||||||
end | ||||||
|
||||||
local cached_consumer = consumers_id_cache[val.value.id] | ||||||
if cached_consumer and | ||||||
cached_consumer.modifiedIndex == val.modifiedIndex then | ||||||
plugins[name].len = plugins[name].len + 1 | ||||||
core.table.insert(plugins[name].nodes, plugins[name].len, | ||||||
cached_consumer) | ||||||
goto CONTINUE_INTERNAL | ||||||
end | ||||||
-- if the val is a Consumer, clone it to the local consumer; | ||||||
-- if the val is a Credential, to get the Consumer by consumer_name and then clone | ||||||
-- it to the local consumer. | ||||||
|
@@ -141,7 +153,10 @@ local function plugin_consumer() | |||||
consumer.auth_conf = config | ||||||
core.log.info("consumer:", core.json.delay_encode(consumer)) | ||||||
core.table.insert(plugins[name].nodes, consumer) | ||||||
consumers_id_cache[val.value.id] = consumer | ||||||
end | ||||||
|
||||||
::CONTINUE_INTERNAL:: | ||||||
end | ||||||
|
||||||
::CONTINUE:: | ||||||
|
@@ -150,6 +165,9 @@ local function plugin_consumer() | |||||
return plugins | ||||||
end | ||||||
|
||||||
end | ||||||
|
||||||
|
||||||
_M.filter_consumers_list = filter_consumers_list | ||||||
|
||||||
function _M.get_consumer_key_from_credential_key(key) | ||||||
|
@@ -186,24 +204,43 @@ function _M.consumers() | |||||
end | ||||||
|
||||||
|
||||||
local function create_consume_cache(consumers_conf, key_attr) | ||||||
local create_consume_cache | ||||||
do | ||||||
local consumers_plugin_key_lrucache_tab = {} | ||||||
|
||||||
local function create_new_consumer(consumer) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need a better name, it is not creating a new consumer it should be |
||||||
local new_consumer = core.table.clone(consumer) | ||||||
new_consumer.auth_conf = secret.fetch_secrets(new_consumer.auth_conf, false) | ||||||
return new_consumer | ||||||
end | ||||||
|
||||||
|
||||||
function create_consume_cache(consumers_conf, key_attr, plugin_name) | ||||||
local consumer_names = {} | ||||||
local lru_cache = consumers_plugin_key_lrucache_tab[plugin_name] | ||||||
if lru_cache == nil then | ||||||
lru_cache = core.lrucache.new({ | ||||||
ttl = 60 * 60 * 24, count = 20480 | ||||||
}) | ||||||
consumers_plugin_key_lrucache_tab[plugin_name] = lru_cache | ||||||
end | ||||||
|
||||||
for _, consumer in ipairs(consumers_conf.nodes) do | ||||||
core.log.info("consumer node: ", core.json.delay_encode(consumer)) | ||||||
local new_consumer = core.table.clone(consumer) | ||||||
new_consumer.auth_conf = secret.fetch_secrets(new_consumer.auth_conf, true, | ||||||
new_consumer.auth_conf, "") | ||||||
consumer_names[new_consumer.auth_conf[key_attr]] = new_consumer | ||||||
local new_consumer = lru_cache(consumer.auth_conf[key_attr], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think: this should be enough, and it seems easier
Suggested change
|
||||||
consumer.modifiedIndex, create_new_consumer, consumer) | ||||||
consumer_names[consumer.auth_conf[key_attr]] = new_consumer | ||||||
end | ||||||
|
||||||
return consumer_names | ||||||
end | ||||||
|
||||||
end | ||||||
|
||||||
|
||||||
function _M.consumers_kv(plugin_name, consumer_conf, key_attr) | ||||||
local consumers = lrucache("consumers_key#" .. plugin_name, consumer_conf.conf_version, | ||||||
create_consume_cache, consumer_conf, key_attr) | ||||||
create_consume_cache, consumer_conf, key_attr, plugin_name) | ||||||
|
||||||
return consumers | ||||||
end | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not know why we have to add
consumers_id_cache
it seems useless