Skip to content

Commit a1815ae

Browse files
authored
fix: directly return an error message when there is no healthy node available (#112)
1 parent 1fe01f7 commit a1815ae

File tree

2 files changed

+86
-25
lines changed

2 files changed

+86
-25
lines changed

lib/resty/etcd/v3.lua

Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ local function choose_endpoint(self)
211211
return endpoint
212212
end
213213
end
214-
utils.log_warn("has no healthy endpoint")
214+
utils.log_warn("has no healthy etcd endpoint available")
215+
return nil, "has no healthy etcd endpoint available"
215216
end
216217

217218
self.init_count = (self.init_count or 0) + 1
@@ -265,8 +266,14 @@ function refresh_jwt_token(self, timeout)
265266
password = self.password,
266267
}
267268
}
268-
local endpoint = choose_endpoint(self)
269-
local res, err = _request_uri(self, endpoint, 'POST',
269+
270+
local endpoint, err = choose_endpoint(self)
271+
if not endpoint then
272+
return nil, err
273+
end
274+
275+
local res
276+
res, err = _request_uri(self, endpoint, 'POST',
270277
endpoint.full_prefix .. "/auth/authenticate",
271278
opts, timeout, true)
272279
self.requesting_token = false
@@ -340,7 +347,12 @@ local function set(self, key, val, attr)
340347
}
341348
}
342349

343-
local endpoint = choose_endpoint(self)
350+
local endpoint
351+
endpoint, err = choose_endpoint(self)
352+
if not endpoint then
353+
return nil, err
354+
end
355+
344356
local res
345357
res, err = _request_uri(self, endpoint, 'POST',
346358
endpoint.full_prefix .. "/kv/put",
@@ -448,7 +460,12 @@ local function get(self, key, attr)
448460
}
449461
}
450462

451-
local endpoint = choose_endpoint(self)
463+
local endpoint
464+
endpoint, err = choose_endpoint(self)
465+
if not endpoint then
466+
return nil, err
467+
end
468+
452469
local res
453470
res, err = _request_uri(self, endpoint, "POST",
454471
endpoint.full_prefix .. "/kv/range",
@@ -490,7 +507,11 @@ local function delete(self, key, attr)
490507
},
491508
}
492509

493-
local endpoint = choose_endpoint(self)
510+
local endpoint, err = choose_endpoint(self)
511+
if not endpoint then
512+
return nil, err
513+
end
514+
494515
return _request_uri(self, endpoint, "POST",
495516
endpoint.full_prefix .. "/kv/deleterange",
496517
opts, self.timeout)
@@ -514,7 +535,11 @@ local function txn(self, opts_arg, compare, success, failure)
514535
},
515536
}
516537

517-
local endpoint = choose_endpoint(self)
538+
local endpoint, err = choose_endpoint(self)
539+
if not endpoint then
540+
return nil, err
541+
end
542+
518543
return _request_uri(self, endpoint, "POST",
519544
endpoint.full_prefix .. "/kv/txn",
520545
opts, timeout or self.timeout)
@@ -726,9 +751,13 @@ local function watch(self, key, attr)
726751
need_cancel = need_cancel,
727752
}
728753

729-
local endpoint = choose_endpoint(self)
754+
local endpoint, err = choose_endpoint(self)
755+
if not endpoint then
756+
return nil, err
757+
end
730758

731-
local callback_fun, err, http_cli = request_chunk(self, endpoint, 'POST',
759+
local callback_fun, http_cli
760+
callback_fun, err, http_cli = request_chunk(self, endpoint, 'POST',
732761
endpoint.scheme,
733762
endpoint.host,
734763
endpoint.port,
@@ -959,7 +988,11 @@ function _M.grant(self, ttl, id)
959988
},
960989
}
961990

962-
local endpoint = choose_endpoint(self)
991+
local endpoint, err = choose_endpoint(self)
992+
if not endpoint then
993+
return nil, err
994+
end
995+
963996
return _request_uri(self, endpoint, "POST",
964997
endpoint.full_prefix .. "/lease/grant", opts)
965998
end
@@ -975,7 +1008,11 @@ function _M.revoke(self, id)
9751008
},
9761009
}
9771010

