reusePort = true; $http_worker->count = $process_count; $http_worker->onWorkerStart = static function () use ($test_type, $pool_size, &$db, &$date) { $db = match ($test_type) { 'pgsql' => new Pgsql(), 'mysql' => new Mysql(), 'pgsql-swow' => new PgsqlSwow($pool_size), 'mysql-swow' => new MysqlSwow($pool_size), 'pgsql-swoole' => new PgsqlSwoole($pool_size), 'mysql-swoole' => new MysqlSwoole($pool_size), 'default' => new Mysql(), }; $date = new Date(); }; Worker::$eventLoopClass = "Workerman\\Events\\$event_loop"; if ($event_loop === 'Swoole') { Coroutine::set(['hook_flags' => SWOOLE_HOOK_ALL]); } $http_worker->onMessage = static function ($connection, $request) use (&$db, &$date) { switch ($request->path()) { case '/plaintext': $connection->headers = [ 'Content-Type' => 'text/plain', 'Date' => $date->date ]; return $connection->send('Hello, World!'); case '/json': $connection->headers = [ 'Content-Type' => 'application/json', 'Date' => $date->date ]; return $connection->send(json_encode(['message' => 'Hello, World!'])); case '/db': $connection->headers = [ 'Content-Type' => 'application/json', 'Date' => $date->date ]; return $connection->send(json_encode($db->db())); case '/fortunes': $connection->headers = [ 'Date' => $date->date ]; return $connection->send($db->fortune()); case '/query': $connection->headers = [ 'Content-Type' => 'application/json', 'Date' => $date->date ]; return $connection->send(json_encode($db->query($request))); case '/update': $connection->headers = [ 'Content-Type' => 'application/json', 'Date' => $date->date ]; return $connection->send(json_encode($db->update($request))); } return $connection->send(new Response(404, [ 'Content-Type' => 'text/plain', 'Date' => $date->date ], '404 Not Found')); }; Worker::runAll();