go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/quota/internal/lua/policy.lua (about) 1 -- Copyright 2022 The LUCI Authors 2 -- 3 -- Licensed under the Apache License, Version 2.0 (the "License"); 4 -- you may not use this file except in compliance with the License. 5 -- You may obtain a copy of the License at 6 -- 7 -- http://www.apache.org/licenses/LICENSE-2.0 8 -- 9 -- Unless required by applicable law or agreed to in writing, software 10 -- distributed under the License is distributed on an "AS IS" BASIS, 11 -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 -- See the License for the specific language governing permissions and 13 -- limitations under the License. 14 15 -- expect to have PB and Utils passed in 16 local PB, Utils = ... 17 assert(PB) 18 assert(Utils) 19 20 local Policy = { 21 -- config => key => { 22 -- policy_ref: PB go.chromium.org.luci.server.quota.PolicyRef, 23 -- pb: PB go.chromium.org.luci.server.quota.Policy, 24 -- } 25 CACHE = {}, 26 } 27 28 local PolicyPB = "go.chromium.org.luci.server.quota.quotapb.Policy" 29 local PolicyRefPB = "go.chromium.org.luci.server.quota.quotapb.PolicyRef" 30 31 function Policy.get(policy_ref) 32 assert(policy_ref ~= nil, "Policy:get called with <nil>") 33 34 local configkey = policy_ref.config 35 local policykey = policy_ref.key 36 37 if configkey == "" and policykey == "" then 38 return nil 39 end 40 41 local configTable = Policy.CACHE[configkey] 42 if not configTable then 43 configTable = {} 44 Policy.CACHE[configkey] = configTable 45 end 46 47 local entry = configTable[policykey] 48 if not entry then 49 local raw = redis.call("HGET", configkey, policykey) 50 if not raw then 51 return nil -- will result in "ERR_UNKNOWN_POLICY" 52 end 53 entry = { 54 policy_ref = policy_ref, 55 pb = PB.unmarshal(PolicyPB, raw), 56 } 57 configTable[policykey] = entry 58 end 59 return entry 60 end 61 62 return Policy