Skip to content

Commit f75f4c5

Browse files
authored
fix: the returned result may be nil when max retry is exceeded (#150)
1 parent 14d4440 commit f75f4c5

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

lib/resty/etcd/v3.lua

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,26 @@ local function _request_uri(self, method, uri, opts, timeout, ignore_auth)
142142
local res
143143
if health_check.conf.retry then
144144
local max_retry = #self.endpoints * health_check.conf.max_fails + 1
145-
for _ = 1, max_retry do
145+
for i = 1, max_retry do
146146
res, err = http_request_uri(self, http_cli, method, uri, body, headers, keepalive)
147-
if err then
148-
if err == "has no healthy etcd endpoint available" then
149-
return nil, err
150-
end
151-
utils.log_warn(err .. ". Retrying")
152-
else
147+
if res then
153148
break
154149
end
150+
151+
if err == "has no healthy etcd endpoint available" then
152+
return nil, err
153+
end
154+
155+
if i < max_retry then
156+
utils.log_warn(err .. ". Retrying")
157+
end
155158
end
156159
else
157160
res, err = http_request_uri(self, http_cli, method, uri, body, headers, keepalive)
158-
if err then
159-
return nil, err
160-
end
161+
end
162+
163+
if err then
164+
return nil, err
161165
end
162166

163167
if not typeof.string(res.body) then
@@ -641,22 +645,26 @@ local function request_chunk(self, method, path, opts, timeout)
641645
local endpoint
642646
if health_check.conf.retry then
643647
local max_retry = #self.endpoints * health_check.conf.max_fails + 1
644-
for _ = 1, max_retry do
648+
for i = 1, max_retry do
645649
endpoint, err = http_request_chunk(self, http_cli)
646-
if err then
647-
utils.log_warn(err .. ". Retrying")
648-
if err == "has no healthy etcd endpoint available" then
649-
return nil, err
650-
end
651-
else
650+
if endpoint then
652651
break
653652
end
653+
654+
if err == "has no healthy etcd endpoint available" then
655+
return nil, err
656+
end
657+
658+
if i < max_retry then
659+
utils.log_warn(err .. ". Retrying")
660+
end
654661
end
655662
else
656663
endpoint, err = http_request_chunk(self, http_cli)
657-
if err then
658-
return nil, err
659-
end
664+
end
665+
666+
if err then
667+
return nil, err
660668
end
661669

662670
local res

0 commit comments

Comments
 (0)