• Stars
    star
    119
  • Rank 287,864 (Top 6 %)
  • Language
    Lua
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Healthcheck library for OpenResty to validate upstream service status

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.

Async behaviour

Since this library heavily uses the SHM to share data between workers, it must use locks. The locks themselves need access to ngx.sleep which is not available in all contexts. Most notably not during startup; init and init_worker.

The library will try and acquire the lock and update, but if it fails it will schedule an async update (timer with delay 0).

One workaround for this in the initial phases would be to replace ngx.sleep with a version that does a blocking sleep in init/init_worker. This will enable the usage of locks in those phases.

History

Versioning is strictly based on Semantic Versioning

Releasing new versions:

  • update changelog below (PR's should be merged including a changelog entry)
  • based on changelog determine new SemVer version
  • create a new rockspec
  • render the docs using ldoc (don't do this within PR's)
  • commit as "release x.x.x" (do not include rockspec revision)
  • tag the commit with "x.x.x" (do not include rockspec revision)
  • push commit and tag
  • upload rock to luarocks: luarocks upload rockspecs/[name] --api-key=abc

2.0.0 (22-Sep-2020)

  • BREAKING: fallback for deprecated top-level field type is now removed (deprecated since 0.5.0) #56
  • BREAKING: Bump lua-resty-worker-events dependency to 2.0.0. This makes a lot of the APIs in this library asynchronous as the worker events post and post_local won't anymore call poll on a running worker automatically, for more information, see: https://github.com/Kong/lua-resty-worker-events#200-16-september-2020
  • BREAKING: tcp_failures can no longer be 0 on http(s) checks (unless http(s)_failures are also set to 0) #55
  • feature: Added support for https_sni #49
  • fix: properly log line numbers by using tail calls #29
  • fix: when not providing a hostname, use IP #48
  • fix: makefile; make install
  • feature: added a status version field #54
  • feature: add headers for probe request #54
  • fix: exit early when reloading during a probe #47
  • fix: prevent target-list from being nil, due to async behaviour #44
  • fix: replace timer and node-wide locks with resty-timer, to prevent interval skips #59
  • change: added additional logging on posting events #25
  • fix: do not run out of timers during init/init_worker when adding a vast amount of targets #57
  • fix: do not call on the module table, but use a method for locks. Also in #57

1.6.2 (17-Nov-2022)

  • 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

1.6.1 (25-Jul-2022)

  • 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

1.6.0 (27-Jun-2022)

  • 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

1.5.3 (14-Nov-2022)

  • 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. #121

1.5.2 (07-Jul-2022)

  • Better handling of resty.lock failure modes, adding more checks to ensure the lock is held before running critical code, and improving the decision whether a function should be retried after a timeout trying to acquire a lock. #113
  • Increased logging for locked function failures. #114
  • The cleanup frequency of deleted targets was lowered, cutting the number of created locks in a short period. #116

1.5.1 (23-Mar-2022)

  • Fix: avoid breaking active health checks when adding or removing targets. #93

1.5.0 (09-Feb-2022)

  • 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

1.4.3 (31-Mar-2022)

  • Fix: avoid breaking active health checks when adding or removing targets. #100

1.4.2 (29-Jun-2021)

  • 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

1.4.1 (17-Feb-2021)

  • Fix: make sure that a single worker will actively check hosts' statuses. #67

1.4.0 (07-Jan-2021)

  • 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

1.3.0 (17-Jun-2020)

  • Adds support to mTLS to active healthchecks. This feature can be used adding the fields ssl_cert and ssl_key, with certificate and key respectively, when creating a new healthcheck object. #41

1.2.0 (13-Feb-2020)

  • Adds set_all_target_statuses_for_hostname, which sets the targets for all entries with a given hostname at once.

1.1.2 (19-Dec-2019)

  • 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 to ngx.sleep, to lock the critical path. #37;

1.1.1 (14-Nov-2019)

  • 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;

1.1.0 (30-Sep-2019)

  • Add support for setting the custom Host header to be used for active checks.
  • Fix: log error on SSL Handshake failure #28;

1.0.0 (05-Jul-2019)

  • 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

0.6.1 (04-Apr-2019)

  • Fix: set up event callback only after target list is loaded #18; fixes Kong issue #4453

0.6.0 (26-Sep-2018)

  • Introduce checks.active.https_verify_certificate field. It is true by default; setting it to false disables certificate verification in active healthchecks over HTTPS.

0.5.0 (25-Jul-2018)

  • Add support for https -- thanks @gaetanfl for the PR!
  • Introduce separate checks.active.type and checks.passive.type fields; the top-level type field is still supported as a fallback but is now deprecated.

0.4.2 (23-May-2018)

  • Fix Host header in active healthchecks

0.4.1 (21-May-2018)

  • Fix internal management of healthcheck counters

0.4.0 (20-Mar-2018)

  • Correct setting of defaults in http_statuses
  • Type and bounds checking to checks table

0.3.0 (18-Dec-2017)

  • Disable individual checks by setting their counters to 0

0.2.0 (30-Nov-2017)

  • Adds set_target_status

0.1.0 (27-Nov-2017) Initial release

  • Initial upload

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.

More Repositories

1

kong

🦍 The Cloud-Native API Gateway and AI Gateway.
Lua
37,159
star
2

insomnia

The open-source, cross-platform API client for GraphQL, REST, WebSockets and gRPC.
JavaScript
30,407
star
3

unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
Java
2,560
star
4

kubernetes-ingress-controller

🦍 Kong for Kubernetes: The official Ingress Controller for Kubernetes.
Go
2,127
star
5

swrv

Stale-while-revalidate data fetching for Vue
TypeScript
2,048
star
6

mockbin

Mock, Test & Track HTTP Requests and Response for Microservices
JavaScript
1,988
star
7

mashape-oauth

OAuth Modules for Node.js - Supporting RSA, HMAC, PLAINTEXT, 2,3-Legged, 1.0a, Echo, XAuth, and 2.0
JavaScript
1,781
star
8

docker-kong

🐒 Docker distribution for Kong
Shell
1,351
star
9

unirest-php

Unirest in PHP: Simplified, lightweight HTTP client library.
PHP
1,282
star
10

httpsnippet

HTTP Request snippet generator for many languages & libraries
TypeScript
1,061
star
11

unirest-nodejs

Unirest in Node.js: Simplified, lightweight HTTP client library.
JavaScript
954
star
12

guardian

Remove the OAuth dance with one request.
JavaScript
640
star
13

unirest-python

Unirest in Python: Simplified, lightweight HTTP client library.
Python
432
star
14

deck

decK: Configuration management and drift detection for Kong
Go
419
star
15

apiembed

Embeddable API code snippets for your website, blog or API documentation
Pug
402
star
16

unirest-ruby

Unirest in Ruby: Simplified, lightweight HTTP client library.
Ruby
365
star
17

unirest-obj-c

Unirest in Objective-C: Simplified, lightweight HTTP client library.
Objective-C
276
star
18

kong-dist-kubernetes

Kubernetes managed Kong cluster
Shell
255
star
19

kong-plugin

Simple template to get started with custom Kong plugins
Lua
230
star
20

charts

Helm chart for Kong
Mustache
224
star
21

unirest-net

Unirest in .NET: Simplified, lightweight HTTP client library.
C#
190
star
22

lua-resty-worker-events

Cross Worker Events for Nginx in Pure Lua
Lua
186
star
23

docs.konghq.com

🦍 Source code for docs.konghq.com website.
Ruby
186
star
24

kong-oauth2-hello-world

This is a simple node.js + express.js application that shows an authorization page for the OAuth 2.0 plugin on Kong.
JavaScript
173
star
25

kong-manager

Admin GUI for Kong Gateway (Official)
TypeScript
170
star
26

lua-resty-dns-client

Lua DNS client, load balancer, and utility library
Lua
151
star
27

kong-pongo

Tooling to run plugin tests with Kong and Kong Enterprise
Lua
139
star
28

go-pdk

Kong Go Plugin Development Kit
Go
126
star
29

kong-vagrant

🐒 Vagrantfile for Kong testing and development
Shell
125
star
30

kongponents

🦍 Kong Vue Component Library
Vue
119
star
31

kong-plugin-prometheus

Prometheus plugin for Kong - this plugin has been moved into https://github.com/Kong/kong, please open issues and PRs in that repo
Lua
119
star
32

apiglossary

Open source glossary of API terms, acronyms and industry buzzwords.
95
star
33

go-plugins

A collection of Kong plugins written in Go
Go
86
star
34

go-kong

Go binding for Kong's admin API
Go
81
star
35

kong-terraform-aws

Kong Terraform Module for AWS
HCL
77
star
36

kong-build-tools

Build tools to package and release Kong
Shell
77
star
37

homebrew-kong

🐒 Homebrew tap for Kong
Ruby
69
star
38

kong-dist-cloudformation

🐒 Kong CloudFormation Stack
66
star
39

go-pluginserver

Kong Go Plugin Server
Go
66
star
40

ngx_wasm_module

Nginx + WebAssembly
C
64
star
41

kong-plugin-zipkin

A Kong plugin for propogating zipkin spans and reporting spans to a zipkin server - this plugin has been moved into https://github.com/Kong/kong, please open issues and PRs in that repo
Lua
60
star
42

kong-operator

Kong Operator for Kubernetes and OpenShift
Mustache
58
star
43

lua-multipart

Multipart Parser for Lua
Lua
55
star
44

mashape-php-library

Mashape PHP Server Library - Easily create an API in PHP. You can use it for existing services or brand new cloud components.
PHP
50
star
45

gojira

Multi-purpose tool to ease development and testing of Kong by using Docker containers
Shell
45
star
46

HARchiver

[Deprecated] Universal Lightweight Proxy for Galileo
OCaml
41
star
47

koko

koko - Control Plane for Kong Gateway [open-source]
Go
41
star
48

unirest-website

Simplified, lightweight HTTP libraries in multiple languages
HTML
39
star
49

kong-python-pdk

Write Kong plugins in Python
Python
39
star
50

go-srp

Secure Remote Password library for Go
Go
38
star
51

tcpbin

TCP Request & Response Service, written in node.js
HTML
37
star
52

kong-portal-templates

Themes, components, and utilities to help you get started with the Kong Dev Portal.
CSS
35
star
53

kong-plugin-acme

Let's Encrypt and ACMEv2 integration with Kong - this plugin has been moved into https://github.com/Kong/kong, please open issues and PRs in that repo
Lua
34
star
54

kong-mesh-dist-kubernetes

Start Kong 1.0 as a K8s sidecar
Makefile
33
star
55

kubernetes-testing-framework

Golang Integration Testing Framework For Kubernetes APIs and Controllers.
Go
32
star
56

gateway-operator

Go
32
star
57

demo-scene

🦍 a collection of demos and examples around Kong tools and technologies
JavaScript
30
star
58

docker-java8

A Dockerfile for starting a container with Java 8 installed
30
star
59

lua-kong-nginx-module

Nginx C module to allow deeper control of Nginx behaviors by Kong Lua code
Perl
30
star
60

Astronode-Broadcaster

A TCP replication server, or broadcaster, that replicates TCP commands to other TCP servers
Java
29
star
61

opentracing-lua

Opentracing Library for Lua
Lua
28
star
62

konnect-portal

Konnect OSS Dev Portal
TypeScript
28
star
63

atc-router

Expression based matching library for Kong
Rust
28
star
64

kong-js-pdk

Kong PDK for Javascript and plugin server
JavaScript
28
star
65

boss.js

Automatically load balance asyncronous jobs across multiple processes in a round-robin fashion.
JavaScript
27
star
66

kong-portal-cli

Kong Developer Portal CLI
TypeScript
25
star
67

lua-uuid

Lua library to generate UUIDs leveraging libuuid
Lua
25
star
68

insomnia-docs

This repository houses all Insomnia documentation.
JavaScript
25
star
69

lua-resty-lmdb

Safe API for manipulating LMDB databases using OpenResty/Lua.
C
24
star
70

lua-resty-aws

AWS SDK for OpenResty
Lua
22
star
71

lua-resty-timer

Extended timers for OpenResty
Perl
22
star
72

lua-resty-events

Inter process Pub/Sub pattern for Nginx worker processes
Perl
22
star
73

kong-plugin-request-transformer

Kong request transformer plugin - this plugin has been moved into https://github.com/Kong/kong, please open issues and PRs in that repo
Lua
21
star
74

lua-resty-counter

Lock-free counter for OpenResty
Perl
21
star
75

kong-plugin-session

🍪 Session plugin for Kong - this plugin has been moved into https://github.com/Kong/kong, please open issues and PRs in that repo
Lua
20
star
76

lua-pack

A library for packing and unpacking binary data.
C
20
star
77

go-apiops

Kong's Go based APIOps library
Go
18
star
78

swagger-ui-kong-theme

Plugin theme for Swagger-UI that adds snippets
JavaScript
18
star
79

api-log-format

Specification and examples of the new API logging format ALF
17
star
80

kong-plugin-serverless-functions

Kong Serverless Plugins - this plugin has been moved into https://github.com/Kong/kong, please open issues and PRs in that repo
Lua
17
star
81

apistatus

API status is a simple tool that checks if an API is online. http://apistatus.org
JavaScript
15
star
82

openresty-patches

Moved to https://github.com/Kong/kong-build-tools
Perl
14
star
83

kong-plugin-grpc-gateway

Kong Plugin to transcode REST request to gRPC - this plugin has been moved into https://github.com/Kong/kong, please open issues and PRs in that repo
Lua
14
star
84

lua-resty-consul-event

Consul Events HTTP API Wrapper
Perl
14
star
85

srp-js

Fork of node-srp modified to work in the browser
TypeScript
14
star
86

harplayer

Replay HAR logs
JavaScript
13
star
87

kong-plugin-aws-lambda

AWS Lambda plugin - this plugin has been moved into https://github.com/Kong/kong, please open issues and PRs in that repo
Lua
13
star
88

openresty-build-tools

Moved to https://github.com/Kong/kong-build-tools
Shell
13
star
89

jenkins-infrastructure

Cloudformation to create and update an ECS cluster that runs jenkins
Shell
12
star
90

kong-plugin-proxy-cache

HTTP Proxy Caching for Kong - this plugin has been moved into https://github.com/Kong/kong, please open issues and PRs in that repo
Lua
12
star
91

openapi2kong

Lib to convert OpenAPI specs into Kong specs
Lua
12
star
92

version.lua

Simple version comparison library
Lua
11
star
93

httpbin

Python
11
star
94

changelog-generator

a changelog generator focused on flexibility and ease of use
TypeScript
11
star
95

vault-kong-secrets

A Kong secrets backend for Vault
Go
11
star
96

py-postgrest

A library to work with PostgREST based APIs from Python
Python
11
star
97

priority-updater

Tool to quickly create a plugin with an updated priority
Lua
11
star
98

galileo-agent-java

Java Agent for Mashape Galileo
Java
10
star
99

kong-plugin-openwhisk

A kong plugin to invoke OpenWhisk action (serverless functions as service).
Lua
10
star
100

kong-upgrade-tests

Tests for upgrading from one Kong version to the next
Shell
10
star