Skip to content
Open
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
3 changes: 3 additions & 0 deletions changes-entries/proxy-hcheck-req-pool.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*) mod_proxy_hcheck: Don't grow memory each time the health check method
or URI of a worker is changed through the balancer-manager.
[Yann Ylavic]
16 changes: 15 additions & 1 deletion modules/proxy/mod_proxy_hcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ typedef struct {
const char *req; /* pre-formatted HTTP/AJP request */
proxy_worker *w; /* Pointer to the actual worker */
const char *protocol; /* HTTP 1.0 or 1.1? */
apr_pool_t *req_pool; /* For req once rebuilt, cleared when rebuilt again */
} wctx_t;

typedef struct {
Expand Down Expand Up @@ -599,7 +600,20 @@ static proxy_worker *hc_get_hcworker(sctx_t *ctx, proxy_worker *worker,
(int)port);
hc->s->method = worker->s->method;
PROXY_STRNCPY(hc->s->hcuri, worker->s->hcuri);
create_hcheck_req(wctx, hc, ctx->p);
/* Rebuild the request in a pool of its own, created at the first
* change only (a pool costs at least 8KB) and cleared at the next
* ones. No check of the worker can be using the old request here:
* the watchdog starts the next check only once the previous one is
* done.
*/
if (!wctx->req_pool) {
apr_pool_create(&wctx->req_pool, ctx->p);
apr_pool_tag(wctx->req_pool, "proxy_hcheck_req");
}
else {
apr_pool_clear(wctx->req_pool);
}
create_hcheck_req(wctx, hc, wctx->req_pool);
}
return hc;
}
Expand Down
77 changes: 77 additions & 0 deletions test/modules/proxy/test_09_hcheck_log_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
all in the logs (PR 66042).
"""

import http.client
import os
import re
import time
import urllib.parse

import pytest

Expand Down Expand Up @@ -48,3 +52,76 @@ def test_proxy_09_01_hcuri_is_logged(self, env):
# the health check's own (expected, deliberately provoked) connect
# failures against the unused backend port
env.httpd_error_log.ignore_recent(lognos=["AH00957"])


@needs_dependency("mod_proxy_hcheck", ProxyTestEnv.has_shared_module("proxy_hcheck"),
reason="mod_proxy_hcheck not available")
class TestHcheckMethodChange:
"""The health check request of a worker is rebuilt when its method is
changed through the balancer-manager (in a pool cleared each time, not
growing): each new method is the one used from then on.
"""

@pytest.fixture(autouse=True, scope='class')
@classmethod
def _class_scope(cls, env):
conf = HttpdConf(env)
conf.add([
'<Proxy "balancer://hcuchange">',
f' BalancerMember "http://127.0.0.1:{env.http_port}" '
'hcmethod=GET hcuri=/hcu-change hcinterval=1',
'</Proxy>',
'ProxyPass "/hcc/" "balancer://hcuchange/"',
'<Location /balancer-manager>',
' SetHandler balancer-manager',
' Require all granted',
'</Location>',
])
conf.install()
assert env.apache_restart() == 0

def _request(self, env, method, path, body=None):
conn = http.client.HTTPConnection("127.0.0.1", env.http_port, timeout=10)
try:
headers = {"Host": f"127.0.0.1:{env.http_port}",
"Referer": f"http://127.0.0.1:{env.http_port}/balancer-manager"}
if body is not None:
headers["Content-Type"] = "application/x-www-form-urlencoded"
conn.request(method, path, body=body, headers=headers)
r = conn.getresponse()
return r.status, r.read().decode(errors="replace")
finally:
conn.close()

def _wait_requested(self, env, method, timeout=10):
"""Whether a health check with method is logged after the current
end of the access log, within timeout.
"""
log = os.path.join(env.server_logs_dir, "access_log")
line = re.compile(rf'"{method} /+hcu-change HTTP/1\.0"')
start = os.path.getsize(log) if os.path.isfile(log) else 0
end = time.time() + timeout
while time.time() < end:
if os.path.isfile(log):
with open(log) as f:
f.seek(start)
if line.search(f.read()):
return True
time.sleep(0.2)
return False

def test_proxy_09_02_hcmethod_change(self, env):
assert self._wait_requested(env, "GET"), \
"the configured hcmethod was not used"
worker = f"http://127.0.0.1:{env.http_port}"
for method in ("HEAD", "GET", "HEAD"):
status, page = self._request(env, "GET", "/balancer-manager")
assert status == 200
m = re.search(r"b=hcuchange&[^\"]*?nonce=([0-9a-fA-F-]+)", page)
assert m, "no nonce for balancer://hcuchange"
body = (f"b=hcuchange&w={urllib.parse.quote(worker, safe='')}"
f"&nonce={m.group(1)}&w_hm={method}")
status, _ = self._request(env, "POST", "/balancer-manager", body)
assert status == 200
assert self._wait_requested(env, method), \
f"the health check did not use the new hcmethod {method}"
Loading