Repository navigation
feat(config): add default Gunicorn settings - #463
Conversation
Add `foca.gunicorn_config`, a Gunicorn config module with the container settings of cloud-registry (bind 0.0.0.0:8080, one gthread worker with four threads, timeouts below the pod's grace period, keep-alive longer than that of proxies, explicit request limits, no access log, no control socket, heartbeat files in /dev/shm if writable). It does not import Gunicorn, which goes into the dev extra for the tests only. The petstore image uses it. GUNICORN_CMD_ARGS still takes precedence. Co-Authored-By: Claude Opus 5.5 <[email protected]>
List the settings of `foca.gunicorn_config` in "Running in production", why they are set and how to override them; link to them from the Helm chart's notes on its `gunicorn` values, and mention them in the migration guide. Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Correct the reason for no access log: FOCA logs only the traffic of controllers decorated with `log_traffic` - Note that the Helm chart's keep-alive (75 seconds) applies instead - Tests: restore `sys.path` after loading the settings, check the heartbeat directory against the helper, test the star import, and drop the health check test (the image has no health check) Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThis PR introduces an opt-in, dependency-free Sequence diagram for Gunicorn configuration precedencesequenceDiagram
participant App as App container
participant Gunicorn
participant Config as foca.gunicorn_config
participant Env as GUNICORN_CMD_ARGS
participant WSGI as wsgi:create_app
App->>Gunicorn: Start with --config python:foca.gunicorn_config
Gunicorn->>Config: Load default settings
Gunicorn->>Env: Read overrides
Env-->>Gunicorn: Override matching settings
Gunicorn->>WSGI: Create app in worker process
WSGI-->>Gunicorn: WSGI application
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #463 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 28 29 +1
Lines 3266 3287 +21
=========================================
+ Hits 3266 3287 +21
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="foca/gunicorn_config.py" line_range="50-52" />
<code_context>
+bind = ["0.0.0.0:8080"]
+
+# One process with a few threads, as each worker loads the whole app
+worker_class = "gthread"
+workers = 1
+threads = 4
+
+# Workers whose main loop does not report to Gunicorn for `timeout` seconds
</code_context>
<issue_to_address>
**Slow uploads exhaust request threads**
When an external client can reach an app with `server.max_content_length` unset and no upstream request-size or read-time limit, `gthread` serves requests in only four threads, and the worker timeout does not count requests running in those threads. With no effective body or read-time limit, slow uploads occupy all four threads and the app stops serving requests, including to authenticated users.
Ensure the default deployment enforces a finite request-body limit and an appropriate upstream read timeout, or use worker settings that prevent slow requests from indefinitely exhausting the available request threads.
</issue_to_address>
### Comment 2
<location path="tests/test_gunicorn_config.py" line_range="105" />
<code_context>
+ assert not cfg.preload_app
+ # Not set: Gunicorn's defaults
+ assert cfg.forwarded_allow_ips == Config().forwarded_allow_ips
+ assert cfg.worker_tmp_dir == _worker_tmp_dir()
+
+
</code_context>
<issue_to_address>
**Disk-backed heartbeat files pass**
When the helper's default path is changed to `/tmp` while `/dev/shm` is writable, `test_settings` compares `cfg.worker_tmp_dir` with `_worker_tmp_dir()` at `tests/test_gunicorn_config.py:105`; if both use `/tmp` instead of `/dev/shm`, they still match. The helper tests pass an explicit `tmp_path` at `:184`, so they also pass without checking the default path.
Add an assertion that the default selects `/dev/shm` when it is writable.
</issue_to_address>Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: foca/gunicorn_config.py:52, tests/test_gunicorn_config.py:105
- Docs: serve the app behind a reverse proxy that buffers requests, as slow uploads would otherwise keep the few threads of a worker busy - Test that the heartbeat directory is `/dev/shm` by default Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Moves cloud-registry's Gunicorn settings into FOCA, so that apps share one set of container defaults:
foca.gunicorn_config: a Gunicorn config module (gunicorn --config python:foca.gunicorn_config "wsgi:create_app()") with the settings of the issue:bind = ["0.0.0.0:8080"], onegthreadworker with 4 threads,timeout = 30,graceful_timeout = 20,keepalive = 100, explicit request limits,accesslog = None,errorlog = "-",control_socket_disable = True,worker_tmp_dirset to/dev/shmif it is writable (else Gunicorn's default).preload_appandforwarded_allow_ipsare not set. The module does not import Gunicorn. It has an__all__, sofrom foca.gunicorn_config import *exports only the settings.gunicorn>=26.2,<27goes into thedevextra only, for the tests. There is nogunicornextra (the issue left this open and defaults to none).gunicorn --config python:foca.gunicorn_config "wsgi:create_app()". The access-control example has no Gunicorn command (it runs the development server), so it is unchanged.tests/test_gunicorn_config.py, from cloud-registry): Gunicorn loads the settings without the app, restoringsys.argv, the environment and the working directory. They also check thatGUNICORN_CMD_ARGSoverrides the settings, the keep-alive against proxies andworker_connections, the/dev/shmfallback (missing, read-only, or a file), that the module does not import Gunicorn, that the star import exports exactly the settings, and the petstore image's command. cloud-registry's health-check port test is left out, as the petstore image has no health check.docs/deployment.md) uses the module. It lists each setting and why it is set (no preloading, no control socket, keep-alive vs. proxy timeouts, graceful timeout vs. termination grace period) and shows both ways to override them (GUNICORN_CMD_ARGS, or an app module that star-imports FOCA's).docs/helm-chart.mdlinks there from thegunicornvalues.docs/migration-0.18.mdmentions the change under "Other changes": it is opt-in, so no migration is needed.Verification
coverage run -m pytest -m "not mongodb": all passed, 100% coverageruff check,ruff format --check,mypy: passmkdocs build --strict: passpre-commit run --all-files: passReviewed by the adversarial and quality reviewer subagents (2 rounds). Fixed: the access-log rationale (FOCA logs only the traffic of
log_trafficcontrollers), the note that the Helm chart's keep-alive (75 s) applies instead of the module's, thesys.pathleak of the test helper, a vacuous health-check test, and a tautologicalworker_tmp_dirassertion.Sourcery: added a docs note to serve the app behind a buffering reverse proxy (slow uploads vs. few threads; the settings stay as the issue sets them), and a test that the heartbeat directory defaults to
/dev/shm.Declined review findings
monkeypatchinstead of manual restore (quality, low): kept as in cloud-registry. The helper is also called outside of fixtures.Open questions
None.
Closes #458
🤖 Generated with Claude Code