Skip to content

Commit

Permalink
feat(request-aware-table): PR feedback
Browse files Browse the repository at this point in the history
* small refactors here and there
  • Loading branch information
samugi committed Oct 4, 2023
1 parent c8ad88f commit 2901f39
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
14 changes: 8 additions & 6 deletions kong/tools/request_aware_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@ local NGX_VAR_PHASES = {
log = true,
balancer = true,
}
local ALLOWED_REQUEST_ID_K = "__allowed_request_id"


-- Check if access is allowed for table, based on the request ID
local function enforce_sequential_access(table)
if not NGX_VAR_PHASES[get_phase()] then
-- not in a request context: allow access and reset allowed request ID
rawset(table, "__allowed_request_id", nil)
-- allow access and reset allowed request ID
rawset(table, ALLOWED_REQUEST_ID_K, nil)
return
end

local curr_request_id = var.request_id
local allowed_request_id = rawget(table, "__allowed_request_id")
local allowed_request_id = rawget(table, ALLOWED_REQUEST_ID_K)
if not allowed_request_id then
-- first access. Set allowed request ID and allow access
rawset(table, "__allowed_request_id", curr_request_id)
rawset(table, ALLOWED_REQUEST_ID_K, curr_request_id)
return
end

if curr_request_id ~= table.__allowed_request_id then
if curr_request_id ~= table[ALLOWED_REQUEST_ID_K] then
error("race condition detected; access to table forbidden", 2)
end
end
Expand All @@ -52,7 +54,7 @@ local function clear_table(self)
end

table_clear(self.__data)
rawset(self, "__allowed_request_id", nil)
rawset(self, ALLOWED_REQUEST_ID_K, nil)
end


Expand Down
32 changes: 18 additions & 14 deletions spec/02-integration/05-proxy/33-request-aware-table_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@ local LOG_LEVELS = {
}


local function new_table()
local function trigger_plugin_new_table()
local client = helpers.proxy_client()
local res = client:get("/", {
query = {
new_tab = true,
}
})
assert.response(res).has.status(200)
assert.logfile().has.no.line("[error]", true)
client:close()
end

local function trigger_plugin_clear_table()
local client = helpers.proxy_client()
local res = client:get("/", {
query = {
clear = true,
}
})
Expand Down Expand Up @@ -63,7 +74,7 @@ for _, log_level in ipairs(LOG_LEVELS) do

before_each(function()
helpers.clean_logfile()
new_table()
trigger_plugin_new_table()
client = helpers.proxy_client()
end)

Expand Down Expand Up @@ -95,21 +106,14 @@ for _, log_level in ipairs(LOG_LEVELS) do

it("allows access when table is cleared between requests", function()
-- access from request 1 (clear)
local r = client:get("/", {
query = {
clear = true,
}
})
local r = client:get("/")
assert.response(r).has.status(200)
trigger_plugin_clear_table()

-- access from request 2 (clear)
r = client:get("/", {
query = {
clear = true,
}
})
r = client:get("/")
assert.response(r).has.status(200)
assert.logfile().has.no.line("[error]", true)
trigger_plugin_clear_table()

-- access from request 3
r = client:get("/")
Expand All @@ -118,4 +122,4 @@ for _, log_level in ipairs(LOG_LEVELS) do
end)
end)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ function _M:access(conf)
if query.new_tab == "true" then
-- new table
tab = RAT.new()
ngx.exit(200)
end

-- access multiple times during same request
access_table()
access_table()
access_table()

if query.clear == "true" then
-- clear table
tab:clear()
ngx.exit(200)
end

-- access multiple times during same request
for _ = 1, 3 do
access_table()
end
end

Expand Down

0 comments on commit 2901f39

Please sign in to comment.