Skip to content

Commit

Permalink
feat: increase the delay before resync
Browse files Browse the repository at this point in the history
It improves the behavior under apache#2899.
  • Loading branch information
spacewander committed Dec 7, 2020
1 parent c41aa85 commit aa020ca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 10 additions & 2 deletions apisix/core/config_etcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ local tonumber = tonumber
local xpcall = xpcall
local debug = debug
local error = error
local rand = math.random()
local created_obj = {}


Expand Down Expand Up @@ -518,8 +519,10 @@ local function _automatic_fetch(premature, self)
self.last_err = nil
end
end
ngx_sleep(0.5)

ngx_sleep(self.resync_delay + rand() * 0.5 * self.resync_delay)
elseif not ok then
-- no error. reentry the sync with different state
ngx_sleep(0.05)
end

Expand All @@ -528,7 +531,7 @@ local function _automatic_fetch(premature, self)
if not ok then
log.error("failed to fetch data from etcd: ", err, ", ",
tostring(self))
ngx_sleep(3)
ngx_sleep(self.resync_delay + rand() * 0.5 * self.resync_delay)
break
end
end
Expand Down Expand Up @@ -559,6 +562,10 @@ function _M.new(key, opts)
etcd_conf.ssl_verify = false
end

if not etcd_conf.resync_delay or etcd_conf.resync_delay < 0 then
etcd_conf.resync_delay = 10
end

local automatic = opts and opts.automatic
local item_schema = opts and opts.item_schema
local filter_fun = opts and opts.filter
Expand All @@ -582,6 +589,7 @@ function _M.new(key, opts)
prev_index = 0,
last_err = nil,
last_err_time = nil,
resync_delay = etcd_conf.resync_delay,
timeout = timeout,
single_item = single_item,
filter = filter_fun,
Expand Down
1 change: 1 addition & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ etcd:
# e.g. "https://127.0.0.1:2379".
prefix: "/apisix" # apisix configurations prefix
timeout: 30 # 30 seconds
# resync_delay: 5 # when sync failed and a rest is needed, resync after the configured seconds plus 50% random jitter
# user: root # root username for etcd
# password: 5tHkHhYkjr6cQY # root password for etcd
tls:
Expand Down

0 comments on commit aa020ca

Please sign in to comment.