Home

Awesome

lua-resty-healthcheck

latest version latest luarocks version master branch License Twitter Follow

A health check library for OpenResty.

Synopsis

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
    }
}

Description

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.

History

Versioning is strictly based on Semantic Versioning

Releasing new versions:

3.1.0 (19-Jun-2024)

3.0.2 (16-May-2024)

3.0.1 (22-Dec-2023)

3.0.0 (12-Oct-2023)

2.0.0 (22-Sep-2020)

Note: Changes in this version has been discarded from current & future development. Below you can see it's changelog but be aware that these changes might not be present in 3.y.z unless they are explicitly stated in 3.y.z, 1.6.3 or previous releases. Read more at: release 3.0.0 (#142) and chore(*): realign master branch to 3.0.0 release (#144)

1.6.3 (06-Sep-2023)

1.6.2 (17-Nov-2022)

1.6.1 (25-Jul-2022)

1.6.0 (27-Jun-2022)

1.5.3 (14-Nov-2022)

1.5.2 (07-Jul-2022)

1.5.1 (23-Mar-2022)

1.5.0 (09-Feb-2022)

1.4.3 (31-Mar-2022)

1.4.2 (29-Jun-2021)

1.4.1 (17-Feb-2021)

1.4.0 (07-Jan-2021)

1.3.0 (17-Jun-2020)

1.2.0 (13-Feb-2020)

1.1.2 (19-Dec-2019)

1.1.1 (14-Nov-2019)

1.1.0 (30-Sep-2019)

1.0.0 (05-Jul-2019)

0.6.1 (04-Apr-2019)

0.6.0 (26-Sep-2018)

0.5.0 (25-Jul-2018)

0.4.2 (23-May-2018)

0.4.1 (21-May-2018)

0.4.0 (20-Mar-2018)

0.3.0 (18-Dec-2017)

0.2.0 (30-Nov-2017)

0.1.0 (27-Nov-2017) Initial release

Copyright and License

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.