Repository: appwrite/appwrite
File: app/http.php (PHP)

Code before:
 * Assigns HTTP requests to worker threads by analyzing its payload/content.
 *
 * Routes requests as 'safe' or 'risky' based on specific content patterns (like POST actions or certain domains)
 * to optimize load distribution between the workers. Utilizes `$safeThreadsPercent` to manage risk by assigning
 * riskier tasks to a dedicated worker subset. Prefers idle workers, with fallback to random selection if necessary.
 * doc: https://openswoole.com/docs/modules/swoole-server/configuration#dispatch_func
 *
 */
$swooleDispatchFunction = new class ($domains) extends \Utopia\Swoole\Dispatch\RiskyRequest {
    public function __construct(private readonly Table $domains)
    {
        parent::__construct(1024 * 1024, intval(System::getEnv('_APP_RISKY_WORKERS_PERCENT', 80)) / 100);
    }

    protected function isRisky(string $request, string $domain): bool
    {
        $risky = false;
        if (str_starts_with($request, 'POST') && str_contains($request, '/executions')) {
            $risky = true;
        } elseif (str_ends_with($domain, System::getEnv('_APP_DOMAIN_FUNCTIONS'))) {
            $risky = true;
        } elseif ($this->domains->get(md5($domain), 'value') === 1) {
            // executions request coming from custom domain
            $risky = true;
        }
        return $risky;
    }
};

$http
    ->set([
        'worker_num' => $totalWorkers,
        'dispatch_func' => $swooleDispatchFunction,
        'open_http2_protocol' => true,
        'http_compression' => false,
        'package_max_length' => $payloadSize,
        'buffer_output_size' => $payloadSize,
        'task_worker_num' => 1, // required for the task to fetch domains background
    ]);

$http->on(Constant::EVENT_WORKER_START, function ($server, $workerId) {
    Console::success('Worker ' . ++$workerId . ' started successfully');
});

$http->on(Constant::EVENT_BEFORE_RELOAD, function ($server, $workerId) {
    Console::success('Starting reload...');
});

$http->on(Constant::EVENT_AFTER_RELOAD, function ($server, $workerId) {
    Console::success('Reload completed...');
});

Review comment (suggestion):
```suggestion
        if (str_starts_with($request, 'POST') && str_contains($request, '/executions')) {
            return true;
        } elseif (str_ends_with($domain, System::getEnv('_APP_DOMAIN_FUNCTIONS'))) {
            return true;
        } elseif ($this->domains->get(md5($domain), 'value') === 1) {
            // executions request coming from custom domain
            return true;
        }
        return false;
```

we can probably skip the var 👍🏻 

Code after:
 * Assigns HTTP requests to worker threads by analyzing its payload/content.
 *
 * Routes requests as 'safe' or 'risky' based on specific content patterns (like POST actions or certain domains)
 * to optimize load distribution between the workers. Utilizes `$safeThreadsPercent` to manage risk by assigning
 * riskier tasks to a dedicated worker subset. Prefers idle workers, with fallback to random selection if necessary.
 * doc: https://openswoole.com/docs/modules/swoole-server/configuration#dispatch_func
 *
 * @param Server $server Swoole server instance.
 * @param int $fd client ID
 * @param int $type the type of data and its current state
 * @param string|null $data Request content for categorization.
 * @global int $totalThreads Total number of workers.
 * @return int Chosen worker ID for the request.
 */
function dispatch(Server $server, int $fd, int $type, $data = null): int
{
    $resolveWorkerId = function (Server $server, $data = null) {
        global $totalWorkers, $domains;

        // If data is not set we can send request to any worker
        // first we try to pick idle worker, if not we randomly pick a worker
        if ($data === null) {
            for ($i = 0; $i < $totalWorkers; $i++) {
                if ($server->getWorkerStatus($i) === SWOOLE_WORKER_IDLE) {
                    return $i;
                }
            }
            return rand(0, $totalWorkers - 1);
        }

        $riskyWorkersPercent = intval(System::getEnv('_APP_RISKY_WORKERS_PERCENT', 80)) / 100; // Decimal form 0 to 1

        // Each worker has numeric ID, starting from 0 and incrementing
        // From 0 to riskyWorkers, we consider safe workers
        // From riskyWorkers to totalWorkers, we consider risky workers
        $riskyWorkers = (int)floor($totalWorkers * $riskyWorkersPercent); // Absolute amount of risky workers

        $domain = '';
        // max up to 3 as first line has request details and second line has host
        $lines = explode("\n", $data, 3);
        $request = $lines[0];
        if (count($lines) > 1) {
            $domain = trim(explode('Host: ', $lines[1])[1]);
        }

        // Sync executions are considered risky
        $risky = false;
        if (str_starts_with($request, 'POST') && str_contains($request, '/executions')) {
            $risky = true;
        } elseif (str_ends_with($domain, System::getEnv('_APP_DOMAIN_FUNCTIONS'))) {
            $risky = true;