A health check library for OpenResty.
http {
lua_shared_dict test_shm 8m;
lua_shared_dict my_worker_events 8m;
init_worker_by_lua_block {
local we = require "resty.worker.events"
local ok, err = we.configure({
shm = "my_worker_events",
interval = 0.1
})
if not ok then
ngx.log(ngx.ERR, "failed to configure worker events: ", err)
return
end
local healthcheck = require("resty.healthcheck")
local checker = healthcheck.new({
name = "testing",
shm_name = "test_shm",
checks = {
active = {
type = "https",
http_path = "/status",
healthy = {
interval = 2,
successes = 1,
},
unhealthy = {
interval = 1,
http_failures = 2,
}
},
}
})
local ok, err = checker:add_target("127.0.0.1", 8080, "example.com", false)
local handler = function(target, eventname, sourcename, pid)
ngx.log(ngx.DEBUG,"Event from: ", sourcename)
if eventname == checker.events.remove
-- a target was removed
ngx.log(ngx.DEBUG,"Target removed: ",
target.ip, ":", target.port, " ", target.hostname)
elseif eventname == checker.events.healthy
-- target changed state, or was added
ngx.log(ngx.DEBUG,"Target switched to healthy: ",
target.ip, ":", target.port, " ", target.hostname)
elseif eventname == checker.events.unhealthy
-- target changed state, or was added
ngx.log(ngx.DEBUG,"Target switched to unhealthy: ",
target.ip, ":", target.port, " ", target.hostname)
else
-- unknown event
end
end
}
}
This library supports performing active and passive health checks on arbitrary hosts.
Control of the library happens via its programmatic API. Consumption of its events happens via the lua-resty-worker-events library.
Targets are added using checker:add_target(host, port)
.
Changes in status ("healthy" or "unhealthy") are broadcasted via worker-events.
Active checks are executed in the background based on the specified timer intervals.
For passive health checks, the library receives explicit notifications via its
programmatic API using functions such as checker:report_http_status(host, port, status)
.
See the online LDoc documentation for the complete API.
Versioning is strictly based on Semantic Versioning
- Fix: avoid raising worker events for new targets that were marked for delayed removal, i.e. targets that already exist in memory only need the removal flag cleared when added back. #122
- Fix: improvements to ensure the proper securing of shared resources to avoid race conditions and clearly report failure states. #112, #113, #114.
- Fix: reduce the frequency of checking for unused targets, reducing the number of locks created. #116
- Fix accept any lua-resty-events
0.1.x
release. #118
- Feature: introduce support to lua-resty-events module in addition to lua-resty-worker-events support. With this addition, the lua-resty-healthcheck luarocks package does not require a specific event-sharing module anymore, but you are still required to provide either lua-resty-worker-events or lua-resty-events. #105
- Change: if available, lua-resty-healthcheck now uses
string.buffer
, the new LuaJIT's serialization API. If it is unavailable, lua-resty-healthcheck fallbacks to cjson. #109
- Fix: avoid breaking active health checks when adding or removing targets. #93
- New option
checks.active.headers
supports one or more lists of values indexed by header name. #87 - Introduce dealyed_clear() function, used to remove addresses after a time interval. This function may be used when an address is being removed but may be added again before the interval expires, keeping its health status. #88
- Fix: avoid breaking active health checks when adding or removing targets. #100
- Fix: prevent new active checks being scheduled while a health check is running. #72
- Fix: remove event watcher when stopping an active health check. #74; fixes Kong issue #7406
- Fix: make sure that a single worker will actively check hosts' statuses. #67
- Use a single timer to actively health check targets. This reduces the number of timers used by health checkers, as they used to use two timers by each target. #62
- Adds support to mTLS to active healthchecks. This feature can be used adding
the fields
ssl_cert
andssl_key
, with certificate and key respectively, when creating a new healthcheck object. #41
- Adds
set_all_target_statuses_for_hostname
, which sets the targets for all entries with a given hostname at once.
- Fix: when
ngx.sleep
API is not available (e.g. in the log phase) it is not possible to lock using lua-resty-lock and any function that needs exclusive access would fail. This fix adds a retry method that starts a new light thread, which has access tongx.sleep
, to lock the critical path. #37;
- Fix: fail when it is not possible to get exclusive access to the list of targets. This fix prevents that workers get to an inconsistent state. #34;
- Add support for setting the custom
Host
header to be used for active checks. - Fix: log error on SSL Handshake failure #28;
- BREAKING: all API functions related to hosts require a
hostname
argument now. This way different hostnames listening on the same IP and ports combination do not have an effect on each other. - Fix: fix reporting active TCP probe successes #20; fixes issue #19
- Introduce
checks.active.https_verify_certificate
field. It istrue
by default; setting it tofalse
disables certificate verification in active healthchecks over HTTPS.
- Add support for
https
-- thanks @gaetanfl for the PR! - Introduce separate
checks.active.type
andchecks.passive.type
fields; the top-leveltype
field is still supported as a fallback but is now deprecated.
- Fix
Host
header in active healthchecks
- Fix internal management of healthcheck counters
- Correct setting of defaults in
http_statuses
- Type and bounds checking to
checks
table
- Disable individual checks by setting their counters to 0
- Adds
set_target_status
- Initial upload
Copyright 2017-2022 Kong Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.