Skip to content
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

fix: provide error instead of nil panic when cache_zone is missing #10138

Merged
merged 10 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion apisix/plugins/proxy-cache/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ local plugin_name = "proxy-cache"

local STRATEGY_DISK = "disk"
local STRATEGY_MEMORY = "memory"
local DEFAULT_CACHE_ZONE = "disk_cache_one"

local schema = {
type = "object",
Expand All @@ -33,7 +34,7 @@ local schema = {
type = "string",
minLength = 1,
maxLength = 100,
default = "disk_cache_one",
default = DEFAULT_CACHE_ZONE,
},
cache_strategy = {
type = "string",
Expand Down Expand Up @@ -140,6 +141,12 @@ function _M.check_schema(conf)
end
end

-- For memory based cache, the default cache_zone cannot be used.
-- cache_zone will also be set as default value in case when passed empty.
if conf.cache_strategy == STRATEGY_MEMORY and conf.cache_zone == DEFAULT_CACHE_ZONE then
Copy link
Contributor

@monkeyDluffy6017 monkeyDluffy6017 Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not very reliable to judge by cache zone name, i think you could modified like this:

if local_conf.apisix.proxy_cache then
    for _, cache in ipairs(local_conf.apisix.proxy_cache.zones) do
        if cache.name == conf.cache_zone then
            if conf.cache_strategy == STRATEGY_MEMORY and not cache.disk_path then
                found = true
                break
            elseif conf.cache_strategy == STRATEGY_DISK and cache.disk_path then
                found = true
                break
            end
        end
    end
    if found == false then
        return false, "cache_zone " .. conf.cache_zone .. " not found"
    end
end

return false, "invalid or empty cache_zone for cache_strategy: "..conf.cache_strategy
end

return true
end

Expand Down
11 changes: 11 additions & 0 deletions apisix/plugins/proxy-cache/memory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ end


function _M:set(key, obj, ttl)
if self.dict == nil then
return nil, "invalid cache_zone provided"
end

local obj_json = core.json.encode(obj)
if not obj_json then
return nil, "could not encode object"
Expand All @@ -43,6 +47,10 @@ end


function _M:get(key)
if self.dict == nil then
return nil, "invalid cache_zone provided"
end
Revolyssup marked this conversation as resolved.
Show resolved Hide resolved

-- If the key does not exist or has expired, then res_json will be nil.
local res_json, err = self.dict:get(key)
if not res_json then
Expand All @@ -63,6 +71,9 @@ end


function _M:purge(key)
if self.dict == nil then
return nil, "invalid cache_zone provided"
end
self.dict:delete(key)
end

Expand Down
43 changes: 43 additions & 0 deletions t/plugin/proxy-cache/memory.t
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,46 @@ GET /hello
--- more_headers
Cache-Control: only-if-cached
--- error_code: 504



=== TEST 36: configure plugin without memory_cache zone for cache_strategy = memory
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-cache": {
"cache_strategy": "memory",
"cache_key":["$host","$uri"],
"cache_bypass": ["$arg_bypass"],
"cache_control": true,
"cache_method": ["GET"],
"cache_ttl": 10,
"cache_http_status": [200]
}
},
"upstream": {
"nodes": {
"127.0.0.1:1986": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body_like
.*err: invalid or empty cache_zone for cache_strategy: memory.*
--- error_code: 400
Loading