public static function create(string $bootstrap, int $workerCount = null) : ProcessManager { if (Socket::isUnixSocketSupported()) { $url = \sprintf('unix://%s/k1_ipc_%s.sock', \sys_get_temp_dir(), \md5((string) \microtime(true))); } else { $port = Socket::findUnusedPort('tcp', '127.0.0.1'); $url = \sprintf('tcp://127.0.0.1:%u', $port); } $errno = null; $errstr = null; $server = @\stream_socket_server($url, $errno, $errstr, \STREAM_SERVER_BIND | \STREAM_SERVER_LISTEN); if ($server === false) { throw new \RuntimeException(\sprintf('Failed to create IPC server "%s" [%s]: "%s"', $url, $errno, $errstr)); } return new static($url, $server, $bootstrap, $workerCount); }
public static function connect(string $dsn, string $username, string $password, LoggerInterface $logger = NULL) : \Generator { if ('mysql:' !== substr($dsn, 0, 6)) { throw new \InvalidArgumentException(sprintf('Invalid MySQL DSN: "%s"', $dsn)); } $settings = []; foreach (explode(';', substr($dsn, 6)) as $part) { list($k, $v) = array_map('trim', explode('=', $part)); switch ($k) { case 'host': case 'dbname': case 'unix_socket': $settings[$k] = $v; break; case 'port': $settings[$k] = (int) $v; break; default: throw new \InvalidArgumentException(sprintf('Unknown MySQL DSN param: "%s"', $k)); } } if (empty($settings['host']) && empty($settings['unix_socket'])) { throw new \InvalidArgumentException('Neighter MySQL host nor Unix domain socket specified in MySQL DSN'); } if (!empty($settings['unix_socket'])) { if (!Socket::isUnixSocketSupported()) { throw new \RuntimeException(sprintf('Cannot connect to MySQL socket "%s", PHP was not compiled with support for Unix domain sockets', $settings['unix_socket'])); } $client = new Client(yield from SocketStream::fromUrl('unix://' . $settings['unix_socket']), $logger); } else { $client = new Client(yield from SocketStream::connect($settings['host'], $settings['port'] ?? 3306), $logger); } yield from $client->handleHandshake($username, $password); $conn = new static($client, (yield currentExecutor()), $logger); if (!empty($settings['dbname'])) { yield from $conn->changeDefaultSchema($settings['dbname']); } return $conn; }
private static function createServerSocket() : array { if (Socket::isUnixSocketSupported()) { $uri = $connectUri = \sprintf('unix://%s/kk_pool_%s.sock', \sys_get_temp_dir(), \md5((string) \microtime(true))); } else { $uri = 'tcp://127.0.0.1:0'; } $errno = null; $errstr = null; $context = \stream_context_create(['socket' => ['backlog' => 128]]); $server = @\stream_socket_server($uri, $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context); if ($server === false) { throw new \RuntimeException(\sprintf('Failed to start IPC socket "%s": [%s] %s', $uri, $errno, $errstr)); } if (!isset($connectUri)) { $connectUri = 'tcp://' . \stream_socket_get_name($server, false); } return [$server, $connectUri]; }