Skip to content

Commit 2f0b80a

Browse files
authored
fix: should fail fast when etcd returns error (#128)
1 parent fdf21e2 commit 2f0b80a

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

lib/resty/etcd/v3.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ local function _request_uri(self, endpoint, method, uri, opts, timeout, ignore_a
8181
if err then
8282
if health_check.conf ~= nil then
8383
health_check.report_failure(endpoint.http_host)
84+
err = endpoint.http_host .. ": " .. err
8485
end
8586
return nil, err
8687
end
@@ -604,7 +605,7 @@ local function request_chunk(self, endpoint, method, scheme, host, port, path, o
604605
if health_check.conf ~= nil then
605606
health_check.report_failure(endpoint.http_host)
606607
end
607-
return nil, err
608+
return nil, endpoint.http_host .. ": " .. err
608609
end
609610

610611
local res
@@ -647,9 +648,10 @@ local function request_chunk(self, endpoint, method, scheme, host, port, path, o
647648
if health_check.conf ~= nil then
648649
health_check.report_failure(endpoint.http_host)
649650
end
651+
return nil, endpoint.http_host .. ": " .. body.error.http_status
650652
end
651653

652-
if body.result.events then
654+
if body.result and body.result.events then
653655
for _, event in ipairs(body.result.events) do
654656
if event.kv.value then -- DELETE not have value
655657
event.kv.value = decode_base64(event.kv.value or "")

t/v3/auth.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ __DATA__
6464
password = 'abc123',
6565
timeout = 3,
6666
http_host = {
67-
"http://127.0.0.1:12379",
67+
"http://127.0.0.1:12379",
6868
},
6969
})
7070
check_res(etcd, err)
@@ -118,7 +118,7 @@ uri: http://127.0.0.1:12379/v3/kv/deleterange, timeout: 3
118118
password = '123',
119119
timeout = 3,
120120
http_host = {
121-
"http://127.0.0.1:12379",
121+
"http://127.0.0.1:12379",
122122
},
123123
})
124124
check_res(etcd, err)

t/v3/health_check.t

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ failed to get ngx.shared dict: error_shm_name
134134
135135
136136
137-
=== TEST 4: trigger unhealthy
137+
=== TEST 4: trigger unhealthy with set
138138
--- http_config eval: $::HttpConfig
139139
--- config
140140
location /t {
@@ -157,19 +157,54 @@ failed to get ngx.shared dict: error_shm_name
157157
})
158158
159159
local res, err = etcd:set("/trigger_unhealthy", { a='abc'})
160-
ngx.say("done")
160+
ngx.say(err)
161161
}
162162
}
163163
--- request
164164
GET /t
165165
--- error_log eval
166166
qr/update endpoint: http:\/\/127.0.0.1:42379 to unhealthy/
167167
--- response_body
168-
done
168+
http://127.0.0.1:42379: connection refused
169+
170+
171+
172+
=== TEST 5: trigger unhealthy with watch
173+
--- http_config eval: $::HttpConfig
174+
--- config
175+
location /t {
176+
content_by_lua_block {
177+
local health_check, err = require "resty.etcd.health_check" .init({
178+
shm_name = "etcd_cluster_health_check",
179+
fail_timeout = 10,
180+
max_fails = 1,
181+
})
182+
183+
local etcd, err = require "resty.etcd" .new({
184+
protocol = "v3",
185+
http_host = {
186+
"http://127.0.0.1:42379",
187+
"http://127.0.0.1:22379",
188+
"http://127.0.0.1:32379",
189+
},
190+
})
191+
192+
local body_chunk_fun, err = etcd:watch("/trigger_unhealthy")
193+
if not body_chunk_fun then
194+
ngx.say(err)
195+
end
196+
}
197+
}
198+
--- request
199+
GET /t
200+
--- error_log eval
201+
qr/update endpoint: http:\/\/127.0.0.1:42379 to unhealthy/
202+
--- response_body
203+
http://127.0.0.1:42379: connection refused
169204
170205
171206
172-
=== TEST 5: fault count
207+
=== TEST 6: fault count
173208
--- http_config eval: $::HttpConfig
174209
--- config
175210
location /t {
@@ -209,7 +244,7 @@ GET /t
209244
210245
211246
212-
=== TEST 6: check endpoint is healthy
247+
=== TEST 7: check endpoint is healthy
213248
--- http_config eval: $::HttpConfig
214249
--- config
215250
location /t {
@@ -246,7 +281,7 @@ false
246281
247282
248283
249-
=== TEST 7: make sure `fail_timeout` works
284+
=== TEST 8: make sure `fail_timeout` works
250285
--- http_config eval: $::HttpConfig
251286
--- config
252287
location /t {
@@ -293,7 +328,7 @@ done
293328
294329
295330
296-
=== TEST 8: has no healthy etcd endpoint, directly return an error message
331+
=== TEST 9: has no healthy etcd endpoint, directly return an error message
297332
--- http_config eval: $::HttpConfig
298333
--- config
299334
location /t {
@@ -334,7 +369,7 @@ qr/has no healthy etcd endpoint available/
334369
335370
336371
337-
=== TEST 9: `health_check` shared by different etcd clients
372+
=== TEST 10: `health_check` shared by different etcd clients
338373
--- http_config eval: $::HttpConfig
339374
--- config
340375
location /t {
@@ -383,7 +418,7 @@ qr/update endpoint: http:\/\/127.0.0.1:42379 to unhealthy/
383418
384419
385420
386-
=== TEST 10: mock etcd error and report fault
421+
=== TEST 11: mock etcd error and report fault
387422
--- http_config eval: $::HttpConfig
388423
--- config
389424
location /v3/auth/authenticate {

0 commit comments

Comments
 (0)