Sync charm-helpers

Change-Id: Ic7cf6265426b467756bc1ee9343b87011bc838bc
Signed-off-by: Raven Kaur <raven.kaur@canonical.com>
This commit is contained in:
Raven Kaur
2025-11-21 15:33:11 -05:00
parent ad685273b4
commit 9f69795fc9

View File

@@ -1697,29 +1697,33 @@ MAX_DEFAULT_WORKERS = 4
DEFAULT_MULTIPLIER = 2
def _calculate_workers():
def _calculate_workers(multiplier=None):
'''
Determine the number of worker processes based on the CPU
count of the unit containing the application.
Determine the number of worker processes based on the CPU count of the
unit containing the application, scaled by the provided multiplier or
the worker-multiplier configuration option.
Workers will be limited to MAX_DEFAULT_WORKERS in
container environments where no worker-multipler configuration
option been set.
container environments when no multiplier is
provided and worker-multiplier configuration option has not been set.
@param multiplier: float: scaling factor for CPU count
@returns int: number of worker processes to use
'''
multiplier = config('worker-multiplier')
cpu_scaling_factor = multiplier
if cpu_scaling_factor is None or cpu_scaling_factor <= 0:
cpu_scaling_factor = config('worker-multiplier')
# distinguish an empty config and an explicit config as 0.0
if multiplier is None:
multiplier = DEFAULT_MULTIPLIER
if cpu_scaling_factor is None:
cpu_scaling_factor = DEFAULT_MULTIPLIER
count = int(_num_cpus() * multiplier)
count = int(_num_cpus() * cpu_scaling_factor)
if count <= 0:
# assign at least one worker
count = 1
if config('worker-multiplier') is None:
if multiplier is None and config('worker-multiplier') is None:
# NOTE(jamespage): Limit unconfigured worker-multiplier
# to MAX_DEFAULT_WORKERS to avoid insane
# worker configuration on large servers
@@ -1770,6 +1774,29 @@ class WSGIWorkerConfigContext(WorkerConfigContext):
enable_wsgi_socket_rotation = config('wsgi-socket-rotation')
if enable_wsgi_socket_rotation is None:
enable_wsgi_socket_rotation = True
try:
admin_worker_multiplier = config('admin-worker-multiplier')
except Exception:
admin_worker_multiplier = None
if admin_worker_multiplier is None or admin_worker_multiplier <= 0:
admin_processes = int(math.ceil(
self.admin_process_weight * total_processes))
else:
admin_processes = _calculate_workers(admin_worker_multiplier)
try:
public_worker_multiplier = config('public-worker-multiplier')
except Exception:
public_worker_multiplier = None
if public_worker_multiplier is None or public_worker_multiplier <= 0:
public_processes = int(math.ceil(
self.public_process_weight * total_processes))
else:
public_processes = _calculate_workers(public_worker_multiplier)
ctxt = {
"service_name": self.service_name,
"user": self.user,
@@ -1778,10 +1805,8 @@ class WSGIWorkerConfigContext(WorkerConfigContext):
"admin_script": self.admin_script,
"public_script": self.public_script,
"processes": int(math.ceil(self.process_weight * total_processes)),
"admin_processes": int(math.ceil(self.admin_process_weight *
total_processes)),
"public_processes": int(math.ceil(self.public_process_weight *
total_processes)),
"admin_processes": admin_processes,
"public_processes": public_processes,
"threads": 1,
"wsgi_socket_rotation": enable_wsgi_socket_rotation,
}