CivicDataLab / CivicDataLab/DataSpaceBackend
Health check builds a new Elasticsearch client on every request
@amit0539 is already working on this.
Since Sep 8, 2026.
- Dominant language
- Python
- Stars
- 2
- Forks
- 1
- Avg merge
- 52m
- Merged PRs (30d)
- 40
Description
api/views/health.py constructs a new Elasticsearch client on every call:
es_settings = settings.ELASTICSEARCH_DSL["default"]
es = Elasticsearch(hosts=es_settings["hosts"], http_auth=es_settings["http_auth"])
if es.ping():
Visible in the logs as a new TCP connection each time:
{"event": "Starting new HTTP connection (1): elasticsearch:9200"}
{"event": "http://elasticsearch:9200 \"HEAD / HTTP/1.1\" 200 0"}
Why it is worth fixing
/health/ is called by the Docker healthcheck, the deploy gate in scripts/ci-deploy.sh, and any external monitoring — so this is a fresh connection several times a minute, forever, to send one HEAD /.
Measured cost is small (the ping itself is ~0.14s, and the endpoint answers in ~0.04s on-box), so this is efficiency and log noise rather than a performance problem.
It also emits a DeprecationWarning on every call:
The 'http_auth' parameter is deprecated. Use 'basic_auth' or 'bearer_auth' parameters instead
Suggested
- Reuse a module-level client, or the one
django-elasticsearch-dslalready configures, instead of building one per request. - Move
http_authtobasic_authwhile there.
Worth keeping the check itself: it is what makes /health/ return 503 when Elasticsearch is down, which the deploy gate depends on. Only the per-request construction should change.
Low priority. Related: the health check's database probe was fixed in #140 to open a fresh connection deliberately — that one is intentional and should stay.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.