From 08848e4c5cba07fb67ba926cc52190af6cf525e3 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 17 Feb 2026 11:31:25 +0000 Subject: [PATCH] Load static files before Swoole fork for copy-on-write memory sharing Move Files::load() from EVENT_WORKER_START (per-worker) to before the Swoole server starts. This allows forked workers to share the loaded file data via OS copy-on-write instead of each worker allocating its own copy (~22MB per worker). Co-Authored-By: Claude Opus 4.6 --- app/http.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/app/http.php b/app/http.php index 1d7949da86..3414d3ba9d 100644 --- a/app/http.php +++ b/app/http.php @@ -37,7 +37,8 @@ use Utopia\System\System; const DOMAIN_SYNC_TIMER = 30; // 30 seconds -$files = null; +$files = new Files(); +$files->load(__DIR__ . '/../public'); $domains = new Table(1_000_000); // 1 million rows $domains->column('value', Table::TYPE_INT, 1); @@ -164,11 +165,7 @@ $http Constant::OPTION_TASK_WORKER_NUM => 1, // required for the task to fetch domains background ]); -$http->on(Constant::EVENT_WORKER_START, function ($server, $workerId) use (&$files) { - if (!$server->taskworker) { - $files = new Files(); - $files->load(__DIR__ . '/../public'); - } +$http->on(Constant::EVENT_WORKER_START, function ($server, $workerId) { }); $http->on(Constant::EVENT_WORKER_STOP, function ($server, $workerId) { @@ -452,7 +449,7 @@ $http->on(Constant::EVENT_START, function (Server $http) use ($payloadSize, $tot }); }); -$http->on(Constant::EVENT_REQUEST, function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) use ($register, &$files) { +$http->on(Constant::EVENT_REQUEST, function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) use ($register, $files) { Span::init('http.request'); Http::setResource('swooleRequest', fn () => $swooleRequest); @@ -463,7 +460,7 @@ $http->on(Constant::EVENT_REQUEST, function (SwooleRequest $swooleRequest, Swool Span::add('http.method', $request->getMethod()); - if ($files instanceof Files && $files->isFileLoaded($request->getURI())) { + if ($files->isFileLoaded($request->getURI())) { $time = (60 * 60 * 24 * 45); // 45 days cache $response