Keep code readable and maintainable by (1) extracting complex or phase-specific logic into dedicated helper functions, (2) removing duplicated logic/schema/label definitions in favor of shared helpers or a single source of truth, and (3) following existing project idioms/utilities to stay consistent.
How to apply:
log_phase_incoming(...) and call them from the main path.core.string.find), prefer the project’s standard concatenation/operator style (..), and use consistent response/header utilities (core.response.set_header).Example (helper extraction + phase separation):
local function log_phase_incoming_thread(premature, self, key, cost)
local conf = self.conf
local red, err = redis.new(conf)
if not red then
return red, err
end
return util.redis_log_phase_incoming(self, red, key, cost)
end
local function log_phase_incoming(self, key, cost, dry_run)
if dry_run then
return true
end
local ok, err = ngx_timer_at(0, log_phase_incoming_thread, self, key, cost)
if not ok then
core.log.error("failed to create timer: ", err)
return nil, err
end
return ok
end
function _M.incoming(self, key, cost, dry_run)
if get_phase() == "log" then
return log_phase_incoming(self, key, cost, dry_run)
end
-- main phase logic...
end