Skip to content

Commit

Permalink
feat(tools): add get_updated_now() in time tools (#13575)
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw authored Sep 4, 2024
1 parent 4abf3ba commit 78bee02
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 23 deletions.
8 changes: 3 additions & 5 deletions kong/cluster_events/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local insert = table.insert
local ngx_log = ngx.log
local ngx_now = ngx.now
local timer_at = ngx.timer.at
local ngx_update_time = ngx.update_time
local now_updated = require("kong.tools.time").get_updated_now

local knode = kong and kong.node or require "kong.pdk.node".new()
local concurrency = require "kong.concurrency"
Expand Down Expand Up @@ -245,8 +245,7 @@ local function process_event(self, row, local_start_time)
local delay

if row.nbf and row.now then
ngx_update_time()
local now = row.now + max(ngx_now() - local_start_time, 0)
local now = row.now + max(now_updated() - local_start_time, 0)
delay = max(row.nbf - now, 0)
end

Expand Down Expand Up @@ -308,8 +307,7 @@ local function poll(self)
end
end

ngx_update_time()
local local_start_time = ngx_now()
local local_start_time = now_updated()
for i = 1, count do
local ok, err = process_event(self, rows[i], local_start_time)
if not ok then
Expand Down
5 changes: 2 additions & 3 deletions kong/clustering/control_plane.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ local exiting = ngx.worker.exiting
local worker_id = ngx.worker.id
local ngx_time = ngx.time
local ngx_now = ngx.now
local ngx_update_time = ngx.update_time
local ngx_var = ngx.var
local table_insert = table.insert
local table_remove = table.remove
local sub = string.sub
local isempty = require("table.isempty")
local sleep = ngx.sleep
local now_updated = require("kong.tools.time").get_updated_now


local plugins_list_to_map = compat.plugins_list_to_map
Expand Down Expand Up @@ -173,8 +173,7 @@ function _M:push_config()
n = n + 1
end

ngx_update_time()
local duration = ngx_now() - start
local duration = now_updated() - start
ngx_log(ngx_DEBUG, _log_prefix, "config pushed to ", n, " data-plane nodes in ", duration, " seconds")
end

Expand Down
8 changes: 1 addition & 7 deletions kong/db/strategies/postgres/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ local floor = math.floor
local type = type
local ngx = ngx
local timer_every = ngx.timer.every
local update_time = ngx.update_time
local get_phase = ngx.get_phase
local null = ngx.null
local now = ngx.now
Expand All @@ -31,6 +30,7 @@ local utils_toposort = db_utils.topological_sort
local insert = table.insert
local table_merge = require("kong.tools.table").table_merge
local strip = require("kong.tools.string").strip
local now_updated = require("kong.tools.time").get_updated_now


local WARN = ngx.WARN
Expand All @@ -54,12 +54,6 @@ local ADMIN_API_PHASE = kong_global.phases.admin_api
local CORE_ENTITIES = constants.CORE_ENTITIES


local function now_updated()
update_time()
return now()
end


local function iterator(rows)
local i = 0
return function()
Expand Down
9 changes: 1 addition & 8 deletions kong/db/strategies/postgres/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ local decode_base64 = ngx.decode_base64
local encode_array = arrays.encode_array
local encode_json = json.encode_json
local setmetatable = setmetatable
local update_time = ngx.update_time
local get_phase = ngx.get_phase
local tonumber = tonumber
local concat = table.concat
Expand All @@ -27,12 +26,12 @@ local null = ngx.null
local type = type
local load = load
local find = string.find
local now = ngx.now
local fmt = string.format
local rep = string.rep
local sub = string.sub
local log = ngx.log
local cycle_aware_deep_copy = require("kong.tools.table").cycle_aware_deep_copy
local now_updated = require("kong.tools.time").get_updated_now


local NOTICE = ngx.NOTICE
Expand All @@ -45,12 +44,6 @@ local function noop(...)
end


local function now_updated()
update_time()
return now()
end


-- @param name Query name, for debugging purposes
-- @param query A string describing an array of single-quoted strings which
-- contain parts of an SQL query including numeric placeholders like $0, $1, etc.
Expand Down
5 changes: 5 additions & 0 deletions kong/tools/time.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ do
local start_time = ngx.req.start_time
local monotonic_msec = require("resty.core.time").monotonic_msec

function _M.get_updated_now()
update_time()
return now() -- time is kept in seconds with millisecond resolution.
end

function _M.get_now_ms()
return now() * 1000 -- time is kept in seconds with millisecond resolution.
end
Expand Down

1 comment on commit 78bee02

@github-actions
Copy link
Contributor

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:78bee0256926905d3fcf5e87d8d85bc19d017757
Artifacts available https://github.com/Kong/kong/actions/runs/10693781757

Please sign in to comment.