Skip to content

Commit

Permalink
refactor(tools/time): updating references of time functions (#9294)
Browse files Browse the repository at this point in the history
We are refactoring kong.tools.*, and trying to updating all references of kong.tools.time, which will make our code more clear and easy to maintain.

KAG-3145

(cherry picked from #13093)

Co-authored-by: Chrono <chrono_cpp@me.com>
  • Loading branch information
team-gateway-bot and chronolaw authored May 29, 2024
1 parent cd675c9 commit f319645
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
10 changes: 5 additions & 5 deletions kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ local instrumentation = require "kong.tracing.instrumentation"
local process = require "ngx.process"
local tablepool = require "tablepool"
local table_new = require "table.new"
local utils = require "kong.tools.utils"
local emmy_debugger = require "kong.tools.emmy_debugger"
local get_ctx_table = require("resty.core.ctx").get_ctx_table
local admin_gui = require "kong.admin_gui"
Expand All @@ -101,6 +100,7 @@ local reports = require "kong.reports"
local pl_file = require "pl.file"
local req_dyn_hook = require "kong.dynamic_hook"
local uuid = require("kong.tools.uuid").uuid
local kong_time = require("kong.tools.time")


local internal_proxies = require "kong.enterprise_edition.proxies"
Expand Down Expand Up @@ -148,10 +148,10 @@ local set_more_tries = ngx_balancer.set_more_tries
local enable_keepalive = ngx_balancer.enable_keepalive


local time_ns = utils.time_ns
local get_now_ms = utils.get_now_ms
local get_start_time_ms = utils.get_start_time_ms
local get_updated_now_ms = utils.get_updated_now_ms
local time_ns = kong_time.time_ns
local get_now_ms = kong_time.get_now_ms
local get_start_time_ms = kong_time.get_start_time_ms
local get_updated_now_ms = kong_time.get_updated_now_ms


local req_dyn_hook_run_hook = req_dyn_hook.run_hook
Expand Down
2 changes: 1 addition & 1 deletion kong/runloop/balancer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ local table = table
local table_concat = table.concat
local run_hook = hooks.run_hook
local var = ngx.var
local get_updated_now_ms = utils.get_updated_now_ms
local get_updated_now_ms = require("kong.tools.time").get_updated_now_ms
local is_http_module = ngx.config.subsystem == "http"

local CRIT = ngx.CRIT
Expand Down
3 changes: 1 addition & 2 deletions kong/runloop/balancer/targets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ local dns_client = require "kong.resty.dns.client"
local upstreams = require "kong.runloop.balancer.upstreams"
local balancers = require "kong.runloop.balancer.balancers"
local dns_utils = require "kong.resty.dns.utils"
local utils = require "kong.tools.utils"

local ngx = ngx
local null = ngx.null
Expand All @@ -30,7 +29,7 @@ local tonumber = tonumber
local table_sort = table.sort
local assert = assert
local exiting = ngx.worker.exiting
local get_updated_now_ms = utils.get_updated_now_ms
local get_updated_now_ms = require("kong.tools.time").get_updated_now_ms

local CRIT = ngx.CRIT
local DEBUG = ngx.DEBUG
Expand Down
3 changes: 1 addition & 2 deletions kong/timing/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
-- [ END OF LICENSE 0867164ffc95e54f04670b5169c09574bdbd9bba ]

local cjson = require("cjson.safe")
local utils = require("kong.tools.utils")

local ngx_get_phase = ngx.get_phase
local ngx_re_gmatch = ngx.re.gmatch
Expand All @@ -16,7 +15,7 @@ local setmetatable = setmetatable
local table_insert = table.insert
local table_remove = table.remove

local get_cur_msec = utils.get_updated_monotonic_ms
local get_cur_msec = require("kong.tools.time").get_updated_monotonic_ms

local assert = assert

Expand Down
2 changes: 1 addition & 1 deletion kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ do
"kong.tools.table",
"kong.tools.uuid",
"kong.tools.rand",
"kong.tools.time",
-- ]] keep it here for compatibility
"kong.tools.string",
"kong.tools.time",
"kong.tools.ip",
"kong.tools.http",
"kong.tools.ssl",
Expand Down
24 changes: 13 additions & 11 deletions spec/01-unit/05-utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -823,22 +823,24 @@ describe("Utils", function()
end)

describe("nginx_conf_time_to_seconds()", function()
local nginx_conf_time_to_seconds = require("kong.tools.time").nginx_conf_time_to_seconds

it("returns value in seconds", function()
assert.equal(5, utils.nginx_conf_time_to_seconds("5"))
assert.equal(5, utils.nginx_conf_time_to_seconds("5s"))
assert.equal(60, utils.nginx_conf_time_to_seconds("60s"))
assert.equal(60, utils.nginx_conf_time_to_seconds("1m"))
assert.equal(120, utils.nginx_conf_time_to_seconds("2m"))
assert.equal(7200, utils.nginx_conf_time_to_seconds("2h"))
assert.equal(172800, utils.nginx_conf_time_to_seconds("2d"))
assert.equal(1209600, utils.nginx_conf_time_to_seconds("2w"))
assert.equal(5184000, utils.nginx_conf_time_to_seconds("2M"))
assert.equal(63072000, utils.nginx_conf_time_to_seconds("2y"))
assert.equal(5, nginx_conf_time_to_seconds("5"))
assert.equal(5, nginx_conf_time_to_seconds("5s"))
assert.equal(60, nginx_conf_time_to_seconds("60s"))
assert.equal(60, nginx_conf_time_to_seconds("1m"))
assert.equal(120, nginx_conf_time_to_seconds("2m"))
assert.equal(7200, nginx_conf_time_to_seconds("2h"))
assert.equal(172800, nginx_conf_time_to_seconds("2d"))
assert.equal(1209600, nginx_conf_time_to_seconds("2w"))
assert.equal(5184000, nginx_conf_time_to_seconds("2M"))
assert.equal(63072000, nginx_conf_time_to_seconds("2y"))
end)

it("throws an error on bad argument", function()
assert.has_error(function()
utils.nginx_conf_time_to_seconds("abcd")
nginx_conf_time_to_seconds("abcd")
end, "bad argument #1 'str'")
end)
end)
Expand Down

0 comments on commit f319645

Please sign in to comment.