Skip to content

Commit 78bee02

Browse files
authored
feat(tools): add get_updated_now() in time tools (#13575)
https://konghq.atlassian.net/browse/KAG-5280
1 parent 4abf3ba commit 78bee02

File tree

5 files changed

+12
-23
lines changed

5 files changed

+12
-23
lines changed

kong/cluster_events/init.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ local insert = table.insert
1010
local ngx_log = ngx.log
1111
local ngx_now = ngx.now
1212
local timer_at = ngx.timer.at
13-
local ngx_update_time = ngx.update_time
13+
local now_updated = require("kong.tools.time").get_updated_now
1414

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

247247
if row.nbf and row.now then
248-
ngx_update_time()
249-
local now = row.now + max(ngx_now() - local_start_time, 0)
248+
local now = row.now + max(now_updated() - local_start_time, 0)
250249
delay = max(row.nbf - now, 0)
251250
end
252251

@@ -308,8 +307,7 @@ local function poll(self)
308307
end
309308
end
310309

311-
ngx_update_time()
312-
local local_start_time = ngx_now()
310+
local local_start_time = now_updated()
313311
for i = 1, count do
314312
local ok, err = process_event(self, rows[i], local_start_time)
315313
if not ok then

kong/clustering/control_plane.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ local exiting = ngx.worker.exiting
2727
local worker_id = ngx.worker.id
2828
local ngx_time = ngx.time
2929
local ngx_now = ngx.now
30-
local ngx_update_time = ngx.update_time
3130
local ngx_var = ngx.var
3231
local table_insert = table.insert
3332
local table_remove = table.remove
3433
local sub = string.sub
3534
local isempty = require("table.isempty")
3635
local sleep = ngx.sleep
36+
local now_updated = require("kong.tools.time").get_updated_now
3737

3838

3939
local plugins_list_to_map = compat.plugins_list_to_map
@@ -173,8 +173,7 @@ function _M:push_config()
173173
n = n + 1
174174
end
175175

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

kong/db/strategies/postgres/connector.lua

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ local floor = math.floor
1919
local type = type
2020
local ngx = ngx
2121
local timer_every = ngx.timer.every
22-
local update_time = ngx.update_time
2322
local get_phase = ngx.get_phase
2423
local null = ngx.null
2524
local now = ngx.now
@@ -31,6 +30,7 @@ local utils_toposort = db_utils.topological_sort
3130
local insert = table.insert
3231
local table_merge = require("kong.tools.table").table_merge
3332
local strip = require("kong.tools.string").strip
33+
local now_updated = require("kong.tools.time").get_updated_now
3434

3535

3636
local WARN = ngx.WARN
@@ -54,12 +54,6 @@ local ADMIN_API_PHASE = kong_global.phases.admin_api
5454
local CORE_ENTITIES = constants.CORE_ENTITIES
5555

5656

57-
local function now_updated()
58-
update_time()
59-
return now()
60-
end
61-
62-
6357
local function iterator(rows)
6458
local i = 0
6559
return function()

kong/db/strategies/postgres/init.lua

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ local decode_base64 = ngx.decode_base64
1212
local encode_array = arrays.encode_array
1313
local encode_json = json.encode_json
1414
local setmetatable = setmetatable
15-
local update_time = ngx.update_time
1615
local get_phase = ngx.get_phase
1716
local tonumber = tonumber
1817
local concat = table.concat
@@ -27,12 +26,12 @@ local null = ngx.null
2726
local type = type
2827
local load = load
2928
local find = string.find
30-
local now = ngx.now
3129
local fmt = string.format
3230
local rep = string.rep
3331
local sub = string.sub
3432
local log = ngx.log
3533
local cycle_aware_deep_copy = require("kong.tools.table").cycle_aware_deep_copy
34+
local now_updated = require("kong.tools.time").get_updated_now
3635

3736

3837
local NOTICE = ngx.NOTICE
@@ -45,12 +44,6 @@ local function noop(...)
4544
end
4645

4746

48-
local function now_updated()
49-
update_time()
50-
return now()
51-
end
52-
53-
5447
-- @param name Query name, for debugging purposes
5548
-- @param query A string describing an array of single-quoted strings which
5649
-- contain parts of an SQL query including numeric placeholders like $0, $1, etc.

kong/tools/time.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ do
7878
local start_time = ngx.req.start_time
7979
local monotonic_msec = require("resty.core.time").monotonic_msec
8080

81+
function _M.get_updated_now()
82+
update_time()
83+
return now() -- time is kept in seconds with millisecond resolution.
84+
end
85+
8186
function _M.get_now_ms()
8287
return now() * 1000 -- time is kept in seconds with millisecond resolution.
8388
end

0 commit comments

Comments
 (0)