Skip to content

Commit a6c7f47

Browse files
authored
fix: release healthchecker on 0 nodes (#12126)
1 parent 1d3581c commit a6c7f47

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

apisix/upstream.lua

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ _M.set = set_directly
8282

8383

8484
local function release_checker(healthcheck_parent)
85+
if not healthcheck_parent or not healthcheck_parent.checker then
86+
return
87+
end
8588
local checker = healthcheck_parent.checker
8689
core.log.info("try to release checker: ", tostring(checker))
8790
checker:delayed_clear(3)
@@ -327,6 +330,7 @@ function _M.set_by_route(route, api_ctx)
327330

328331
local nodes_count = up_conf.nodes and #up_conf.nodes or 0
329332
if nodes_count == 0 then
333+
release_checker(up_conf.parent)
330334
return HTTP_CODE_UPSTREAM_UNAVAILABLE, "no valid upstream node"
331335
end
332336

t/discovery/reset-healthchecker.t

+55
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,58 @@ ok
112112
--- timeout: 22
113113
--- no_error_log
114114
unhealthy TCP increment (10/30)
115+
116+
117+
118+
=== TEST 2: Validate healthchecker deletion on node count reduces to 0
119+
--- http_config
120+
server {
121+
listen 3000 ;
122+
location / {
123+
return 200 'ok';
124+
}
125+
}
126+
--- apisix_yaml
127+
routes:
128+
-
129+
uris:
130+
- /
131+
upstream_id: 1
132+
--- config
133+
location /t {
134+
content_by_lua_block {
135+
local t = require("lib.test_admin").test
136+
local discovery = require("apisix.discovery.init").discovery
137+
discovery.mock = {
138+
nodes = function()
139+
return {
140+
{host = "127.0.0.1", port = 3000, weight = 50},
141+
{host = "127.0.0.1", port = 8000, weight = 50},
142+
}
143+
end
144+
}
145+
local http = require "resty.http"
146+
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/"
147+
local httpc = http.new()
148+
local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
149+
ngx.sleep(5)
150+
discovery.mock = {
151+
nodes = function()
152+
return {
153+
}
154+
end
155+
}
156+
local http = require "resty.http"
157+
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/"
158+
local httpc = http.new()
159+
local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})
160+
ngx.status = res.status
161+
ngx.sleep(5)
162+
}
163+
}
164+
--- request
165+
GET /t
166+
--- timeout: 22
167+
--- no_error_log
168+
unhealthy TCP increment (10/30)
169+
--- error_code: 503

0 commit comments

Comments
 (0)