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(healthcheck): fixed incorrect default http_statuses when new() was called multiple times #83

Merged
merged 1 commit into from
Oct 2, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 45 additions & 43 deletions lib/resty/healthcheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1257,52 +1257,52 @@ local function fill_in_settings(opts, defaults, ctx)
return obj
end


local defaults = {
name = NO_DEFAULT,
shm_name = NO_DEFAULT,
type = NO_DEFAULT,
status_ver = 0,
checks = {
active = {
type = "http",
timeout = 1,
concurrency = 10,
http_path = "/",
https_sni = NO_DEFAULT,
https_verify_certificate = true,
headers = {""},
healthy = {
interval = 0, -- 0 = disabled by default
http_statuses = { 200, 302 },
successes = 2,
},
unhealthy = {
interval = 0, -- 0 = disabled by default
http_statuses = { 429, 404,
500, 501, 502, 503, 504, 505 },
tcp_failures = 2,
timeouts = 3,
http_failures = 5,
},
},
passive = {
type = "http",
healthy = {
http_statuses = { 200, 201, 202, 203, 204, 205, 206, 207, 208, 226,
300, 301, 302, 303, 304, 305, 306, 307, 308 },
successes = 5,
local function get_defaults()
return {
name = NO_DEFAULT,
shm_name = NO_DEFAULT,
type = NO_DEFAULT,
status_ver = 0,
checks = {
active = {
type = "http",
timeout = 1,
concurrency = 10,
http_path = "/",
https_sni = NO_DEFAULT,
https_verify_certificate = true,
headers = {""},
healthy = {
interval = 0, -- 0 = disabled by default
http_statuses = { 200, 302 },
successes = 2,
},
unhealthy = {
interval = 0, -- 0 = disabled by default
http_statuses = { 429, 404,
500, 501, 502, 503, 504, 505 },
tcp_failures = 2,
timeouts = 3,
http_failures = 5,
},
},
unhealthy = {
http_statuses = { 429, 500, 503 },
tcp_failures = 2,
timeouts = 7,
http_failures = 5,
passive = {
type = "http",
healthy = {
http_statuses = { 200, 201, 202, 203, 204, 205, 206, 207, 208, 226,
300, 301, 302, 303, 304, 305, 306, 307, 308 },
successes = 5,
},
unhealthy = {
http_statuses = { 429, 500, 503 },
tcp_failures = 2,
timeouts = 7,
http_failures = 5,
},
},
},
},
}

}
end

local function to_set(tbl, key)
local set = {}
Expand Down Expand Up @@ -1374,6 +1374,8 @@ function _M.new(opts)
assert(worker_events.configured(), "please configure the " ..
"'lua-resty-worker-events' module before using 'lua-resty-healthcheck'")

-- create a new defaults table within new() as defaults table will be modified by to_set function later
local defaults = get_defaults()
local self = fill_in_settings(opts, defaults)

assert(self.checks.active.healthy.successes < 255, "checks.active.healthy.successes must be at most 254")
Expand Down
62 changes: 61 additions & 1 deletion t/00-new.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use Cwd qw(cwd);

workers(1);

plan tests => repeat_each() * (blocks() * 3) - 2;
plan tests => repeat_each() * (blocks() * 3) - 3;

my $pwd = cwd();

Expand Down Expand Up @@ -251,3 +251,63 @@ false
false
false
false

=== TEST 8: new() was called multiple times with input which do not have healthy/unhealthy config
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
local healthcheck = require("resty.healthcheck")

-- CASE 1: default http_statuses should be set correctly when new() was called multiple times
local hc1 = healthcheck.new({
name = "testing",
shm_name = "test_shm",
checks = {
active = {
type = "http",
},
}
})
-- make sure checks.active.healthy.http_statuses is filled with defaults
ngx.say(hc1.checks.active.healthy.http_statuses[200])

local hc2 = healthcheck.new({
name = "testing",
shm_name = "test_shm",
checks = {
active = {
type = "http",
},
}
})
-- make sure checks.active.healthy.http_statuses is filled with defaults
ngx.say(hc2.checks.active.healthy.http_statuses[200])

-- CASE 2: the given http_statuses should not be overridden by default
local hc3 = healthcheck.new({
name = "testing",
shm_name = "test_shm",
checks = {
active = {
type = "http",
healthy = {
http_statuses = {201}
}
},
}
})
-- make sure defaults won't override the given input
ngx.say(hc3.checks.active.healthy.http_statuses[200])
ngx.say(hc3.checks.active.healthy.http_statuses[201])
}
}
--- request
GET /t
--- response_body
true
true
nil
true