Skip to content

Commit

Permalink
Use mtime instead of ctime
Browse files Browse the repository at this point in the history
In order to avoid unnecessary reloading of the yaml configuration. The time used to detect the last change to the config file is changed from `change` which is the time of the last file status change to the `modification` time wich is the time of the last data modification.

For more information also checkou http://lunarmodules.github.io/luafilesystem/manual.html#reference and https://linux.die.net/man/2/stat

Fixes: #9051
  • Loading branch information
boekkooi-lengoo committed Jan 10, 2024
1 parent 8a2d740 commit c3c014b
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions apisix/core/config_yaml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ local mt = {


local apisix_yaml
local apisix_yaml_ctime
local apisix_yaml_mtime
local function read_apisix_yaml(premature, pre_mtime)
if premature then
return
Expand All @@ -74,9 +74,8 @@ local function read_apisix_yaml(premature, pre_mtime)
return
end

-- log.info("change: ", json.encode(attributes))
local last_change_time = attributes.change
if apisix_yaml_ctime == last_change_time then
local last_modification_time = attributes.modification
if apisix_yaml_mtime == last_modification_time then
return
end

Expand Down Expand Up @@ -114,7 +113,7 @@ local function read_apisix_yaml(premature, pre_mtime)
end

apisix_yaml = apisix_yaml_new
apisix_yaml_ctime = last_change_time
apisix_yaml_mtime = last_modification_time
log.warn("config file ", apisix_yaml_path, " reloaded.")
end

Expand All @@ -124,12 +123,12 @@ local function sync_data(self)
return nil, "missing 'key' arguments"
end

if not apisix_yaml_ctime then
if not apisix_yaml_mtime then
log.warn("wait for more time")
return nil, "failed to read local file " .. apisix_yaml_path
end

if self.conf_version == apisix_yaml_ctime then
if self.conf_version == apisix_yaml_mtime then
return true
end

Expand All @@ -138,7 +137,7 @@ local function sync_data(self)
if not items then
self.values = new_tab(8, 0)
self.values_hash = new_tab(0, 8)
self.conf_version = apisix_yaml_ctime
self.conf_version = apisix_yaml_mtime
return true
end

Expand All @@ -155,7 +154,7 @@ local function sync_data(self)
self.values_hash = new_tab(0, 1)

local item = items
local conf_item = {value = item, modifiedIndex = apisix_yaml_ctime,
local conf_item = {value = item, modifiedIndex = apisix_yaml_mtime,
key = "/" .. self.key}

local data_valid = true
Expand Down Expand Up @@ -202,7 +201,7 @@ local function sync_data(self)
end

local key = item.id or "arr_" .. i
local conf_item = {value = item, modifiedIndex = apisix_yaml_ctime,
local conf_item = {value = item, modifiedIndex = apisix_yaml_mtime,
key = "/" .. self.key .. "/" .. key}

if data_valid and self.item_schema then
Expand Down Expand Up @@ -236,7 +235,7 @@ local function sync_data(self)
end
end

self.conf_version = apisix_yaml_ctime
self.conf_version = apisix_yaml_mtime
return true
end

Expand Down

0 comments on commit c3c014b

Please sign in to comment.