Skip to content

Commit fc71d76

Browse files
committed
make octane swoole support unix socket
1 parent 00e4d40 commit fc71d76

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

bin/createSwooleServer.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,24 @@
77

88
$sock = filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? SWOOLE_SOCK_TCP : SWOOLE_SOCK_TCP6;
99

10-
$server = new Swoole\Http\Server(
11-
$host,
12-
$serverState['port'] ?? 8000,
13-
$config['swoole']['mode'] ?? SWOOLE_PROCESS,
14-
($config['swoole']['ssl'] ?? false)
15-
? $sock | SWOOLE_SSL
16-
: $sock,
17-
);
10+
$unixSock = $serverState['sock'] ?? null;
11+
if (is_string($unixSock) && str_starts_with($unixSock, '/')) {
12+
$server = new Swoole\Http\Server(
13+
$unixSock, // Use UNIX socket
14+
0, // Port is not needed
15+
$config['swoole']['mode'] ?? SWOOLE_PROCESS,
16+
SWOOLE_SOCK_UNIX_STREAM
17+
);
18+
} else {
19+
$server = new Swoole\Http\Server(
20+
$host,
21+
$serverState['port'] ?? 8000,
22+
$config['swoole']['mode'] ?? SWOOLE_PROCESS,
23+
($config['swoole']['ssl'] ?? false)
24+
? $sock | SWOOLE_SSL
25+
: $sock,
26+
);
27+
}
1828
} catch (Throwable $e) {
1929
Laravel\Octane\Stream::shutdown($e);
2030

src/Commands/Concerns/InteractsWithServers.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,15 @@ protected function writeServerRunningMessage()
102102
{
103103
$this->components->info('Server running…');
104104

105+
if ($this->option('sock')) {
106+
$str = ' Local: <fg=white;options=bold>unix:'.$this->option('sock').' </>';
107+
} else {
108+
$str = ' Local: <fg=white;options=bold>'.($this->hasOption('https') && $this->option('https') ? 'https://' : 'http://').$this->getHost().':'.$this->getPort().' </>';
109+
}
110+
105111
$this->output->writeln([
106112
'',
107-
' Local: <fg=white;options=bold>'.($this->hasOption('https') && $this->option('https') ? 'https://' : 'http://').$this->getHost().':'.$this->getPort().' </>',
113+
$str,
108114
'',
109115
' <fg=yellow>Press Ctrl+C to stop the server</>',
110116
'',

src/Commands/StartCommand.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class StartCommand extends Command implements SignalableCommandInterface
1919
{--server= : The server that should be used to serve the application}
2020
{--host= : The IP address the server should bind to}
2121
{--port= : The port the server should be available on [default: "8000"]}
22+
{--sock= : The unix domain socket the server should bind to [Swoole only]}
2223
{--admin-port= : The port the admin server should be available on [FrankenPHP only]}
2324
{--rpc-host= : The RPC IP address the server should bind to}
2425
{--rpc-port= : The RPC port the server should be available on}
@@ -64,15 +65,18 @@ public function handle()
6465
*/
6566
protected function startSwooleServer()
6667
{
67-
return $this->call('octane:swoole', [
68+
$params = [
6869
'--host' => $this->getHost(),
6970
'--port' => $this->getPort(),
71+
'--sock' => $this->option('sock'),
7072
'--workers' => $this->option('workers') ?: config('octane.workers', 'auto'),
7173
'--task-workers' => $this->option('task-workers') ?: config('octane.task_workers', 'auto'),
7274
'--max-requests' => $this->option('max-requests') ?: config('octane.max_requests', 500),
7375
'--watch' => $this->option('watch'),
7476
'--poll' => $this->option('poll'),
75-
]);
77+
];
78+
79+
return $this->call('octane:swoole', $params);
7680
}
7781

7882
/**

src/Commands/StartSwooleCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class StartSwooleCommand extends Command implements SignalableCommandInterface
2424
public $signature = 'octane:swoole
2525
{--host= : The IP address the server should bind to}
2626
{--port= : The port the server should be available on}
27+
{--sock= : The unix socket the server should bind to [Swoole only]}
2728
{--workers=auto : The number of workers that should be available to handle requests}
2829
{--task-workers=auto : The number of task workers that should be available to handle tasks}
2930
{--max-requests=500 : The number of requests to process before reloading the server}
@@ -105,6 +106,7 @@ protected function writeServerStateFile(
105106
'appName' => config('app.name', 'Laravel'),
106107
'host' => $this->getHost(),
107108
'port' => $this->getPort(),
109+
'sock' => $this->option('sock'),
108110
'workers' => $this->workerCount($extension),
109111
'taskWorkers' => $this->taskWorkerCount($extension),
110112
'maxRequests' => $this->option('max-requests'),

0 commit comments

Comments
 (0)