Skip to content

Commit

Permalink
refactor(tools): simplify the logic of request_aware_table (#11756)
Browse files Browse the repository at this point in the history
There are some duplicated code in tracing.request_id and request_aware_table,
use request_id.get() to get clean code.
  • Loading branch information
chronolaw authored Oct 25, 2023
1 parent 616bc7f commit 14521a0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
21 changes: 6 additions & 15 deletions kong/tools/request_aware_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,28 @@ local table_new = require("table.new")
local table_clear = require("table.clear")
local get_request_id = require("kong.tracing.request_id").get


local is_not_debug_mode = (kong.configuration.log_level ~= "debug")


local error = error
local rawset = rawset
local setmetatable = setmetatable
local get_phase = ngx.get_phase


local NGX_VAR_PHASES = {
set = true,
rewrite = true,
access = true,
content = true,
header_filter = true,
body_filter = true,
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
local curr_request_id = get_request_id()

if not curr_request_id then
-- allow access and reset allowed request ID
rawset(table, ALLOWED_REQUEST_ID_K, nil)
return
end

local curr_request_id = get_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
Expand Down
1 change: 1 addition & 0 deletions kong/tracing/request_id.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local ngx = ngx
local var = ngx.var
local get_phase = ngx.get_phase


local NGX_VAR_PHASES = {
set = true,
rewrite = true,
Expand Down

1 comment on commit 14521a0

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

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

Bazel Build

Docker image available kong/kong:14521a0c132a48be2ebbd6399a61951820c0bdf2
Artifacts available https://github.com/Kong/kong/actions/runs/6637843653

Please sign in to comment.