Skip to content

Commit abd9ca9

Browse files
committed
allow fork server in threads
1 parent 644b571 commit abd9ca9

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

src/Child.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
4+
namespace Antidot\React;
5+
6+
class Child
7+
{
8+
public static function fork(int $numberOfWorkers, callable $asyncServer, int $numberOfFork = 0): void
9+
{
10+
$pid = pcntl_fork();
11+
if (-1 === $pid) {
12+
// @fail
13+
die('Fork failed');
14+
}
15+
16+
if (0 === $pid) {
17+
$asyncServer();
18+
pcntl_waitpid($pid, $status);
19+
return;
20+
}
21+
22+
// @parent
23+
$numberOfWorkers--;
24+
++$numberOfFork;
25+
if ($numberOfWorkers > 0) {
26+
self::fork($numberOfWorkers, $asyncServer, $numberOfFork);
27+
}
28+
29+
pcntl_waitpid($pid, $status);
30+
}
31+
}

src/Container/Config/ConfigProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public function __invoke(): array
2424
Socket::class => SocketFactory::class,
2525
],
2626
],
27-
'server' => []
27+
'server' => [
28+
'workers' => 1
29+
]
2830
];
2931
}
3032
}

src/PromiseResponse.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
namespace Antidot\React;
66

7-
use Psr\Http\Message\ResponseInterface;
87
use React\Promise\PromiseInterface;
98
use RingCentral\Psr7\Response;
10-
use Throwable;
119

1210
class PromiseResponse extends Response implements PromiseInterface
1311
{

src/ServerFactory.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Antidot\Application\Http\Application;
88
use Psr\Container\ContainerInterface;
9-
use Psr\Http\Message\ResponseInterface;
109
use Psr\Http\Message\ServerRequestInterface;
1110
use React\EventLoop\LoopInterface;
1211
use React\Http\Server;

src/SocketFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public function __invoke(ContainerInterface $container): Socket
1919
/** @var array<string, string|null> $config */
2020
$config = $globalConfig['server'];
2121

22-
return new Socket(sprintf(
23-
'%s:%s',
24-
$config['host'] ?? '0.0.0.0',
25-
$config['port'] ?? '8080'
26-
), $loop);
22+
return new Socket(
23+
sprintf('%s:%s', $config['host'] ?? '0.0.0.0', $config['port'] ?? '8080'),
24+
$loop,
25+
['tcp' => ['so_reuseport' => 2 <= $config['workers']]]
26+
);
2727
}
2828
}

0 commit comments

Comments
 (0)