Skip to content

Commit

Permalink
fix(proxy-cache) avoid HTTP 500 in /proxy-cache when multiple plugins…
Browse files Browse the repository at this point in the history
… are active

Avoid a HTTP 500 error triggered in the global `/proxy-cache` endpoint
when other plugins are also active. This fixes the iteration logic
to ensure it goes only through the proxy-cache plugins.

Fixes #12
  • Loading branch information
hishamhm committed Dec 5, 2019
1 parent 6573b6b commit b12b85f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
38 changes: 23 additions & 15 deletions kong/plugins/proxy-cache/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,32 @@ local function broadcast_purge(plugin_id, cache_key)
end


local function each_proxy_cache()
local iter = kong.db.plugins:each()

return function()
while true do
local plugin, err = iter()
if err then
return kong.response.exit(500, { message = err })
end
if not plugin then
return
end
if plugin.name == "proxy-cache" then
return plugin
end
end
end
end


return {
["/proxy-cache"] = {
resource = "proxy-cache",

DELETE = function()
for plugin, err in kong.db.plugins:each(1000,
{ cache_key = "proxy-cache", }) do
if err then
return kong.response.exit(500, { message = err })
end
for plugin in each_proxy_cache() do

local strategy = require(STRATEGY_PATH)({
strategy_name = plugin.config.strategy,
Expand Down Expand Up @@ -51,11 +67,7 @@ return {
resource = "proxy-cache",

GET = function(self)
for plugin, err in kong.db.plugins:each(1000,
{ cache_key = "proxy-cache", }) do
if err then
return kong.response.exit(500, { message = err })
end
for plugin in each_proxy_cache() do

local strategy = require(STRATEGY_PATH)({
strategy_name = plugin.config.strategy,
Expand All @@ -78,11 +90,7 @@ return {
end,

DELETE = function(self)
for plugin, err in kong.db.plugins:each(1000,
{ cache_key = "proxy-cache", }) do
if err then
return kong.response.exit(500, { message = err })
end
for plugin in each_proxy_cache() do

local strategy = require(STRATEGY_PATH)({
strategy_name = plugin.config.strategy,
Expand Down
9 changes: 8 additions & 1 deletion spec/03-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ describe("Plugin: proxy-cache", function()
},
})

-- an additional plugin does not interfere with the iteration in
-- the global /proxy-cache API handler: regression test for
-- https://github.com/Kong/kong-plugin-proxy-cache/issues/12
assert(bp.plugins:insert {
name = "request-transformer",
})

local route2 = assert(bp.routes:insert {
hosts = { "route-2.com" },
})
Expand All @@ -41,7 +48,7 @@ describe("Plugin: proxy-cache", function()
})

assert(helpers.start_kong({
plugins = "proxy-cache",
plugins = "proxy-cache,request-transformer",
nginx_conf = "spec/fixtures/custom_nginx.template",
}))

Expand Down

0 comments on commit b12b85f

Please sign in to comment.