Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(consul): retry connecting after a delay #4979

Merged
merged 1 commit into from Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions apisix/discovery/consul_kv.lua
Expand Up @@ -17,6 +17,7 @@
local require = require
local local_conf = require("apisix.core.config_local").local_conf()
local core = require("apisix.core")
local core_sleep = require("apisix.core.utils").sleep
local resty_consul = require('resty.consul')
local cjson = require('cjson')
local http = require('resty.http')
Expand Down Expand Up @@ -307,7 +308,7 @@ local function show_dump_file()
end


function _M.connect(premature, consul_server)
function _M.connect(premature, consul_server, retry_delay)
if premature then
return
end
Expand All @@ -331,6 +332,15 @@ function _M.connect(premature, consul_server)
", got result: ", json_delay_encode(result, true),
", with error: ", error_info)

if not retry_delay then
retry_delay = 1
else
retry_delay = retry_delay * 4
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
end

log.warn("retry connecting consul after ", retry_delay, " seconds")
core_sleep(retry_delay)

goto ERR
end

Expand Down Expand Up @@ -370,7 +380,7 @@ function _M.connect(premature, consul_server)
:: ERR ::
local keepalive = consul_server.keepalive
if keepalive then
local ok, err = ngx_timer_at(0, _M.connect, consul_server)
local ok, err = ngx_timer_at(0, _M.connect, consul_server, retry_delay)
if not ok then
log.error("create ngx_timer_at got error: ", err)
return
Expand Down
48 changes: 48 additions & 0 deletions t/discovery/consul_kv.t
Expand Up @@ -514,3 +514,51 @@ location /sleep {
qr/ok\n/,
qr/server 1\n/
]



=== TEST 12: retry when Consul can't be reached (long connect type)
--- yaml_config
apisix:
node_listen: 1984
config_center: yaml
enable_admin: false

discovery:
consul_kv:
servers:
- "http://127.0.0.1:8501"
keepalive: true
fetch_interval: 3
default_service:
host: "127.0.0.1"
port: 20999
#END
--- apisix_yaml
routes:
-
uri: /*
upstream:
service_name: http://127.0.0.1:8501/v1/kv/upstreams/webpages/
discovery_type: consul_kv
type: roundrobin
#END
--- timeout: 4
--- config
location /sleep {
content_by_lua_block {
local args = ngx.req.get_uri_args()
local sec = args.sec or "2"
ngx.sleep(tonumber(sec))
ngx.say("ok")
}
}
--- request
GET /sleep?sec=3
--- response_body
ok
--- grep_error_log eval
qr/retry connecting consul after \d seconds/
--- grep_error_log_out
retry connecting consul after 1 seconds
retry connecting consul after 4 seconds