fekitibi commented on code in PR #12484: URL: https://github.com/apache/apisix/pull/12484#discussion_r2262498411
########## apisix/core/admin_key.lua: ########## @@ -0,0 +1,162 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +--- Load or generate admin keys. +-- +-- @module core.admin_key + +local fetch_local_conf = require("apisix.core.config_local").local_conf +local try_read_attr = require("apisix.core.table").try_read_attr +local log = require("apisix.core.log") +local string = string +local math = math +local ipairs = ipairs +local type = type + +local _M = {version = 0.1} +local admin_keys_cache = {} + +function _M.get_admin_keys() + -- Lazy loading: only load keys when actually needed + if #admin_keys_cache == 0 then + _M.load_keys_from_shared_memory() + end + return admin_keys_cache +end + +function _M.admin_key_required() + local local_conf = fetch_local_conf() + return local_conf.deployment.admin.admin_key_required +end + +function _M.init_worker() + local local_conf = fetch_local_conf() + local deployment_role = local_conf.deployment and local_conf.deployment.role + + if local_conf.deployment.admin.admin_key_required == false then + return + end + + if not deployment_role or (deployment_role ~= "traditional" and deployment_role ~= "control_plane") then + return + end + + local config_admin_keys = try_read_attr(local_conf, "deployment", "admin", "admin_key") + if not config_admin_keys or type(config_admin_keys) ~= "table" then + return + end + + local admin_keys_shm = ngx.shared.admin_keys + if not admin_keys_shm then + log.error("admin_keys shared memory zone not configured") + return + end + + -- Check if already initialized by another worker + if admin_keys_shm:get("completed") then + _M.load_keys_from_shared_memory() + return + end + + -- Atomic check-and-set: only one worker can claim initialization + local claimed_init, err = admin_keys_shm:safe_add("init_claimed", ngx.worker.id() or 0, 5) + if not claimed_init then + -- Another worker is handling initialization, no waiting needed Review Comment: The concern doesn’t apply here, `get_admin_keys()` lazy-loads from ngx.shared.admin_keys, which is visible to all workers. Even if another worker is initializing, keys are read directly from the shared dict, not from an uninitialized local cache. The "completed" flag is just an optimization, not a requirement for correctness, so no retry logic is needed. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@apisix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org