Skip to content

Commit

Permalink
[IMP] healthcheck: implement timeouts for healthchecks
Browse files Browse the repository at this point in the history
this should help if ports are/become firewalled for healthcheck to be
able to mark container as unhealthy
  • Loading branch information
ap-wtioit committed Feb 6, 2024
1 parent cb4f2c6 commit 2507c58
Show file tree
Hide file tree
Showing 7 changed files with 436 additions and 305 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ Default: `http://$TARGET/`
Url to use in [`HTTP_HEALTHCHECK`](#http_healthcheck) if enabled. `$TARGET` gets
replaced inside the url by the configured [`TARGET`](#target).

### `HTTP_HEALTHCHECK_TIMEOUT_MS`

Default: `2000`

Timeout in milliseconds for http healthcheck. This is used as a timeout for connecting
and receiving an answer. You may end up with twice the time spend.

### `MODE`

Default: `tcp`
Expand Down Expand Up @@ -143,6 +150,13 @@ Default: `HELP`
Enables changing the healthcheck command for servers that do not support `HELP` (e.g.
for [MailHog](https://github.com/mailhog/MailHog) you can use `QUIT`)

### `SMTP_HEALTHCHECK_TIMEOUT_MS`

Default: `2000`

Timeout in milliseconds for smtp healthcheck. This is used as a timeout for connecting
and receiving an answer. You may end up with twice the time spend.

### `UDP_ANSWERS`

Default: `1`
Expand Down
6 changes: 6 additions & 0 deletions healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def http_healthcheck():
import pycurl

check_url = os.environ.get("HTTP_HEALTHCHECK_URL", "http://localhost/")
check_timeout_ms = int(os.environ.get("HTTP_HEALTHCHECK_TIMEOUT_MS", 2000))
target = os.environ.get("TARGET", "localhost")
check_url_with_target = check_url.replace("$TARGET", target)
port = re.search("https?://[^:]*(?::([^/]+))?", check_url_with_target)[1] or "80"
Expand All @@ -36,6 +37,8 @@ def http_healthcheck():
# do not send the request to the target directly but use our own socat proxy process to check if it's still
# working
request.setopt(pycurl.RESOLVE, ["{}:{}:127.0.0.1".format(target, port)])
request.setopt(pycurl.CONNECTTIMEOUT_MS, check_timeout_ms)
request.setopt(pycurl.TIMEOUT_MS, check_timeout_ms)
request.perform()
request.close()
except pycurl.error as e:
Expand All @@ -53,6 +56,7 @@ def smtp_healthcheck():

check_url = os.environ.get("SMTP_HEALTHCHECK_URL", "smtp://localhost/")
check_command = os.environ.get("SMTP_HEALTHCHECK_COMMAND", "HELP")
check_timeout_ms = int(os.environ.get("SMTP_HEALTHCHECK_TIMEOUT_MS", 2000))
target = os.environ.get("TARGET", "localhost")
check_url_with_target = check_url.replace("$TARGET", target)
port = re.search("smtp://[^:]*(?::([^/]+))?", check_url_with_target)[1] or "25"
Expand All @@ -64,6 +68,8 @@ def smtp_healthcheck():
# do not send the request to the target directly but use our own socat proxy process to check if it's still
# working
request.setopt(pycurl.RESOLVE, ["{}:{}:127.0.0.1".format(target, port)])
request.setopt(pycurl.CONNECTTIMEOUT_MS, check_timeout_ms)
request.setopt(pycurl.TIMEOUT_MS, check_timeout_ms)
request.perform()
request.close()
except pycurl.error as e:
Expand Down
Loading

0 comments on commit 2507c58

Please sign in to comment.