Repository navigation
Add --max-tasks-jitter to spread out worker restarts - #73
Merged
Merged
Conversation
Under --workers N --continuous --max-tasks K the workers start together and reach K at about the same time, so they restart together and the host briefly has no worker. Each worker now adds a random number from 0 to J, drawn once at start, to --max-tasks, as gunicorn's max_requests_jitter does. The draw is made in the worker, not the supervisor, so every worker and every restart gets its own, and it comes from SystemRandom so a project that seeds the random module does not make the draws alike. The limit drawn is printed at startup and carried as max_tasks on the Worker started record. The default of 0 leaves --max-tasks as it was, and the option is ignored without --max-tasks. Part of #64.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #64: the jitter on the task count only. The lifetime and memory conditions stay open in the issue.
Under
--workers N --continuous --max-tasks Kthe workers start together and reach K at about the same time, so they restart together and the host briefly has no worker.--max-tasks-jitter Jmakes each worker add a random number from 0 to J, drawn once at start, to--max-tasks, as gunicorn'smax_requests_jitterdoes.What changes
--max-tasks-jitter Jonrun_database_tasks, default 0. With 0,--max-tasksbehaves exactly as before.--workerssupervisor, so every worker and every restart gets its own value. A worker run by a process manager takes the option the same way.random.SystemRandom, so a project that seeds therandommodule at import does not make every worker draw the same value.Max tasks: 1037 (1000 + jitter 37 of up to 100)) and carried as a newmax_tasksfield on theWorker startedlog record.Reached max tasks limitreports the drawn limit.Decisions to check
--max-tasks-jitter, an integer count, as proposed in the issue.--max-tasksthe option is ignored, as gunicorn ignoresmax_requests_jitterwithoutmax_requests, rather than refused. A negative value is refused with aCommandError.max_tasksis added toWorker startedso the drawn value is visible in a fleet's logs; it is 0 when there is no limit. No existing field or message changes.Compatibility
Default 0 keeps the current behaviour; the only change to existing output is the added log field. Existing projects need no changes.
Testing
venv(Django 6.0) and.venv(Django 6.1,-W error::DeprecationWarning) pass, as does the suite against PostgreSQL 16. A smoke run of--workers 3 --continuous --max-tasks 3 --max-tasks-jitter 4on PostgreSQL showed restarted workers drawing different limits.