978-
local endpoint = choose_endpoint(self)
1011+
local endpoint, err = choose_endpoint(self)
1012+
if not endpoint then
1013+
return nil, err
1014+
end
1015+
9791016
return _request_uri(self, endpoint, "POST",
9801017
endpoint.full_prefix .. "/kv/lease/revoke", opts)
9811018
end
@@ -991,7 +1028,11 @@ function _M.keepalive(self, id)
9911028
},
9921029
}
9931030

994-
local endpoint = choose_endpoint(self)
1031+
local endpoint, err = choose_endpoint(self)
1032+
if not endpoint then
1033+
return nil, err
1034+
end
1035+
9951036
return _request_uri(self, endpoint, "POST",
9961037
endpoint.full_prefix .. "/lease/keepalive", opts)
9971038
end
@@ -1009,8 +1050,13 @@ function _M.timetolive(self, id, keys)
10091050
},
10101051
}
10111052

1012-
local endpoint = choose_endpoint(self)
1013-
local res, err = _request_uri(self, endpoint, "POST",
1053+
local endpoint, err = choose_endpoint(self)
1054+
if not endpoint then
1055+
return nil, err
1056+
end
1057+
1058+
local res
1059+
res, err = _request_uri(self, endpoint, "POST",
10141060
endpoint.full_prefix .. "/kv/lease/timetolive", opts)
10151061

10161062
if res and res.status == 200 then
@@ -1025,37 +1071,52 @@ function _M.timetolive(self, id, keys)
10251071
end
10261072

10271073
function _M.leases(self)
1028-
local endpoint = choose_endpoint(self)
1074+
local endpoint, err = choose_endpoint(self)
1075+
if not endpoint then
1076+
return nil, err
1077+
end
10291078
return _request_uri(self, endpoint, "POST",
10301079
endpoint.full_prefix .. "/lease/leases")
10311080
end
10321081

10331082

10341083
-- /version
10351084
function _M.version(self)
1036-
local endpoint = choose_endpoint(self)
1085+
local endpoint, err = choose_endpoint(self)
1086+
if not endpoint then
1087+
return nil, err
1088+
end
10371089
return _request_uri(self, endpoint, "GET",
10381090
endpoint.http_host .. "/version",
10391091
nil, self.timeout)
10401092
end
10411093

10421094
-- /stats
10431095
function _M.stats_leader(self)
1044-
local endpoint = choose_endpoint(self)
1096+
local endpoint, err = choose_endpoint(self)
1097+
if not endpoint then
1098+
return nil, err
1099+
end
10451100
return _request_uri(self, endpoint, "GET",
10461101
endpoint.http_host .. "/v2/stats/leader",
10471102
nil, self.timeout)
10481103
end
10491104

10501105
function _M.stats_self(self)
1051-
local endpoint = choose_endpoint(self)
1106+
local endpoint, err = choose_endpoint(self)
1107+
if not endpoint then
1108+
return nil, err
1109+
end
10521110
return _request_uri(self, endpoint, "GET",
10531111
endpoint.http_host .. "/v2/stats/self",
10541112
nil, self.timeout)
10551113
end
10561114

10571115
function _M.stats_store(self)
1058-
local endpoint = choose_endpoint(self)
1116+
local endpoint, err = choose_endpoint(self)
1117+
if not endpoint then
1118+
return nil, err
1119+
end
10591120
return _request_uri(self, endpoint, "GET",
10601121
endpoint.http_host .. "/v2/stats/store",
10611122
nil, self.timeout)

t/v3/health_check.t

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ done
293293
294294
295295
296-
=== TEST 8: has no healthy etcd endpoint, follow old style
296+
=== TEST 8: has no healthy etcd endpoint, directly return an error message
297297
--- http_config eval: $::HttpConfig
298298
--- config
299299
location /t {
@@ -320,17 +320,17 @@ done
320320
health_check.report_failure("http://127.0.0.1:32379")
321321
322322
local res, err = etcd:set("/no_healthy_endpoint", "hello")
323-
check_res(etcd, err)
324-
325-
ngx.say("done")
323+
ngx.say(err)
326324
}
327325
}
328326
--- request
329327
GET /t
330328
--- response_body
331-
done
329+
has no healthy etcd endpoint available
330+
--- no_error_log
331+
[error]
332332
--- error_log eval
333-
qr/has no healthy endpoint/
333+
qr/has no healthy etcd endpoint available/
334334
335335
336336

0 commit comments

Comments
 (0)